【Unity】8.4 擴充UnityGUI

來源:互聯網
上載者:User

標籤:

分類:Unity、C#、VS2015

建立日期:2016-04-27 一、簡介

有很多種方法可以補充和擴充 UnityGUI 以滿足您的需求。你可以混合和建立控制項,並且可以有多種方法來規定使用者 GUI 輸入的處理方法。 二、複合控制項

GUI 中可能會出現各種情況,但總是同時出現兩種類型的控制項。例如,你可能會使用水平滑塊建立通過角色控制的螢幕(Character Creation)。此時,所有這些滑塊都需要提供一個標籤 (Label) 來識別,這樣玩家才能知道它們是調節什麼的。這種情況下,可以將 GUI.Label() 的每個調用與 GUI.HorizontalSlider() 的調用配對,或者建立一個同時包含標籤和滑塊的複合控制項 (Compound Control) 。

例如:

using UnityEngine;using System.Collections;public class GUITest : MonoBehaviour {    private float mySlider = 1.0f;    void OnGUI () {        mySlider = LabelSlider (new Rect (10, 100, 100, 20), mySlider, 5.0f, "Label text here");    }    float LabelSlider (Rect screenRect, float sliderValue, float sliderMaxValue, string labelText) {        GUI.Label (screenRect, labelText);        // <- 將滑塊推至標籤末端        screenRect.x += screenRect.width;         sliderValue = GUI.HorizontalSlider (screenRect, sliderValue, 0.0f, sliderMaxValue);        return sliderValue;    }}

效果如下:

在這個樣本中,調用 LabelSlider() 並傳遞正確的參數將使標籤 (Label) 與水平滑動條 (Horizontal Slider) 配對。編寫複合控制項 (Compound Controls) 時,請務必記住在函數結束時返回正確的值以使其互動。三、靜態複合控制項

使用靜態 (Static) 函數,你可以為自己建立完備的複合控制項集。這樣你就不需要在同一指令碼中使用函數時聲明此函數。

using UnityEngine;using System.Collections;public class CompoundControls : MonoBehaviour{public static float LabelSlider (Rect screenRect, float sliderValue, float sliderMaxValue, string labelText){        GUI.Label (screenRect, labelText);        //將滑塊推至標籤末端        screenRect.x += screenRect.width;         sliderValue = GUI.HorizontalSlider (screenRect, sliderValue, 0.0f, sliderMaxValue);        return sliderValue;    }}

通過將上述樣本儲存至 CompoundControls 指令碼,只需簡單地鍵入 CompoundControls.LabelSlider() 並提供變數,就可以從其他任何指令碼中調用 LabelSlider()。四、複雜的複合控制項

你還可以將複合控制項 (Compound Controls) 變得更具創造力。即讓它們按照你喜歡的方式排列和分組。下面的C#程式碼範例建立一個能夠重複使用的 RGB 滑塊:

using UnityEngine;using System.Collections;public class GUITest : MonoBehaviour {    public Color myColor;    void OnGUI () {        myColor = RGBSlider (new Rect (10,10,200,10), myColor);    }    Color RGBSlider (Rect screenRect, Color rgb) {        rgb.r = GUI.HorizontalSlider (screenRect, rgb.r, 0.0f, 1.0f);        // <- 將下個控制項向下移動一點以避免重疊        screenRect.y += 20;         rgb.g = GUI.HorizontalSlider (screenRect, rgb.g, 0.0f, 1.0f);        // <- 將下個控制項向下移動一點以避免重疊        screenRect.y += 20;         rgb.b = GUI.HorizontalSlider (screenRect, rgb.b, 0.0f, 1.0f);        return rgb;    }}

效果如下:

為了展示複合控制項如何在其他複合控制項中使用,現在讓我們在它們各自上方建立複合控制項。為此,我們將像上面那樣建立一個新的 RGB 滑塊,但使用“標籤滑塊”(LabelSlider) 來實現。這樣就總有一個標籤使我們可以分辨出哪個滑塊對應哪種顏色:

using UnityEngine;using System.Collections;public class GUITest : MonoBehaviour {    public Color myColor;    void OnGUI () {        myColor = RGBSlider (new Rect (10,10,200,30), myColor);    }    Color RGBSlider (Rect screenRect, Color rgb) {        rgb.r =  CompoundControls.LabelSlider (screenRect, rgb.r, 1.0f, "Red");        // 將下個控制項向下移動一點以避免重疊        screenRect.y += 20;         rgb.g = CompoundControls.LabelSlider (screenRect, rgb.g, 1.0f, "Green");        // 將下個控制項向下移動一點以避免重疊        screenRect.y += 20;         rgb.b = CompoundControls.LabelSlider (screenRect, rgb.b, 1.0f, "Blue");        return rgb;    }    }

這段代碼的效果如下:

【Unity】8.4 擴充UnityGUI

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.