如果我点击按钮,改变我的人体纹理。我能怎么做?

时间:2015-11-16 21:37:19

标签: c# unity3d 3dsmax

我需要代码和指导。我有一个按钮和一个人体模型。我创造了人类和两个纹理,但我的代码并没有改变人类的纹理。

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 () {

}

1 个答案:

答案 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组件的回调函数。

如果您对此不了解,请查看此页面。

UI Button OnClick Function