1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Sirenix.OdinInspector;
- namespace KairoEngine.GameTools.InteractionTools
- {
- /// <summary>
- /// Manages which interaction tool is currently on.
- /// </summary>
- public class InteractionToolsManager : MonoBehaviour
- {
- public static InteractionToolsManager instance;
- [ReadOnly] public int currentTools = 0;
- public List<GameObject> tools = new List<GameObject>();
- private List<IInteractionTool> toolInterfaces = new List<IInteractionTool>();
- private void Awake()
- {
- if(instance == null) instance = this;
- else Destroy(this);
- }
- private void Start()
- {
- RegisterInterfaces();
- ChangeTool(currentTools);
- }
- private void RegisterInterfaces()
- {
- toolInterfaces = new List<IInteractionTool>();
- for (int i = 0; i < tools.Count; i++)
- {
- IInteractionTool toolInterface = tools[i].GetComponent<IInteractionTool>();
- if(toolInterface == null) toolInterface = tools[i].GetComponentInChildren<IInteractionTool>();
- if(toolInterface != null) toolInterfaces.Add(toolInterface);
- else Debug.LogError($"No Interaction tool interface found in components on \"{tools[i].name}\"", tools[i]);
- }
- }
- public void ChangeTool(int toolIndex)
- {
- currentTools = toolIndex;
- for (int i = 0; i < toolInterfaces.Count; i++)
- {
- if(i != toolIndex) toolInterfaces[i].DisableTool();
- else toolInterfaces[i].EnableTool();
- }
- }
- }
- }
|