2018-10-30 20:05:06 +01:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Events;
|
2019-03-17 23:43:20 +01:00
|
|
|
using UnityAtoms.Utils;
|
2019-04-07 16:03:16 +02:00
|
|
|
using UnityEngine.Serialization;
|
2018-10-30 20:05:06 +01:00
|
|
|
|
|
|
|
namespace UnityAtoms
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
public abstract class CreateVariableOnAwake<T, V, E1, E2, L1, L2, GA1, GA2, GA3, GA4, UER1, UER2> : MonoBehaviour
|
|
|
|
where V : ScriptableObjectVariable<T, E1, E2>
|
|
|
|
where E1 : GameEvent<T>
|
|
|
|
where E2 : GameEvent<T, T>
|
|
|
|
where L1 : GameEventListener<T, GA1, E1, UER1>
|
|
|
|
where L2 : GameEventListener<T, T, GA2, E2, UER2>
|
|
|
|
where GA1 : GameAction<T>
|
|
|
|
where GA2 : GameAction<T, T>
|
|
|
|
where GA3 : GameAction<V>
|
|
|
|
where GA4 : GameAction<V, GameObject>
|
|
|
|
where UER1 : UnityEvent<T>
|
|
|
|
where UER2 : UnityEvent<T, T>
|
2018-10-30 20:05:06 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
[FormerlySerializedAs("CreateChangedEvent")]
|
2018-10-30 20:05:06 +01:00
|
|
|
[SerializeField]
|
2019-04-07 16:03:16 +02:00
|
|
|
private bool _createChangedEvent = true;
|
|
|
|
|
|
|
|
[FormerlySerializedAs("CreateChangedWithHistoryEvent")]
|
2018-10-30 20:05:06 +01:00
|
|
|
[SerializeField]
|
2019-04-08 16:14:50 +02:00
|
|
|
private bool _createChangedWithHistoryEvent = false;
|
2018-10-30 20:05:06 +01:00
|
|
|
|
2019-04-07 16:03:16 +02:00
|
|
|
[FormerlySerializedAs("Listener")]
|
2018-10-30 20:05:06 +01:00
|
|
|
[SerializeField]
|
2019-04-08 16:14:50 +02:00
|
|
|
private L1 _listener = null;
|
2019-04-07 16:03:16 +02:00
|
|
|
|
|
|
|
[FormerlySerializedAs("ListenerWithHistory")]
|
2018-10-30 20:05:06 +01:00
|
|
|
[SerializeField]
|
2019-04-08 16:14:50 +02:00
|
|
|
private L2 _listenerWithHistory = null;
|
2018-10-30 20:05:06 +01:00
|
|
|
|
2019-04-07 16:03:16 +02:00
|
|
|
[FormerlySerializedAs("OnVariableCreate")]
|
2018-10-30 20:05:06 +01:00
|
|
|
[SerializeField]
|
2019-04-08 16:14:50 +02:00
|
|
|
private GA3 _onVariableCreate = null;
|
2019-04-07 16:03:16 +02:00
|
|
|
|
|
|
|
[FormerlySerializedAs("OnVariableCreateWithGO")]
|
2018-10-30 20:05:06 +01:00
|
|
|
[SerializeField]
|
2019-04-08 16:14:50 +02:00
|
|
|
private GA4 _onVariableCreateWithGO = null;
|
2018-10-30 20:05:06 +01:00
|
|
|
|
2019-04-07 16:03:16 +02:00
|
|
|
private void Awake()
|
2018-10-30 20:05:06 +01:00
|
|
|
{
|
2019-04-12 09:41:53 +02:00
|
|
|
var variable = DynamicAtoms.CreateVariable<T, V, E1, E2>(
|
|
|
|
initialValue: default(T),
|
|
|
|
changed: _createChangedEvent ? ScriptableObject.CreateInstance<E1>() : null,
|
|
|
|
changedWithHistory: _createChangedWithHistoryEvent ? ScriptableObject.CreateInstance<E2>() : null
|
|
|
|
);
|
2018-10-30 20:05:06 +01:00
|
|
|
|
|
|
|
if (variable.Changed != null)
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
if (_listener != null)
|
2018-10-30 20:05:06 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_listener.GameEvent = variable.Changed;
|
|
|
|
_listener.GameEvent.RegisterListener(_listener);
|
2018-10-30 20:05:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (variable.ChangedWithHistory != null)
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
if (_listenerWithHistory != null)
|
2018-10-30 20:05:06 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_listenerWithHistory.GameEvent = variable.ChangedWithHistory;
|
|
|
|
_listenerWithHistory.GameEvent.RegisterListener(_listenerWithHistory);
|
2018-10-30 20:05:06 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-07 16:03:16 +02:00
|
|
|
if (_onVariableCreate != null) { _onVariableCreate.Do(variable); }
|
|
|
|
if (_onVariableCreateWithGO != null) { _onVariableCreateWithGO.Do(variable, gameObject); }
|
2018-10-30 20:05:06 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|