一个按钮和一个text,点击按钮后text改变,再次点击,text恢复,改变屏幕分辨率,按钮适配中间,text适配左上角
public void onclick()
{
Text curText = GameObject.Find ("will_change").GetComponent () as Text;
if (curText.text == "hello unity") curText.text = "I have changed!";
else curText.text = "hello unity";
}
一个滑动条和一个text, 可以拖动滑动条,text可以实时显示对应的值 (最小值到最大值分别对应1~100)
public void slide_trig()
{
Text numberText = GameObject.Find ("number").GetComponent ();
Slider slide = GameObject.Find ("text_slider").GetComponent ();
int curNumber = (int)slide.value;
numberText.text = curNumber.ToString ();
}
一个按钮和一张图片,点击按钮切换图片,再次点击,图片恢复
public void image_change()
{
Image img = GameObject.Find("change_image").GetComponent() as Image;
string spritePath = string.Format("pic2");
Sprite oldSprite = Resources.Load(spritePath,typeof(Sprite)) as Sprite;
if (img.sprite == oldSprite)
{
spritePath = string.Format ("pic1");
img.sprite = Resources.Load (spritePath, typeof(Sprite)) as Sprite;
}
else
{
spritePath = string.Format ("pic2");
img.sprite = Resources.Load (spritePath, typeof(Sprite)) as Sprite;
}
//can be refactor follow if there are many picture
/*
Image img = GameObject.Find("change_image").GetComponent() as Image;
string spritePath = string.Format(pictures[index]);
index = (index + 1) % pictures.size();
img.sprite = Resources.Load (spritePath, typeof(Sprite)) as Sprite;
*/
}
一个下拉框和一个text,切换下拉框,text改变成对应的name
public void drop_text()
{
Dropdown curDrop = GameObject.Find ("dropToChange").GetComponent () as Dropdown;
GameObject.Find ("Drop_Text").GetComponent ().text = curDrop.captionText.text;
}
一个勾选框,默认状况不勾选,点击勾选,再次点击不勾选
* 在Unity中创建一个Toggle控件,并设置Is On属性
在主界面加一个button,点击button打开一个面板,面板要有关闭功能
* 在Unity中创建一个Button和Panel控件
* 创建一个C#脚本 ControllPanel.cs
public void open_or_close_panel(){
Text curText = GameObject.Find ("Panel_Display_Text").GetComponent ();
Image img = GameObject.Find ("Panel_Image").GetComponent ();
if (curText.text == "Display")
{
img.enabled = true;
curText.text = "Hide";
}
else
{
img.enabled = false;
curText.text = "Display";
}
}