94 lines
2.3 KiB
C#
94 lines
2.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
[System.Serializable]
|
|
public class RuntimeEventSet
|
|
{
|
|
public string setName;
|
|
public UnityEvent onInvoke;
|
|
}
|
|
|
|
public class RuntimeEventTool : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private List<RuntimeEventSet> eventSets = new List<RuntimeEventSet>();
|
|
|
|
[SerializeField]
|
|
private string activeSetName;
|
|
|
|
/// <summary>
|
|
/// Gets or sets the active set name manually.
|
|
/// Does not invoke the event.
|
|
/// </summary>
|
|
public string ActiveSetName
|
|
{
|
|
get => activeSetName;
|
|
set
|
|
{
|
|
if (eventSets.Exists(e => e.setName == value))
|
|
{
|
|
activeSetName = value;
|
|
Debug.Log($"[RuntimeEventTool] Active set set to: {activeSetName}");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[RuntimeEventTool] Set '{value}' not found.");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Alternative for code use: set active set without property assignment.
|
|
/// </summary>
|
|
public void SetActiveSet(string setName)
|
|
{
|
|
ActiveSetName = setName;
|
|
}
|
|
|
|
public void AddNewSet(string setName)
|
|
{
|
|
if (eventSets.Exists(e => e.setName == setName))
|
|
{
|
|
Debug.LogWarning($"Set '{setName}' already exists.");
|
|
return;
|
|
}
|
|
|
|
var newSet = new RuntimeEventSet
|
|
{
|
|
setName = setName,
|
|
onInvoke = new UnityEvent()
|
|
};
|
|
eventSets.Add(newSet);
|
|
Debug.Log($"[RuntimeEventTool] Added new event set: {setName}");
|
|
}
|
|
|
|
public void InvokeSetByName(string setName)
|
|
{
|
|
var set = eventSets.Find(e => e.setName == setName);
|
|
if (set != null)
|
|
{
|
|
Debug.Log($"[RuntimeEventTool] Invoking set: {setName}");
|
|
set.onInvoke.Invoke();
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[RuntimeEventTool] Set '{setName}' not found.");
|
|
}
|
|
}
|
|
|
|
[ContextMenu("Invoke Active Set")]
|
|
public void InvokeActiveSet()
|
|
{
|
|
if (string.IsNullOrEmpty(activeSetName))
|
|
{
|
|
Debug.LogWarning("[RuntimeEventTool] No active set selected.");
|
|
return;
|
|
}
|
|
|
|
InvokeSetByName(activeSetName);
|
|
}
|
|
|
|
public List<RuntimeEventSet> GetEventSets() => eventSets;
|
|
}
|