创建精灵并设置其颜色的函数
Function to create a sprite and set its color
我有一个从 SQL 数据库创建的项目。
到目前为止,我一直在成功使用 GetSprite() 函数...
如果指定了自定义颜色,现在我需要这个函数来更改精灵的颜色。
// The Items come from a SQL database
[System.Serializable]
public class Consumable : Item
{
// inherits public string Name;
// inherits public string ImagePath;
//specific to derived class Consumable
public string CustomColor = "";
public override Sprite GetSprite()
{
Sprite newSprite = Sprite.Create(Resources.Load<Texture2D>(ImagePath),
new Rect(0, 0, 64, 64), new Vector2(0.5f, 0.5f));
if(CustomColor != "")
{
// Change the color here
// Color col;
// ColorUtility.TryParseHtmlString(CustomColor, out col);
// newSprite.color = col; <--- I wish!
}
return newSprite;
}
public override bool IsConsumable()
{
return true;
}
}
public class ShowInventory : MonoBehaviour
{
// set in the inspector
// the itemViewPrefab object has a component of type Image
public GameObject itemViewPrefab;
// get a list of Items
List<Item> playersItemsFromTheDB = DBAccess.GetAllItemsForMyPlayer();
foreach(Item i in playersItemsFromTheDB)
{
//instantiate the itemViewPrefab (in the real app this forms a grid)
GameObject itemView = Instantiate(itemViewPrefab);
// set the Image for the item
itemView.GetComponent<Image>().sprite = i.GetSprite();
// HERE we could change the Image color but I would need to add this
// to about 100 different spots so it would be really nice if it
// was in the Consumable class. THE FOLLOWING DOES WORK:
// if( i.IsConsumable() && (i as Consumable).CustomColor != "") {
// Color newColor;
// ColorUtility.TryParseHtmlString((i as
// Consumable).CustomColor, out newColor);
// itemView.GetComponent<Image>().color = newColor;
// }
}
}
是否可以对精灵做这样的事情?
我应该将 GetSprite() 重写为 GetImage() 吗?
谢谢!
精灵没有颜色(图片有)
你有这条线的地方:
itemView.GetComponent<Image>().sprite = i.GetSprite();
这是您可以设置颜色的地方:
itemView.GetComponent<Image>().color = new Color(0,0,0,0); //fill in your own values here
我有一个从 SQL 数据库创建的项目。
到目前为止,我一直在成功使用 GetSprite() 函数... 如果指定了自定义颜色,现在我需要这个函数来更改精灵的颜色。
// The Items come from a SQL database
[System.Serializable]
public class Consumable : Item
{
// inherits public string Name;
// inherits public string ImagePath;
//specific to derived class Consumable
public string CustomColor = "";
public override Sprite GetSprite()
{
Sprite newSprite = Sprite.Create(Resources.Load<Texture2D>(ImagePath),
new Rect(0, 0, 64, 64), new Vector2(0.5f, 0.5f));
if(CustomColor != "")
{
// Change the color here
// Color col;
// ColorUtility.TryParseHtmlString(CustomColor, out col);
// newSprite.color = col; <--- I wish!
}
return newSprite;
}
public override bool IsConsumable()
{
return true;
}
}
public class ShowInventory : MonoBehaviour
{
// set in the inspector
// the itemViewPrefab object has a component of type Image
public GameObject itemViewPrefab;
// get a list of Items
List<Item> playersItemsFromTheDB = DBAccess.GetAllItemsForMyPlayer();
foreach(Item i in playersItemsFromTheDB)
{
//instantiate the itemViewPrefab (in the real app this forms a grid)
GameObject itemView = Instantiate(itemViewPrefab);
// set the Image for the item
itemView.GetComponent<Image>().sprite = i.GetSprite();
// HERE we could change the Image color but I would need to add this
// to about 100 different spots so it would be really nice if it
// was in the Consumable class. THE FOLLOWING DOES WORK:
// if( i.IsConsumable() && (i as Consumable).CustomColor != "") {
// Color newColor;
// ColorUtility.TryParseHtmlString((i as
// Consumable).CustomColor, out newColor);
// itemView.GetComponent<Image>().color = newColor;
// }
}
}
是否可以对精灵做这样的事情? 我应该将 GetSprite() 重写为 GetImage() 吗?
谢谢!
精灵没有颜色(图片有)
你有这条线的地方:
itemView.GetComponent<Image>().sprite = i.GetSprite();
这是您可以设置颜色的地方:
itemView.GetComponent<Image>().color = new Color(0,0,0,0); //fill in your own values here