InteractionToolsManager.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. namespace KairoEngine.GameTools.InteractionTools
  6. {
  7. /// <summary>
  8. /// Manages which interaction tool is currently on.
  9. /// </summary>
  10. public class InteractionToolsManager : MonoBehaviour
  11. {
  12. public static InteractionToolsManager instance;
  13. [ReadOnly] public int currentTools = 0;
  14. public bool showDebug = false;
  15. public List<GameObject> tools = new List<GameObject>();
  16. [HideInInspector] public List<IInteractionTool> toolInterfaces = new List<IInteractionTool>();
  17. private void Awake()
  18. {
  19. if(instance == null) instance = this;
  20. else Destroy(this);
  21. }
  22. private void Start()
  23. {
  24. RegisterInterfaces();
  25. ChangeTool(currentTools);
  26. }
  27. private void RegisterInterfaces()
  28. {
  29. toolInterfaces = new List<IInteractionTool>();
  30. for (int i = 0; i < tools.Count; i++)
  31. {
  32. IInteractionTool toolInterface = tools[i].GetComponent<IInteractionTool>();
  33. if(toolInterface == null) toolInterface = tools[i].GetComponentInChildren<IInteractionTool>();
  34. if(toolInterface != null)
  35. {
  36. if(showDebug) Debug.Log($"Registered tool: {tools[i].name} ({i})");
  37. toolInterfaces.Add(toolInterface);
  38. }
  39. else Debug.LogError($"No Interaction tool interface found in components on \"{tools[i].name}\"", tools[i]);
  40. }
  41. }
  42. public void ChangeTool(int toolIndex)
  43. {
  44. if(toolIndex == currentTools) return;
  45. if(showDebug) Debug.Log($"Changing tool to {tools[toolIndex].name} ({toolIndex})");
  46. currentTools = toolIndex;
  47. for (int i = 0; i < toolInterfaces.Count; i++)
  48. {
  49. if(i != toolIndex) toolInterfaces[i].DisableTool();
  50. else toolInterfaces[i].EnableTool();
  51. }
  52. }
  53. }
  54. }