Last active
December 5, 2019 15:47
-
-
Save capnslipp/50db1dc724514bb3db91 to your computer and use it in GitHub Desktop.
SingleEventAction, KeyedEventActions, & Friends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// @creator: Slipp Douglas Thompson | |
/// @license: Public Domain per The Unlicense. See <http://unlicense.org/>. | |
/// @purpose: Forwards keyed events to a KeyedEventActions component elsewhere. | |
/// @why: Because this functionality should be built-into Unity. | |
/// @usage: Needs to be put on the same GameObject as the Mecanim Animator component or “legacy” Animation component. | |
/// Animation events should be configured in the Unity Editor's Animation timeline view or model import settings › Animations tab › Events disclosure (or via the scripting API, I suppose) to call “Function: Action, Parameter String: «your-decided-key-string-for-this-action»”. | |
/// @intended project path: Assets/Utils/EventAction/KeyedEventActionForwarder.cs | |
/// @interwebsouce: https://gist.github.com/capnslipp/50db1dc724514bb3db91 | |
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.Events; | |
[AddComponentMenu("Utils/EventAction/Keyed Event Action Forwarder")] | |
public class KeyedEventActionForwarder : MonoBehaviour | |
{ | |
[Serializable] public struct TargetEntry | |
{ | |
public KeyedEventActions target; | |
public string[] keyStrings; | |
} | |
public List<TargetEntry> targets; | |
protected void OnValidate() | |
{ | |
List<TargetEntry> targets = this.targets; | |
if (targets.Count == 0) | |
targets.Add(new TargetEntry()); | |
} | |
public void Action(string key) | |
{ | |
if (!this.enabled) { | |
Debug.LogWarning(typeof(KeyedEventActionForwarder)+" received Action() but is disabled. Ignoring.", this); | |
return; | |
} | |
if (string.IsNullOrEmpty(key)) { | |
Debug.LogWarning(typeof(KeyedEventActionForwarder)+" received Action() with null or 0-length key. Skipping.", this); | |
return; | |
} | |
foreach (TargetEntry entry in this.targets) | |
{ | |
bool hasKey = ((ICollection<string>)entry.keyStrings).Contains(key); | |
if (hasKey) | |
entry.target.Action(key); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// @creator: Slipp Douglas Thompson | |
/// @license: Public Domain per The Unlicense. See <http://unlicense.org/>. | |
/// @purpose: Responds to animation events in the form of `Action("key-string")` with configurable `UnityEvent` action lists, per key string. | |
/// @why: Because this functionality should be built-into Unity. | |
/// @usage: Should be put on the same GameObject as the Mecanim Animator component or “legacy” Animation component, unless it's being referenced by a KeyedEventActionForwarder (in which case it can be put anywhere). | |
/// Animation events should be configured in the Unity Editor's Animation timeline view or model import settings › Animations tab › Events disclosure (or via the scripting API, I suppose) to call “Function: Action, Parameter String: «your-decided-key-string-for-this-action»”. | |
/// @intended project path: Assets/Utils/EventAction/KeyedEventActions.cs | |
/// @interwebsouce: https://gist.github.com/capnslipp/50db1dc724514bb3db91 | |
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.Events; | |
[AddComponentMenu("Utils/EventAction/Keyed Event Actions")] | |
public class KeyedEventActions : MonoBehaviour | |
{ | |
[Serializable] public struct ActionEntry | |
{ | |
public string keyString; | |
public UnityEvent action; | |
[Serializable] public class ActivateFlags | |
{ | |
public bool onEnable = false; | |
} | |
public ActivateFlags activate; | |
} | |
public List<ActionEntry> actions; | |
protected void OnEnable() | |
{ | |
foreach (ActionEntry entry in this.actions) { | |
if (entry.activate.onEnable) | |
entry.action.Invoke(); | |
} | |
} | |
protected void OnValidate() | |
{ | |
List<ActionEntry> actions = this.actions; | |
if (actions.Count == 0) | |
actions.Add(new ActionEntry()); | |
} | |
public void Action(string key) | |
{ | |
if (!this.enabled) { | |
Debug.LogWarning(typeof(KeyedEventActions)+" received Action() but is disabled. Ignoring.", this); | |
return; | |
} | |
if (string.IsNullOrEmpty(key)) { | |
Debug.LogWarning(typeof(KeyedEventActions)+" received Action() with null or 0-length key. Skipping.", this); | |
return; | |
} | |
foreach (ActionEntry entry in this.actions) | |
{ | |
if (entry.keyString == key) | |
entry.action.Invoke(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// @creator: Slipp Douglas Thompson | |
/// @license: Public Domain per The Unlicense. See <http://unlicense.org/>. | |
/// @purpose: Responds to an animation event in the form of `Action()` with a configurable `UnityEvent` action list. | |
/// @why: Because this functionality should be built-into Unity. | |
/// @usage: Needs to be put on the same GameObject as the Mecanim Animator component or “legacy” Animation component. | |
/// Animation events should be configured in the Unity Editor's Animation timeline view or model import settings › Animations tab › Events disclosure (or via the scripting API, I suppose) to call “Function: Action”. | |
/// @intended project path: Assets/Utils/EventAction/SingleEventAction.cs | |
/// @interwebsouce: https://gist.github.com/capnslipp/50db1dc724514bb3db91 | |
using System; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.Events; | |
[AddComponentMenu("Utils/EventAction/Single Event Action")] | |
public class SingleEventAction : MonoBehaviour | |
{ | |
public UnityEvent action; | |
[Serializable] public class ActivateFlags | |
{ | |
public bool onEnable = false; | |
public bool whenToldTo = true; | |
} | |
public ActivateFlags activate; | |
protected void OnEnable() | |
{ | |
if (this.activate.onEnable) | |
this.action.Invoke(); | |
} | |
public void Action() | |
{ | |
if (!this.activate.whenToldTo) { | |
Debug.LogWarning(typeof(SingleEventAction)+" received Action() but activate.whenToldTo is disabled. Skipping.", this); | |
return; | |
} | |
if (!this.enabled) { | |
Debug.LogWarning(typeof(SingleEventAction)+" received Action() but is disabled. Ignoring.", this); | |
return; | |
} | |
this.action.Invoke(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment