99re热这里只有精品视频,7777色鬼xxxx欧美色妇,国产成人精品一区二三区在线观看,内射爽无广熟女亚洲,精品人妻av一区二区三区

OnGUI Button 控件

2020-07-13 14:03 更新

在 Unity 3D 開(kāi)發(fā)中 Button 控件是游戲開(kāi)發(fā)中最常使用的控件之一,用戶(hù)常常通過(guò) Button 控件來(lái)確定其選擇行為,當(dāng)用戶(hù)單擊 Button 控件時(shí),Button 控件會(huì)顯示按下的效果,并觸發(fā)與該控件關(guān)聯(lián)的游戲功能。

在游戲中通常用作游戲界面、游戲功能、游戲設(shè)置的開(kāi)關(guān)。

一般來(lái)說(shuō),按鈕分兩種:

  • 普通按鈕。

  • 圖片按鈕。

普通按鈕

普通按鈕是系統(tǒng)默認(rèn)顯示的按鈕,Unity 3D 的普通按鈕背景呈半透明狀態(tài),顯示白色文字。

普通按鈕的使用方法如下:

public static function Button(position:Rect, text:string):bool;
public static function Button(position:Rect, image:Texture):bool;
public static function Button(position:Rect, content:GUIContent):bool;
public static function Button(position:Rect, text:string, style:GUIStyle):bool;
public static function Button(position:Rect, image:Texture, style:GUIStyle):bool;
public static function Button(position:Rect, content:GUIContent, style:GUIStyle):bool;

注:

  • position 指按鈕在屏幕上的位置以及長(zhǎng)寬值。

  • text 指按鈕上顯示的文本。

Button 控件的參數(shù)如下表所示。

參數(shù) 描述
position 設(shè)置控件在屏幕上的位置及大小。
image 設(shè)置控件上顯示的紋理圖片。
style 設(shè)置控件使用的樣式。
text 設(shè)置控件上顯示的文本。
content 設(shè)置控件的文本、圖片和提示。

使用案例

  1. 啟動(dòng) Unity 3D 創(chuàng)建新項(xiàng)目。

  1. 執(zhí)行 FileSave Scene 命令,并保存場(chǎng)景。

  1. 創(chuàng)建 JavaScript 腳本。

  1. 打開(kāi)腳本編輯器,輸入腳本語(yǔ)句,然后保存。

    function OnGUI(){
        if(GUI.Button(Rect(0, 0, 100, 50), "click here")){
            print("you have click here!");
        }
    }

  1. 將腳本與主攝像機(jī)相連。

將腳本拖到 Hierarchy 視圖中的 Main Camera 對(duì)象中產(chǎn)生關(guān)聯(lián)。

  1. 測(cè)試腳本。

單擊 Unity 3D 工具欄上的運(yùn)行按鈕對(duì)腳本進(jìn)行測(cè)試,如下圖所示,在 Game 視圖中出現(xiàn)了一個(gè)按鈕,按鈕上顯示 "click here",單擊該按鈕,在 Unity 3D 主界面底部的狀態(tài)欄上輸出 "You have click here"。

圖片按鈕

Button 控件除了可以顯示文字以外,還可以顯示貼圖。

貼圖是一種美化按鈕的方法,開(kāi)發(fā)者可以設(shè)定按鈕的背景圖片,比如水晶按鈕、卡通按鈕等。

在 Unity 3D 中實(shí)現(xiàn) Button 貼圖十分容易,只要將圖片作為一個(gè)參數(shù)傳遞到 Button 函數(shù)中即可。

Button 貼圖方法如下:

public static function Button(position:Rect, image:Texture):bool;
public static function Button(position:Rect, image:Texture, style:GUIStyle):bool;

其中 Position 定義了按鈕在屏幕上的位置及長(zhǎng)寬值,image 為按鈕上顯示的圖片。

使用案例

  1. 啟動(dòng) Unity 3D 創(chuàng)建新項(xiàng)目,將其命名為 Button。

  1. 在菜單中執(zhí)行 FileSave Scene 命令,保存當(dāng)前場(chǎng)景,命名為 scene,即在 Unity 3D 中創(chuàng)建了一個(gè)游戲場(chǎng)景。

  1. 單擊 Project 視圖中 create 右側(cè)的下拉三角形,選擇 JavaScript ,創(chuàng)建 JavaScript 腳本。

  1. 在 Project 視圖中雙擊該腳本文件,打開(kāi)腳本編輯器,輸入下列腳本語(yǔ)句:

    var btnTexture:Texture;
    var atnTexture:Texture;
    function OnGUI(){
        if(!btnTexture){
            Debug.LogError("Please assign a texture on the inspector");
            return;
        }
        if(!atnTexture){
            Debug.LogError("Please assign a texture on the inspector");
            return;
        }
        if(GUI.Button(Rect(Screen.width/2-50, Screen.height/2+130, 70, 70),atnTexture)){
            Application.LoadLevel("play");
        }
        if(GUI.Button(Rect(Screen.width/2+30, Screen.height/2+130, 70, 70),btnTexture)){
            Application.LoadLevel("exit");
        }
    }

  1. 保存腳本(Ctrl+S 鍵)。

  1. 將腳本與主攝像機(jī)相連。

  1. 單擊主攝像機(jī),在 Inspector 屬性面板中添加紋理圖片。

  1. 單擊 play 按鈕測(cè)試效果,可以看見(jiàn)按鈕已經(jīng)換成了二維卡通圖片的形式,如下圖所示。

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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)