第一种用户界面 OnGUI

用OnGUI函数来实现按钮功能

利用按钮对它旋转,缩放,平移

创建平面,创建立方体

创建GUIButton.cs脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GUIButton : MonoBehaviour
{
    float speed = 3f;
    float scale = 1f; 
    float rodatespeed = 30f;

    public Texture btnTexture;//按钮贴图
    GUIContent content;//文本内容

    // Start is called before the first frame update
    void Start()
    {
        content = new GUIContent("旋转立方体", btnTexture);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    private void OnGUI()
    {
        if (GUILayout.Button("移动立方体"))
        { 
            transform.Translate(Vector3.right * speed * Time.deltaTime);
        }
        if (GUI.Button(new Rect(160, 106, 200, 80), content))
        {
            transform.Rotate(Vector3.up * rodatespeed * Time.deltaTime,Space.World);
        }
        if (GUILayout.Button("缩放立方体"))
        {
            scale += scale * speed * Time.deltaTime;
            transform.localScale = new Vector3(scale, scale, scale);
        }
    }
}

效果

第二种用户界面 UGUI

创建按钮

设置为2D

按钮背景图设置

网上随便下载一张图

在Unity中把图片格式设置成精灵Sprite再导入

调整大小

按钮颜色设置

正常

悬浮

按下

松开

修改按钮文本

效果

现在为该按钮写脚本 OnButtonEvents.cs

待会要用到

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OnButtonEvent : MonoBehaviour
{
    public GameObject cube;
    public float speed=30f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

    public void MoveCubeRight()
    {
        cube.transform.position += Vector3.right * speed * Time.deltaTime;
    }
    public void MoveCubeLeft()
    {
        cube.transform.position += Vector3.left * speed * Time.deltaTime;
    }
    public void MoveCubeUp()
    {
        cube.transform.position += Vector3.forward * speed * Time.deltaTime;
    }
    public void MoveCubeDown()
    {
        cube.transform.position += Vector3.back * speed * Time.deltaTime;
    }

    public void Move()
    {
        cube.transform.Translate(Vector3.forward * speed * Time.deltaTime, Space.World);
    }

    public void Rotate()
    {
        cube.transform.Rotate(Vector3.up * speed, Space.World);
    }

    public void Scale()
    {
        cube.transform.localScale = new Vector3(3, 3, 3);
    }
}

创建Rotate.cs脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rotate : MonoBehaviour
{
    public GameObject gameobj;
    public float speed = 300f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

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

    public void rotateObj()
    {
        gameobj.transform.Rotate(Vector3.up * speed * Time.deltaTime, Space.World);
    }
}

把脚本绑定到Cube上并指定对象为Cube

选择按钮

添加对象和鼠标事件

效果

新建一个场景GameUI

下载一张图片作游戏背景

分辨率:700*525

导入并设置图片类型为精灵

选中Image对象

调整大小与图像分辨率一致

导入图像并调整位置

效果

创建文本

字体大小最大27,超过27不显示(为什么?)

因为文本框有点小,可以按如下方式调整文本框

对其方式如下,不然可能看不见

效果

建一个按钮

再建一个测试文本,此文本一直存在,直到点击关于游戏时,将显示一段文本以覆盖此文本

创建一个 GameUI.cs 脚本,绑定到测试文本上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//要用文本text,就要包含这个

public class GameUI : MonoBehaviour
{
    private Text text;//定义一个文本变量
    // Start is called before the first frame update
    void Start()
    {
        text = GetComponent<Text>();//获取text组件
    }

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

    public void ButtonFunc()
    {
        text.text = "北林数媒181";
    }
}

把这个脚本绑定到测试文本上

为按钮添加鼠标事件

若GameUI中没有找到ButtonFunc函数,说明刚刚编辑的脚本没有保存

保存后就有了

效果

点击前

点击后

创建一个勾选工具

修改GameUI代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//要用文本text,就要包含这个

public class GameUI : MonoBehaviour
{
    private Text text;//定义一个文本变量
    // Start is called before the first frame update
    void Start()
    {
        text = GetComponent<Text>();//获取text组件
    }

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

    public void ButtonFunc()
    {
        text.text = "北林数媒181";
    }

    public void ToggleFunc(Toggle toggle)
    {
        text.color = toggle.isOn ? Color.black : Color.cyan;
    }
}

选择Toggle,添加鼠标事件,绑定测试文本

效果

默认勾选

取消勾选后测试文本变色

添加滚动条

修改GameUI代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//要用文本text,就要包含这个
using System;//类型转换

public class GameUI : MonoBehaviour
{
    private Text text;//定义一个文本变量
    int fontsize;//字体大小
    // Start is called before the first frame update
    void Start()
    {
        text = GetComponent<Text>();//获取text组件
        fontsize = text.fontSize;
    }

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

    public void ButtonFunc()
    {
        text.text = "北林数媒181";
    }

    public void ToggleFunc(Toggle toggle)
    {
        text.color = toggle.isOn ? Color.black : Color.cyan;
    }

    public void SliddleFunc(Slider slider)
    {
        float s = slider.value;
        text.fontSize = fontsize + Convert.ToInt32(s);//类型转换
    }
}

为滑动条添加鼠标事件

绑定测试文本,添加刚刚添加的函数

指定Slider,不指定就不会获取值

调整Slider最大值

效果

添加滑动条

修改脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//要用文本text,就要包含这个
using System;//类型转换

public class GameUI : MonoBehaviour
{
    private Text text;//定义一个文本变量
    int fontsize;//字体大小
    Vector3 pos;//字体的位移向量
    // Start is called before the first frame update
    void Start()
    {
        text = GetComponent<Text>();//获取text组件
        fontsize = text.fontSize;
        pos = text.transform.position;//初始化的时候先把字体的位置获取了
    }

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

    public void ButtonFunc()
    {
        text.text = "北林数媒181";
    }

    public void ToggleFunc(Toggle toggle)
    {
        text.color = toggle.isOn ? Color.black : Color.cyan;
    }

    public void SliddleFunc(Slider slider)
    {
        float s = slider.value;
        text.fontSize = fontsize + Convert.ToInt32(s);//类型转换
    }

    public void ScrollbarFunc(Scrollbar scrollbar)
    {
        text.transform.position = new Vector3(pos.x + scrollbar.value * 100f, pos.y, pos.z);//放大100倍,只改变x,其他不变
    }
}

为滑动条添加鼠标事件

效果

添加一个下拉菜单

修改代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//要用文本text,就要包含这个
using System;//类型转换

public class GameUI : MonoBehaviour
{
    private Text text;//定义一个文本变量
    int fontsize;//字体大小
    Vector3 pos;//字体的位移向量
    // Start is called before the first frame update
    void Start()
    {
        text = GetComponent<Text>();//获取text组件
        fontsize = text.fontSize;
        pos = text.transform.position;//初始化的时候先把字体的位置获取了
    }

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

    public void ButtonFunc()
    {
        text.text = "北林数媒181";
    }

    public void ToggleFunc(Toggle toggle)
    {
        text.color = toggle.isOn ? Color.black : Color.cyan;
    }

    public void SliddleFunc(Slider slider)
    {
        float s = slider.value;
        text.fontSize = fontsize + Convert.ToInt32(s);//类型转换
    }

    public void ScrollbarFunc(Scrollbar scrollbar)
    {
        text.transform.position = new Vector3(pos.x + scrollbar.value * 100f, pos.y, pos.z);//放大100倍,只改变x,其他不变
    }

    public void DropdownFunc(Dropdown dropdown)
    {
        switch(dropdown.value)
        {
            case 0:
                text.fontStyle = FontStyle.Bold;
                break;
            case 1:
                text.fontStyle = FontStyle.BoldAndItalic;
                break;
            case 2:
                text.fontStyle = FontStyle.Italic;
                break;
        }
    }
}

添加鼠标事件和对象

效果

选择不同项目时,测试文本的字体格式不同

添加动态文本框

修改脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//要用文本text,就要包含这个
using System;//类型转换

public class GameUI : MonoBehaviour
{
    private Text text;//定义一个文本变量
    int fontsize;//字体大小
    Vector3 pos;//字体的位移向量
    // Start is called before the first frame update
    void Start()
    {
        text = GetComponent<Text>();//获取text组件
        fontsize = text.fontSize;
        pos = text.transform.position;//初始化的时候先把字体的位置获取了
    }

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

    public void ButtonFunc()
    {
        text.text = "北林数媒181";
    }

    public void ToggleFunc(Toggle toggle)
    {
        text.color = toggle.isOn ? Color.black : Color.cyan;
    }

    public void SliddleFunc(Slider slider)
    {
        float s = slider.value;
        text.fontSize = fontsize + Convert.ToInt32(s);//类型转换
    }

    public void ScrollbarFunc(Scrollbar scrollbar)
    {
        text.transform.position = new Vector3(pos.x + scrollbar.value * 100f, pos.y, pos.z);//放大100倍,只改变x,其他不变
    }

    public void DropdownFunc(Dropdown dropdown)
    {
        switch(dropdown.value)
        {
            case 0:
                text.fontStyle = FontStyle.Bold;
                break;
            case 1:
                text.fontStyle = FontStyle.BoldAndItalic;
                break;
            case 2:
                text.fontStyle = FontStyle.Italic;
                break;
        }
    }

    public void InputFieldFuc(InputField inputField)
    {
        text.text = inputField.text;
    }
}

添加鼠标事件和对象

效果

编辑前

结束编辑时文字改变

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐