效果图:Unity GUI获取玩家名字并在控制台输出

我们需要使用3个函数

//1 GUILayout.Label(string str) :显示标签
//2 GUILayout.Button(string str): 显示按钮,返回值为bool,表示是否被按下
//3 GUILayout.TextField(string str) :显示输入文本区域,返回用户更改后的文本

具体实现如下

using UnityEngine;
using System.Collections;

public class C_3_3 : MonoBehaviour {
    string text="";
    string myName="";
    void OnGUI()
    {
        //用标签显示文本
        GUILayout.Label("请输入你的名字:");
        //用文本区域输入名字
        text = GUILayout.TextField(text);
        //
        if(GUILayout.Button("提交"))
        {
            myName = text;
        }
        //当myName不为空的时候,说明我们已经提交了名字,则显示名字
        if( !string.IsNullOrEmpty(myName))
        {
            GUILayout.Label("提交成功,名字:"+myName);
        }

}

 

相关文章:

  • 2022-12-23
  • 2022-03-09
  • 2022-12-23
  • 2021-11-29
  • 2021-10-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-08
  • 2021-05-19
  • 2021-12-02
  • 2021-06-14
  • 2021-10-24
  • 2022-12-23
相关资源
相似解决方案