2.GUI控件的使用 --《UNITY 3D 遊戲開發》筆記
阿新 • • 發佈:2018-09-14
div info art color text tar scrip 寬高 執行
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 遊戲開發》筆記