首页 > 编程语言 > 详细

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

时间:2018-09-14 19:15:23      阅读:204      评论:0      收藏:0      [点我收藏+]

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控件

 

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

原文:https://www.cnblogs.com/cici-seven/p/9648208.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!