OnGUI开发 1、Label文字开发。 private void OnGUI() { GUI.Label(new Rect(30, 10, 100, 200), “Open0”);//30,10,100,200分别对应X,Y,Width,Height. GUI.Label(new Rect(30, Screen.height - 50, 100, 200), “Open1”); GUI.Label(new Rect(Screen.width - 50, Screen.height - 50, 100, 200), “Open2”); GUI.Label(new Rect(Screen.width - 50, 10, 100, 200), “Open3”); } 注意事项:必须放到OnGUI里面,不然会报错。 2、Button按钮开发。 2.1普通按钮。 GUI.Button(new Rect(50, 50, 50, 50), “按钮”); 2.2带图片带文字的按钮 public Texture text; GUIContent guic = new GUIContent(“按钮”, text); GUI.Button(new Rect(150, 50, 50, 50), guic); 2.3按键从上往下排 自动排序 if (GUILayout.Button(“1”)) Debug.Log(“1”); if (GUILayout.Button(“2”)) Debug.Log(“2”); 3、Toggle控件 3.1普通Toggle. public bool toggle1; toggle1 = GUI.Toggle(new Rect(130, 220, 200, 30), toggle1, “Toggle”); 3.2带有图片的Toggle。 public bool toggle2; public Texture text; toggle2 = GUI.Toggle(new Rect(30, 150, 100, 30), toggle2, text); 4、滑动条Slider public float hSliderValue; hSliderValue = GUI.HorizontalSlider(new Rect(30, 200, 100, 30), hSliderValue, 0, 10); public float vSliderVaule; vSliderVaule = GUI.VerticalSlider(new Rect(0, 200, 30, 100), vSliderVaule, 0, 10); 5、工具栏Toolbar private int toolbarInt; private int lastValue; toolbarInt = GUI.Toolbar(new Rect(130, 50, 250, 30), toolbarInt, new string[] { “功能一”, “功能二”, “功能 三” }); if (lastValue != toolbarInt) { if (toolbarInt == 0) Debug.Log(1111); if (toolbarInt == 1) Debug.Log(2222); if (toolbarInt == 2) Debug.Log(3333); lastValue = toolbarInt; } 6、滚动条Scrollbar private float hSbarValue; private float vSbarValue; hSbarValue = GUI.HorizontalScrollbar(new Rect(30, 320, 100, 30), hSbarValue, 1.0f, 0.0f, 10.0f); vSbarValue = GUI.VerticalScrollbar(new Rect(0, 320, 30, 100), vSbarValue, 1.0f, 0.0f, 10.0f); 七、菜单Box GUI.Box(new Rect(30, 50, 300, 100), “Menu”);
八、文本框TextField
private string useNmae=“userName”; private string passWord=“passWord”; private bool isBntLogin; private bool isSuccess; useNmae = GUI.TextField(new Rect(Screen.width / 2, Screen.height / 2 - 200, 150, 30), useNmae); //*****密码字符的掩码字符 passWord = GUI.PasswordField(new Rect(Screen.width / 2, Screen.height / 2 - 150, 150, 30), passWord, ‘*’, 25); if (GUI.Button(new Rect(Screen.width / 2, Screen.height / 2 - 50, 150, 30), “登录”)) { isBntLogin = true; Debug.Log(isBntLogin + “isBntLogin”); if (useNmae.Equals(“admin”) && passWord.Equals(“123”)) { isSuccess = true; GUI.Label(new Rect(500, 350, 150, 30), “登录成功”); } else { isSuccess = false; GUI.Label(new Rect(500, 350, 150, 30), “登录失败”); } } if (isBntLogin) { if (isSuccess) GUI.Label(new Rect(Screen.width / 2, Screen.height / 2 + 50, 150, 30), “登录成功”); else { GUI.Label(new Rect(Screen.width / 2, Screen.height / 2 + 50, 150, 30), “登录失败”); } }
C#进行OnGUI的Label、Button、Toggle、Slider、Toolbar、Scrollbar、TextField的开发
关注
打赏