OnGUI Window控件

2020-07-13 14:28 更新

通常情況下,一個游戲界面可以由很多窗口組成,在每個窗口中可以添加不同的、任意的功能組件,這讓窗口的使用豐富了游戲界面和內(nèi)容。

使用 Window 控件為當(dāng)前界面添加窗口,具體方法如下:

public static function Window(id:int, clientRect:Rect, func:GUI.WindowFunction,text:string):Rect;
public static function Window(id:int, clientRect:Rect, func:GUI.WindowFunction,image:Texture):Rect;
public static function Window(id:int, clientRect:Rect, func:GUI.WindowFunction,content:GUIContent):Rect;
public static function Window(id:int, clientRect:Rect, func:GUI.WindowFunction,text:string, style:GUIStyle):Rect;
public static function Window(id:int, clientRect:Rect, func:GUI.WindowFunction,image:Texture, style:GUIStyle):Rect;
public static function Window(id:int, clientRect:Rect, func:GUI.WindowFunction,title:GUIContent, style:GUIStyle):Rect;

注:

  • id 為窗口的標(biāo)號,用以標(biāo)識窗口。

  • clientRect 為窗口顯示區(qū)域。

  • func 是回調(diào)方法的名稱。

  • text 為窗口標(biāo)題。

參數(shù)列表

參數(shù) 描述
Style 設(shè)置用于窗口的可選樣式。如果遺漏了,則使用當(dāng)前GUISkin的窗口樣式。
clientRect 設(shè)置可以拖動的窗口的一部分,這部分將被剪切到實際的窗口中。
text 設(shè)置文本在窗口內(nèi)呈現(xiàn)。
content 設(shè)置在窗口內(nèi)渲染的圖形。
title 設(shè)置文本在窗口標(biāo)題欄顯示。
id 設(shè)置窗口的ID號(可以是任何值, 只要它是唯一的)。
func 設(shè)置顯示窗口內(nèi)容的腳本函數(shù)。
image 設(shè)置在窗口中渲染的圖像。
style 設(shè)置窗口的樣式信息。

使用案例

  1. 創(chuàng)建項目,將其命名為 window,保存場景。

  1. 執(zhí)行 AssetsCreateJavaScript 命令,創(chuàng)建一個新的腳本文件。

  1. 在 Project 視圖中打開腳本編輯器,輸入下列語句:

    var windowRect0:Rect=Rect(20, 20, 120, 50);
    var windowRect1:Rect=Rect(20, 100, 120, 50);
    function OnGUI(){
        GUI.color=Color.red;
        windowRect0=GUI.Window(0, windowRect0, DoMyWindow, "Red Window");
        GUI.color=Color.green;
        windowRect1=GUI.Window(1, windowRect1, DoMyWindow, "Green Window");
    }
    function DoMyWindow(windowID:int){
        if(GUI.Button(Rect(10, 20, 100, 20), "Hello World"))
        print("Got a click in window with color"+GUI.color);
        GUI.DragWindow(Rect(0, 0, 10000, 10000));
    }

  1. Ctrl+S 鍵保存腳本。

  1. 在 Project 視圖中選擇腳本,并將其拖曳到 Hierarchy 視圖中的 Main Camera 上,使腳本和攝像機(jī)產(chǎn)生關(guān)聯(lián)。

  1. 進(jìn)行測試,效果如下圖所示。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號