【问题标题】:if i click button, change my human texture. How can i do?如果我点击按钮,改变我的人体纹理。我能怎么做?
【发布时间】:2016-02-18 03:02:10
【问题描述】:

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

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

}

【问题讨论】:

  • 正如答案指出的那样,您将精灵放在按钮上而不是任何模型上,但也可能值得指出的是您的文件名中有错字。如果实际文件名有相同的拼写错误,这不会影响功能,但如果它不同,则值得指出。

标签: c# unity3d 3dsmax


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 2019-02-08
    • 2022-11-12
    • 2018-07-10
    • 1970-01-01
    • 2022-01-10
    • 2013-08-26
    • 2021-08-21
    • 2022-11-12
    • 1970-01-01
    相关资源
    最近更新 更多