123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- 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 bool showDebug = false;
- public List<GameObject> tools = new List<GameObject>();
- [HideInInspector] public 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)
- {
- if(showDebug) Debug.Log($"Registered tool: {tools[i].name} ({i})");
- toolInterfaces.Add(toolInterface);
- }
- else Debug.LogError($"No Interaction tool interface found in components on \"{tools[i].name}\"", tools[i]);
- }
- }
- public void ChangeTool(int toolIndex)
- {
- if(toolIndex == currentTools) return;
- if(showDebug) Debug.Log($"Changing tool to {tools[toolIndex].name} ({toolIndex})");
- currentTools = toolIndex;
- for (int i = 0; i < toolInterfaces.Count; i++)
- {
- if(i != toolIndex) toolInterfaces[i].DisableTool();
- else toolInterfaces[i].EnableTool();
- }
- }
- }
- }
|