天天看点

Unity-3D 简单控件

有关标签

public class Label : MonoBehaviour {
    public Texture **img**;
    void OnGUI()
    {
        //标签控件
        //文字
        GUI.Label(new Rect(x,y,weight,hight),"string");
        //图片
        GUI.Label(new Rect(x,y,weight,hight),**img**);//使用图片是需在前方定义一个图片的名字

        //按钮控件
        //单击按钮 按下即为true
        GUI.Button(new Rect(x,y,weight,hight),"string");
        //长按按钮 按下即为true
        GUI.RepeatButton(new Rect(x,y,weight,hight),"string");
    }
    void Update () {

    }
}
           

有关字体和输入

public class Label : MonoBehaviour {
    private string userName;
    private string passWord;
    private string message;
    void OnGUI()
    {
        //定义一种格式,用于修改文字字体字号等属性
        GUIStyle sty = new GUIStyle();
        //字号
        sty.fontSize = ;
        //字体 暂时没实现
        //sty.fontStyle = 
        //字体颜色
        sty.normal.textColor = Color.green;
        //背景
        sty.normal.background = null;

        //单行输入 20代表字符最多的个数
        userName = GUI.TextField(new Rect(, , , ),userName,);
        //密码输入 '*' 代表密码输入的字符用*代替
        passWord = GUI.PasswordField(new Rect(, , , ), passWord, '*', );
        //多行输入
        message = GUI.TextArea(new Rect(, , , ), message, sty);
    }

    //多有要用的字符串都必须提前初始化,否则将会报错
    void Start () 
    {
        userName = "";
        passWord = "";
        message = "";
        info = "";
        username = "wonameshuai";
        password = "nishuodedui";
     }
     void Update () {

    }
}
           

Toolbar

Int : Toolbar(rect,int(索引号),img(texture)/string);

第一个按钮按下的值为0

public class Toolbar : MonoBehaviour {
    private int toolbarID;
    private string[] toolbarinfo;
    void OnGUI()
    {
        toolbarID = GUI.Toolbar(new Rect(, , , ), toolbarID, toolbarinfo);
        GUI.Label(new Rect(, , , ), toolbarinfo[toolbarID]);
    }
   void Start () {
        toolbarinfo = new string[] { "File", "Edit", "Assets" };
    }

    // Update is called once per frame
    void Update () {

    }
}