LoginSignup
0
0

More than 3 years have passed since last update.

【Unity】アクティブ切り替え用クラス

Posted at

概要

毎回.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値};
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0