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 () {

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将MySQL数据库与Unity3D中的按钮控件结合使用,您需要编写一些代码来实现以下步骤: 1. 建立数据库连接:您需要使用MySQL连接器来建立Unity3D和MySQL数据库之间的连接。您可以在Unity Asset Store中搜索MySQL连接器,并将其导入到您的Unity项目中。 2. 查询数据库:一旦建立了连接,您可以使用SQL查询语句从数据库中检索数据。例如,您可以使用SELECT语句检索按钮的标签和位置。 3. 更新按钮控件:一旦您检索到按钮的标签和位置,您可以使用Unity的按钮控件来更新按钮的标签和位置。例如,您可以使用button.GetComponentInChildren<Text>()方法获取按钮的文本组件,并使用它来设置按钮的标签。 以下是一个简单的示例代码,演示如何将MySQL数据库与Unity3D中的按钮结合使用: ```c# using UnityEngine; using System.Collections; using System.Data; using MySql.Data.MySqlClient; public class ButtonController : MonoBehaviour { public GameObject buttonPrefab; public Transform buttonContainer; private MySqlConnection connection; private MySqlCommand command; private MySqlDataReader reader; void Start () { // Connect to the database string connectionString = "Server=localhost;Database=mydatabase;Uid=root;Pwd=;"; connection = new MySqlConnection(connectionString); connection.Open(); // Query the database for button data string query = "SELECT * FROM buttons"; command = new MySqlCommand(query, connection); reader = command.ExecuteReader(); // Create buttons based on the data retrieved from the database while (reader.Read()) { // Get the button label and position from the database string label = reader.GetString("label"); Vector3 position = new Vector3(reader.GetFloat("x"), reader.GetFloat("y"), reader.GetFloat("z")); // Create a new button and set its label and position GameObject button = Instantiate(buttonPrefab, position, Quaternion.identity) as GameObject; button.transform.SetParent(buttonContainer); button.GetComponentInChildren<Text>().text = label; } // Close the database connection connection.Close(); } } ``` 请注意,这只是一个示例代码,您需要根据您的具体需求进行修改,并确保您的MySQL数据库已正确设置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值