2.GUI控件的使用 --《UNITY 3D 游戏开发》笔记

1.Label 控件

编写脚本文件,直接绑定在main camera上

public class labelScript : MonoBehaviour {

    //设定一个值来接收外部赋值的字符串
    public string str;
    //接收外部赋值贴图
    public Texture imageTexture;
    //设定私有变量,只可以在脚本内访问的
    private int imageWidth;
    private int imageHeight;
    private int screenWidth;
    private int screenHeight;

    // Start方法,只执行一次,初始化用
    void Start () {
        
        //得到屏幕的宽高
        screenWidth = Screen.width;
        screenHeight = Screen.height;
        //得到图片的宽高
        imageWidth = imageTexture.width;
        imageHeight = imageTexture.height;

    }
    
    // OnGUI方法绘制页面UI
    void OnGUI () {

        //将文字内容显示在屏幕中
        GUI.Label(new Rect(100, 10, 100, 30), str);
        GUI.Label(new Rect(100, 40, 200, 30), "当前屏幕宽:" + screenWidth);
        GUI.Label(new Rect(100, 80, 100, 30), "当前屏幕高:" + screenHeight);
        //将贴图显示在屏幕中
        GUI.Label(new Rect(100, 120, imageWidth, imageHeight), imageTexture);

    }
}

脚本保存以后,在main camera的属性栏里就可以添加自己定义的两个变量str和贴图啦~

贴图直接从project视图拖到Image Texture栏就可以了~

直接运行游戏就能看到效果啦:

2.Button控件

猜你喜欢

转载自www.cnblogs.com/cici-seven/p/9648208.html