Unity & NGUI でセレクトボックス的なあれ
はじめに
セレクトボックスを作る手順
- [NGUI] > [Create] > [2D UI] でルート要素の作成
- UI Root を選択し、[NGUI] > [Create] > [Sprite] でセレクトボックス用のオブジェクトの作成
- 作成したオブジェクト名を "PopupList" に変更
- PopupList を選択し、 [NGUI] > [Attach] から [Collider], [Button Script], [Popup List Script] をアタッチ
- PopupList を選択し、 [NGUI] > [Create] > [Label] で、子要素にラベルの追加
- PopupList のインスペクターで、UIPopup Script の On Value Change の Notify に、子要素の Label をドラッグアンドドロップし、Method を UILabel.SetCurrentSelection にすると、選択中の文字列が Label に反映される
セレクトボックスの値変更イベント発生時に処理をする
- 以下のスクリプトを任意の GameObject にアタッチし、それを PopupList の UIPopup List の On Value Change の Notify に割り当てる
public class PopupListListener : MonoBehaviour { public void PopupListOnChange () { //UIPopupList.current は変更された UIPopupList オブジェクトを保持している(コールバック関数内でのみ参照可能) Debug.Log ("New value is " + UIPopupList.current.value); } }