我需要代码和指导。我有一个按钮和一个人体模型。我创造了人类和两个纹理,但我的代码并没有改变人类的纹理。
public Sprite myImage;
public Button kirmizi;
void Start()
{
myImage = Resources.Load<Sprite>("SportyGrilSkin1.png"); // Make sure not to include the file extension
//Make sure it is added in the Inspector. Or reference it using GameObject.Find.
kirmizi.image.sprite = myImage; // That is right, no need to GetComponent.
}
// Update is called once per frame
void Update () {
}
答案 0 :(得分:0)
kirmizi.image.sprite = myImage;
这意味着将myImage
放入按钮精灵。
我认为你应该将myImage
加入human
的精灵。
public Sprite myImage;
public Sprite humanImage;
public SpriteRenderer humanSpriteRenderer;
void Start()
{
myImage = Resources.Load<Sprite>("SportyGrilSkin1.png"); // Make sure not to include the file extension
humanSpriteRenderer=GameObject.Find("human").GetComponent<SpriteRenderer>();
humanImage = GameObject.Find("human").GetComponent<SpriteRenderer>().sprite; // you need 'human' object. it has sprite.
}
void OnClick() {
humanSpriteRenderer.sprite=myImage;
//humanImage = myImage;
}
关于OnClick()
,它是Button组件的回调函数。
如果您对此不了解,请查看此页面。