Files
2022-07-06 12:36:49 +03:00

48 lines
1.7 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class Switch : MonoBehaviour
{
List<string> switcheStatesListNames = new List<string>();
//Dictionary<string, GameObject> switcheStatesList = new Dictionary<string, GameObject>();
public Dictionary<string, SwitchState> switcheStatesList = new Dictionary<string, SwitchState>();
bool isStarted = false;
// int posCount;
// int currentPos;
void Start()
{
if (isStarted) return;
// Debug.Log(go.name);
Transform[] switchStates = this.GetComponentsInChildren<Transform>();
// Debug.Log(btnList.Length);
for (int i = 0; i < switchStates.Length; i++)
{
if (i == 0) continue;
// Debug.Log(btnList[i].gameObject.name);
switchStates[i].gameObject.AddComponent<SwitchState>();
switcheStatesListNames.Add(switchStates[i].gameObject.name);
switcheStatesList.Add(switchStates[i].gameObject.name, switchStates[i].gameObject.GetComponent<SwitchState>());
}
switcheStatesListNames.Sort();
if(switcheStatesListNames.Count>0)
switcheStatesList[switcheStatesListNames[0]].switchOn();
isStarted = true;
//setState(1);
}
public void setState(int state)
{
if (!isStarted) Start();
if (state >= 0 && state < switcheStatesListNames.Count)
switcheStatesList[switcheStatesListNames[state]].switchOn();
else
Debug.LogError("Ошибка установки состояния " + state + " объекта " + this.gameObject.name);
}
public void setState(string swName)
{
if (!isStarted) Start();
switcheStatesList[swName].switchOn();
}
}