GUI関連、デバッグ表示など
メモ
private変数はunityツールのデバックモードを使えばインスペクタ上で見る事ができるので実は以下のコードはあんまり必要ない
(ユニティツールのインスペクタパネル右上のアイコンからオンにできる)
実機で値が見たい場合などに利用できる程度
サンプルコード
/*
* GUIのテストサンプルコード */
using UnityEngine;
using System.Collections;
public class LevelGuiScript : MonoBehaviour {
public GUISkin customSkin; public float value=100; //private ArrayList myAL= new ArrayList(); 等を使う手もある //今回はハッシュテーブルを使った private Hashtable myHT; // Use this for initialization void Start () { myHT = new Hashtable(); DebugLabel("X=","256",256,256); DebugLabel("Y=","512",512,512); DebugLabel("Y=","1024",512,512); } // Update is called once per frame void Update () { } //unityではOnGUI()をオーバーライドしてGUIを表示する void OnGUI(){ //標準的なやりかた GUI.skin = customSkin; GUILayout.Button("button"); GUILayout.Button(value.ToString()); GUILayout.Label("test"); if(GUI.Button(new Rect(100,100,300,150),"I am Button")){ print("Click!"); } //ハッシュテーブル内の要素をすべて表示する if(myHT.Count>0){ foreach (DictionaryEntry item in myHT ){ CustomLabel customlabel = (CustomLabel)item.Value; GUI.Label(new Rect(customlabel.X,customlabel.Y,256,256),item.Key + customlabel.Text); } } } //重複するキーは上書きしてハッシュする public void DebugLabel(string key,string text,int x,int y){ //myAL.Add(new CustomLabel(){Text=text,X=x,Y=y,}); if(myHT.Contains(key)){ myHT.Remove(key); } myHT.Add(key,new CustomLabel(){Key=key,Text=text,X=x,Y=y,}); } //デバッグ表示用に作ったカスタムラベル private class CustomLabel { public string Key { get; set; } public string Text { get; set; } public int X { get; set; } public int Y { get; set; } }
}
メニュー
- 最終更新:2014-06-13 12:11:33