| | | 1 | | using System; |
| | | 2 | | using UnityEngine; |
| | | 3 | | |
| | | 4 | | namespace Hertzole.ScriptableValues |
| | | 5 | | { |
| | | 6 | | public abstract class ScriptableListenerBase : MonoBehaviour |
| | | 7 | | { |
| | | 8 | | [SerializeField] |
| | | 9 | | [EditorTooltip("When listeners should start listening.")] |
| | 10638 | 10 | | private StartListenEvents startListening = StartListenEvents.Awake; |
| | | 11 | | [SerializeField] |
| | | 12 | | [EditorTooltip("When listeners should stop listening.")] |
| | 10638 | 13 | | private StopListenEvents stopListening = StopListenEvents.OnDestroy; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Is the listener currently listening to the target value? |
| | | 17 | | /// </summary> |
| | 80394 | 18 | | public bool IsListening { get; private set; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// When listeners should start listening. |
| | | 22 | | /// </summary> |
| | | 23 | | public StartListenEvents StartListening |
| | | 24 | | { |
| | 2862 | 25 | | get { return startListening; } |
| | 2862 | 26 | | set { startListening = value; } |
| | | 27 | | } |
| | | 28 | | /// <summary> |
| | | 29 | | /// When listeners should stop listening. |
| | | 30 | | /// </summary> |
| | | 31 | | public StopListenEvents StopListening |
| | | 32 | | { |
| | 1872 | 33 | | get { return stopListening; } |
| | 2862 | 34 | | set { stopListening = value; } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | protected virtual void Awake() |
| | 10638 | 38 | | { |
| | 10638 | 39 | | IsListening = false; |
| | | 40 | | |
| | 10638 | 41 | | if (startListening == StartListenEvents.Awake) |
| | 10002 | 42 | | { |
| | 10002 | 43 | | SetListening(true); |
| | 10002 | 44 | | } |
| | 10638 | 45 | | } |
| | | 46 | | |
| | | 47 | | protected void Start() |
| | 984 | 48 | | { |
| | 984 | 49 | | if (!IsListening && startListening == StartListenEvents.Start) |
| | 318 | 50 | | { |
| | 318 | 51 | | SetListening(true); |
| | 318 | 52 | | } |
| | 984 | 53 | | } |
| | | 54 | | |
| | | 55 | | protected virtual void OnEnable() |
| | 10956 | 56 | | { |
| | 10956 | 57 | | if (!IsListening && startListening == StartListenEvents.OnEnable) |
| | 636 | 58 | | { |
| | 636 | 59 | | SetListening(true); |
| | 636 | 60 | | } |
| | 10956 | 61 | | } |
| | | 62 | | |
| | | 63 | | protected virtual void OnDisable() |
| | 10956 | 64 | | { |
| | 10956 | 65 | | if (IsListening && stopListening == StopListenEvents.OnDisable) |
| | 1272 | 66 | | { |
| | 1272 | 67 | | SetListening(false); |
| | 1272 | 68 | | } |
| | 10956 | 69 | | } |
| | | 70 | | |
| | | 71 | | protected virtual void OnDestroy() |
| | 10638 | 72 | | { |
| | 10638 | 73 | | if (IsListening && stopListening == StopListenEvents.OnDestroy) |
| | 9684 | 74 | | { |
| | 9684 | 75 | | SetListening(false); |
| | 9684 | 76 | | } |
| | 10638 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Sets the listening state of the object. |
| | | 81 | | /// </summary> |
| | | 82 | | /// <param name="listen">If the object should listen to the target value.</param> |
| | | 83 | | protected virtual void SetListening(bool listen) |
| | 21912 | 84 | | { |
| | 21912 | 85 | | IsListening = listen; |
| | 21912 | 86 | | } |
| | | 87 | | |
| | | 88 | | #region Obsolete |
| | | 89 | | #if UNITY_EDITOR // Don't include in builds. |
| | | 90 | | [Obsolete("Use 'SetListening' instead. This will be removed in build.", true)] |
| | | 91 | | protected virtual void ToggleListening(bool listen) |
| | 0 | 92 | | { |
| | 0 | 93 | | throw new NotSupportedException(nameof(ToggleListening) + " is obsolete. Use " + nameof(SetListening) + " in |
| | | 94 | | } |
| | | 95 | | #endif // UNITY_EDITOR |
| | | 96 | | #endregion |
| | | 97 | | } |
| | | 98 | | } |