概要
毎回.gameObject.SetActive(true)
のように記述するのは面倒だったため切り替え用のクラスを作成しました。
メインクラス
List型オブジェクトに切り替えたい要素を定義しCurrent
のindex値で表示の切り替えを行う。
using UnityEngine;
using System.Collections.Generic;
namespace xxx
{
[ExecuteInEditMode]
public class ViewChanger : MonoBehaviour
{
[SerializeField]
public List<GameObject> viewList;
[SerializeField]
private int current;
public int Current {
get { return this.current; }
set {
this.current = value;
UpdateView();
}
}
protected void UpdateView()
{
for (int i = 0; i < this.viewList.Count; i++) {
GameObject view = this.viewList[i];
if (view == null) {
continue;
}
view.SetActive(this.Current == i);
}
}
}
}
切り替え処理
Controller等からCurrent
にindex値を指定し切り替え
[SerializeField] private ViewChanger sampleViewChanger;
...
this.sampleViewChanger.Current = (int){index値};