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 = 20;
        //字体 暂时没实现
        //sty.fontStyle = 
        //字体颜色
        sty.normal.textColor = Color.green;
        //背景
        sty.normal.background = null;
        
        //单行输入 20代表字符最多的个数
        userName = GUI.TextField(new Rect(120, 70, 200, 30),userName,20);
        //密码输入 '*' 代表密码输入的字符用*代替
        passWord = GUI.PasswordField(new Rect(120, 110, 200, 30), passWord, '*', 15);
        //多行输入
        message = GUI.TextArea(new Rect(400, 30, 200, 200), 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(10, 10, 600, 30), toolbarID, toolbarinfo);
        GUI.Label(new Rect(20, 40, 100, 20), toolbarinfo[toolbarID]);
    }
   void Start () {
        toolbarinfo = new string[] { "File", "Edit", "Assets" };
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}

你可能感兴趣的:(Unity-3D 简单控件)