using UnityEngine; using UnityEngine.Events; using System.Collections.Generic; namespace Lean.Common { /// This component allows you to randomly invoke one of the specified events when you manually call the Invoke method. [HelpURL(LeanHelper.PlusHelpUrlPrefix + "LeanRandomEvents")] [AddComponentMenu(LeanHelper.ComponentPathPrefix + "Random Events")] public class LeanRandomEvents : MonoBehaviour { public List Events { get { if (events == null) events = new List(); return events; } } [SerializeField] private List events; [ContextMenu("Invoke")] public void Invoke() { if (events != null && events.Count > 0) { var index = Random.Range(0, events.Count); var element = events[index]; if (element != null) { element.Invoke(); } } } } } #if UNITY_EDITOR namespace Lean.Common.Editor { using TARGET = LeanRandomEvents; [UnityEditor.CanEditMultipleObjects] [UnityEditor.CustomEditor(typeof(TARGET))] public class LeanRandomEvents_Editor : LeanEditor { protected override void OnInspector() { TARGET tgt; TARGET[] tgts; GetTargets(out tgt, out tgts); Draw("events"); } } } #endif