Unity GUI 擴充在這裡適用Unity 本身的GUI來進行自己的擴充工具的類別。
這邊擴充了ImageButton及Windws與HorizontalSlider三個比較常用的工具,還可以繼續在擴充。
GUIComponents(自訂) class:
using UnityEngine;
using System.Collections;
public class GUIComponents{
//ImageButton OnClick View
bool imageButtonOnClick;
// windwosTitleName and WindwosIdNumber
public string windowsTitle;
public int windowsId;
/**
* ImageButton
* TODO ImageButton function(wordPosistionSize, image, style)
*/
public bool getImageButton(Rect rectScreenRect, Texture image, GUIStyle style){
imageButtonOnClick = GUI.Button (rectScreenRect, image, style);
return imageButtonOnClick;
}
/**
* setAlertDialog
* TODO setAlertDialog Message
*
* @param {string}Title
* @param {string}Message
*/
public void setAlertDialog(string Title){
this.windowsTitle = Title;
windowsId++;
}
/**
* Windows
* TODO Windows function(wordPosistionSize, windwfunctuin, title)
*/
public void getWindwos (int id, Rect screenRect, GUI.WindowFunction windowFunctuin, string title){
title = this.windowsTitle;
id = this.windowsId;
GUI.Window (id, screenRect, windowFunctuin, title);
}
/**
* HorizontalSlider
* TODO HorizontalSlider function(wordPosistionSize, SliderValue, MaxValue, SliderStyle, ThumbStyle)
*/
public float getHorizontalSlider (Rect screenRect, float sliderValue, float sliderMaxValue, GUIStyle slider, GUIStyle thumb){
screenRect.x += screenRect.width;
sliderValue = GUI.HorizontalSlider (screenRect, sliderValue, 0.0f, sliderMaxValue, slider, thumb);
return sliderValue;
}
}
--------------------------------------------------------------------------------
套用腳本:
using UnityEngine;
using System.Collections;
public class SystemSetting : MonoBehaviour{
//ImageButton in a Image and buttonStyle
public Texture image;
public GUIStyle buttonstyle;
/**
* WindowSetting
* OnGUI Windows Setting
* WindowsfunctionSetting can in another class use unction
*/
public void OnGUI(){
DpiResolution dpiResolution = new DpiResolution ();
GUIComponents guiCompoents = new GUIComponents ();
Rect wordPosition = new Rect (dpiResolution.getScreenWidth()/dpiResolution.getScreenWidth(),dpiResolution.getScreenHeight()/dpiResolution.getScreenWidth(),
dpiResolution.getScreenWidth(), dpiResolution.getScreenHeight()/1.2f);
guiCompoents.setAlertDialog ("123");
guiCompoents.getWindwos (guiCompoents.windowsId, wordPosition, WindwosMessage, guiCompoents.windowsTitle);
}
void WindwosMessage(int id){
GUIComponents guiCompents = new GUIComponents ();
DpiResolution dpiResolution = new DpiResolution ();
Rect wordPosition = new Rect (dpiResolution.getScreenWidth()/dpiResolution.getScreenWidth(),dpiResolution.getScreenHeight()/dpiResolution.getScreenWidth(),
image.width/5, image.height/2);
if(guiCompents.getImageButton (wordPosition,image, buttonstyle)){
Debug.Log("ok");
}
}
}
--------------------------------------------------------------------------------
執行結果: