2015-04-23 109 views

回答

1

假设你在hirerarchy按钮命名为按钮,这应该说是:

GameObject button; 

void Start() { 
button = GameObject.Find("Canvas").transform.Find("button").gameObject; 
} 

为了设置图像:

button.GetComponent<Image>().mainTexture = /* Texture goes here */; 

而且不要忘记!在UI元素时,您需要经常调用的UI库开始你的C#脚本:

using UnityEngine.UI; 
0

我会建议不要使用find(“按钮”),因为它可能会导致运行时错误,因为GameObject.Find(objectname)继电器上,如果你更改名称....可怕的东西可能发生的对象的名称;) 序列化,而不是:

[SerializeField] Button myButton = null;

然后只需拖动buttonreference对象与指定的脚本在编辑和电话: myButton.image.color = Color.white;

,如果你想改变纹理使用:

myButton.image.sprite = mySprite; //reference sprite in script too

这只能如果您使用统一4.6及以上和@Zee说你需要导入UnityEngine.UI

希望有所帮助:)