【问题标题】:4.6 New UI How to change Button Image?4.6 新用户界面 如何更改按钮图像?
【发布时间】:2015-03-01 21:33:35
【问题描述】:

我搜索的所有内容都基于旧 UI。

我尝试了以下方法

button.image = Resource.Load<Image>("...");
button.GetComponent<Image>() = Resources.Load<Image>("....");
button.GetComponent<Button>().image = Resources.Load<Image>("....");
button.GetComponent<Image>().sprite = Resources.Load<Sprite>("....");

我想更改事件的按钮图像。

【问题讨论】:

    标签: c# unity3d unity3d-gui


    【解决方案1】:

    经过测试和工作。

    public Sprite myImage;
    public Button myBtn;
    void Start(){
    
             myImage = Resources.Load<Sprite>("BTNS"); // Make sure not to include the file extension
    
             //Make sure it is added in the Inspector. Or reference it using GameObject.Find.
             myBtn.image.sprite = myImage; // That is right, no need to GetComponent.
    
    }
    

    【讨论】:

      【解决方案2】:

      你也可以使用按钮组件的属性

      http://docs.unity3d.com/Manual/script-Button.html

      具体来说,您可以根据事件设置按钮的行为。

      例子:

      然后:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多