unity-atoms/Source/CreateOnAwake/CreateListOnAwake.cs
2019-05-29 18:52:27 +02:00

85 lines
2.9 KiB
C#

using UnityEngine;
using UnityEngine.Events;
using UnityAtoms.Utils;
using UnityEngine.Serialization;
namespace UnityAtoms
{
public abstract class CreateListOnAwake<T, L, E, TEL, TELR, GA1, GA2, UER> : MonoBehaviour
where L : ScriptableObjectList<T, E> where E : GameEvent<T>
where TEL : GameEventListener<T, TELR, E, UER>
where TELR : GameAction<T>
where GA1 : GameAction<L> where GA2 : GameAction<L, GameObject>
where UER : UnityEvent<T>
{
[FormerlySerializedAs("CreateAddedEvent")]
[SerializeField]
private bool _createAddedEvent = true;
[FormerlySerializedAs("CreateRemovedEvent")]
[SerializeField]
private bool _createRemovedEvent = true;
[FormerlySerializedAs("CreateClearedEvent")]
[SerializeField]
private bool _createClearedEvent = false;
[FormerlySerializedAs("AddedListener")]
[SerializeField]
private TEL _addedListener = null;
[FormerlySerializedAs("RemovedListener")]
[SerializeField]
private TEL _removedListener = null;
[FormerlySerializedAs("ClearedListener")]
[SerializeField]
private VoidListener _clearedListener = null;
[FormerlySerializedAs("OnListCreate")]
[SerializeField]
private GA1 _onListCreate = null;
[FormerlySerializedAs("OnListCreateWithGO")]
[SerializeField]
private GA2 _onListCreateWithGO = null;
private void Awake()
{
var list = DynamicAtoms.CreateList<T, L, E>(
added: _createAddedEvent ? ScriptableObject.CreateInstance<E>() : null,
removed: _createRemovedEvent ? ScriptableObject.CreateInstance<E>() : null,
cleared: _createClearedEvent ? ScriptableObject.CreateInstance<VoidEvent>() : null
);
if (list.Added != null)
{
if (_addedListener != null)
{
_addedListener.GameEvent = list.Added;
_addedListener.GameEvent.RegisterListener(_addedListener);
}
}
if (list.Removed != null)
{
if (_removedListener != null)
{
_removedListener.GameEvent = list.Removed;
_removedListener.GameEvent.RegisterListener(_removedListener);
}
}
if (list.Cleared != null)
{
if (_clearedListener != null)
{
_clearedListener.GameEvent = list.Cleared;
_clearedListener.GameEvent.RegisterListener(_clearedListener);
}
}
if (_onListCreate != null) { _onListCreate.Do(list); }
if (_onListCreateWithGO != null) { _onListCreateWithGO.Do(list, gameObject); }
}
}
}