我只想問什麼是在Unity上創建和顯示視圖的正確方法。我只是一個初學者,最近剛剛觀看了關於如何編寫腳本和了解如何創建GUI的教程,其中包括按鈕,編輯文本框和其他視圖,但不確定應該如何真正以其優化方式實現它並不會影響應用程序的性能。我到目前爲止所嘗試的是創建一個editText,其中我將其設置在MainCamera上進行顯示,這是我的目標是可重用的,以便在需要在應用程序中添加更多editText時,我可以拖放腳本。這裏是代碼到目前爲止:Unity添加顯示視圖的正確方法
using UnityEngine;
using System.Collections;
public class EditText : MonoBehaviour {
public int width = 150;
public int height = 25;
public int x_position = 0;
public int y_position = 0;
public string text = "Hello World";
public Position position = Position.unset;
public int text_size = 15;
public enum Position{unset,center_screen, center_vertical, center_horizontal};
void OnGUI() {
setPosition();
GUI.skin.textArea.fontSize = text_size;
GUI.color = Color.red;
}
private void setPosition(){
switch (position) {
case Position.center_screen:
GUI.TextArea (new Rect (Screen.width/2 - width/2, Screen.height/2 - height/2, width, height), text);
GUI.backgroundColor = Color.white;
break;
case Position.center_vertical:
GUI.TextArea (new Rect (x_position, Screen.height/2 - height/2, width, height), text);
GUI.backgroundColor = Color.white;
break;
case Position.center_horizontal:
GUI.TextArea (new Rect (Screen.width/2 - width/2, y_position, width, height), text);
GUI.backgroundColor = Color.white;
break;
default:
GUI.TextArea (new Rect (x_position, y_position, width, height), text);
GUI.backgroundColor = Color.white;
break;
}
}
}
問題是我做這個權利,或者我應該停止並使用其他實施?因爲我真的希望它像Android視圖一樣可重用。如果還有其他庫或腳本可以用來實現這一點,那麼我不會爲此重新發明輪子?
在旁邊注意我也搜查了NGUI,雖然我不確定這是否符合我的需要。因爲當我嘗試創建UILabel時,組件只有幾個選項,我不確定如何使用它。
謝謝@David,這裏只是另一個問題,因爲你說我的解決方案很簡單而且很有效。這個解決方案是否也有效?因爲在幻想方面我想我會堅持單位。 :) – KaHeL
我認爲這是目前最好的,除非Unity發佈新的GUI系統。繼續前進,不要害怕! :) – David
哈哈!以及我不熟悉GUILayout,因爲我只是一個初學者,但我會檢查一下,希望進一步提高我的技能。 :) – KaHeL