using System.Linq; using UnityEngine; public class InteractableManager : MonoBehaviour { public Interactable[] interactables; public static InteractableManager Instance { get; private set; } private void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); LoadInteractable(); } else { Destroy(gameObject); } } public ScriptableObject GetInteractableByName(string name) { foreach (var interactable in interactables) if (interactable.objectName == name) return Instantiate((ScriptableObject)interactable); return null; } public ScriptableObject GetInteractableById(int id) { foreach (var interactable in interactables) if (interactable.id == id) return Instantiate((ScriptableObject)interactable); return null; } public ScriptableObject GetRandomInteractable() { if (interactables.Length == 0) { Debug.LogWarning("No interactable found"); return null; } return Instantiate((ScriptableObject)interactables[Random.Range(0, interactables.Length - 1)]); } public CropInteractable GetRandomCropInteractable() { if (interactables.Length == 0) { Debug.LogWarning("No interactable found"); return null; } var cropInteractables = interactables.OfType().ToArray(); return Instantiate(cropInteractables[Random.Range(0, cropInteractables.Length - 1)]); } public ScriptableObject GetRandomInteractableOfType(InteractableType type) { if (interactables.Length == 0) { Debug.LogWarning("No interactable found"); return null; } var interactablesOfType = interactables.Where(i => i.type == type).ToArray(); return Instantiate((ScriptableObject)interactablesOfType[Random.Range(0, interactablesOfType.Length - 1)]); } public void LoadInteractable() { Debug.Log("Loading interactable from Resources/Interactable"); interactables = Resources.LoadAll("Interactables"); // interactables = interactables.Concat(Resources.LoadAll("Interactables")).ToArray(); // interactables = interactables.Concat(Resources.LoadAll("Interactables")).ToArray(); // interactables = interactables.Concat(Resources.LoadAll("Interactables")).ToArray(); foreach (var interactable in interactables) Debug.Log("Loaded interactable: " + interactable.id); } }