72 lines
1.9 KiB
C#
72 lines
1.9 KiB
C#
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);
|
|
LoadInteractables();
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
|
|
public Interactable GetInteractableByName(string name)
|
|
{
|
|
foreach (var interactable in interactables)
|
|
if (interactable.name == name)
|
|
return Instantiate(interactable);
|
|
|
|
return null;
|
|
}
|
|
|
|
public Interactable GetInteractableById(int id)
|
|
{
|
|
foreach (var interactable in interactables)
|
|
if (interactable.id == id)
|
|
return Instantiate(interactable);
|
|
|
|
return null;
|
|
}
|
|
|
|
public Interactable GetRandomInteractable()
|
|
{
|
|
if (interactables.Length == 0)
|
|
{
|
|
Debug.LogWarning("No interactable found");
|
|
return null;
|
|
}
|
|
|
|
return Instantiate(interactables[Random.Range(0, interactables.Length - 1)]);
|
|
}
|
|
|
|
public Interactable GetRandomInteractableOfType(Interactable.InteractableType type)
|
|
{
|
|
if (interactables.Length == 0)
|
|
{
|
|
Debug.LogWarning("No interactable found");
|
|
return null;
|
|
}
|
|
|
|
var interactablesOfType = interactables.Where(i => i.type == type).ToArray();
|
|
return Instantiate(interactablesOfType[Random.Range(0, interactablesOfType.Length - 1)]);
|
|
}
|
|
|
|
public void LoadInteractables()
|
|
{
|
|
Debug.Log("Loading interactables from Resources/Interactables");
|
|
interactables = Resources.LoadAll<Interactable>("Interactables");
|
|
|
|
foreach (var interactable in interactables) Debug.Log("Loaded interactable: " + interactable.name);
|
|
}
|
|
} |