Added Variable Instancer, Event Reference, Atom Collection and Atom List (old Atom List renamed to Atom Value List) (#110)
AtomVariableInstancer
- Added AtomVariableInstancer as an option to AtomReference.
- Added AtomVariableInstancer to generator.
- Added editor icon for AtomVariableInstancer.
AtomEventReference
- Added an AtomEventReference class (and AtomEventX2Reference). It’s similar to an AtomReference, but for Events. Let’s you pick between an Event, Variable (will select the Changed event) and a VariableInstancer (see above).
- Added AtomEventReference and AtomEventX2Reference to generator.
- Added a drawer for AtomEventReference.
- Listeners are now using AtomEventReference instead of AtomEvent.
- Refactoring of VoidHooks since Listeners are now using AtomEventReference.
AtomCollection
- Created an AtomCollection - a collection of Atoms associated with key strings (AtomReferences).
- Added new editor icon for collections.
- Created a SerializableDictionary class, which AtomCollection is using.
- Custom property drawer for SerializableDictionary.
- SerializableDictionary supports nested structures meaning that a AtomCollection can have a KVP that is pointing to another AtomCollection.
- AtomCollections have 3 events: Added, Removed, Cleared.
- Added an option to sync an InstanceVariable to collection - adding it to the collection when created (using gameObject’s instance id as key) and removing it from the collection when destroyed.
AtomList
- Renamed old AtomList to AtomValueList
- Added AtomList, like Collection, but a list
- Added new icon for AtomList
- Created a AtomBaseVariableList class, which AtomList is using.
- Custom property drawer for AtomBaseVariableList.
- AtomLists have 3 events: Added, Removed, Cleared.
- Added an option to sync an InstanceVariable to list - adding it to the list when created and removing it from the list when destroyed.
2020-02-22 20:39:43 -05:00
using UnityEngine ;
using UnityEngine.Assertions ;
namespace UnityAtoms
{
/// <summary>
/// A Variable Instancer is a MonoBehaviour that takes a variable as a base and creates an in memory copy of it OnEnable.
/// This is handy when you want to use atoms for prefabs that are instantiated at runtime. Use together with AtomCollection to
2020-03-15 17:33:27 -04:00
/// react accordingly when a prefab with an associated atom is added or deleted to the scene.
Added Variable Instancer, Event Reference, Atom Collection and Atom List (old Atom List renamed to Atom Value List) (#110)
AtomVariableInstancer
- Added AtomVariableInstancer as an option to AtomReference.
- Added AtomVariableInstancer to generator.
- Added editor icon for AtomVariableInstancer.
AtomEventReference
- Added an AtomEventReference class (and AtomEventX2Reference). It’s similar to an AtomReference, but for Events. Let’s you pick between an Event, Variable (will select the Changed event) and a VariableInstancer (see above).
- Added AtomEventReference and AtomEventX2Reference to generator.
- Added a drawer for AtomEventReference.
- Listeners are now using AtomEventReference instead of AtomEvent.
- Refactoring of VoidHooks since Listeners are now using AtomEventReference.
AtomCollection
- Created an AtomCollection - a collection of Atoms associated with key strings (AtomReferences).
- Added new editor icon for collections.
- Created a SerializableDictionary class, which AtomCollection is using.
- Custom property drawer for SerializableDictionary.
- SerializableDictionary supports nested structures meaning that a AtomCollection can have a KVP that is pointing to another AtomCollection.
- AtomCollections have 3 events: Added, Removed, Cleared.
- Added an option to sync an InstanceVariable to collection - adding it to the collection when created (using gameObject’s instance id as key) and removing it from the collection when destroyed.
AtomList
- Renamed old AtomList to AtomValueList
- Added AtomList, like Collection, but a list
- Added new icon for AtomList
- Created a AtomBaseVariableList class, which AtomList is using.
- Custom property drawer for AtomBaseVariableList.
- AtomLists have 3 events: Added, Removed, Cleared.
- Added an option to sync an InstanceVariable to list - adding it to the list when created and removing it from the list when destroyed.
2020-02-22 20:39:43 -05:00
/// </summary>
/// <typeparam name="V">Variable of type T.</typeparam>
/// <typeparam name="T">The value type.</typeparam>
/// <typeparam name="E1">Event of type T.</typeparam>
/// <typeparam name="E2">Event x 2 of type T.</typeparam>
/// <typeparam name="F">Function of type T => T</typeparam>
[EditorIcon("atom-icon-hotpink")]
[DefaultExecutionOrder(Runtime.ExecutionOrder.VARIABLE_INSTANCER)]
public abstract class AtomVariableInstancer < V , T , E1 , E2 , F > : MonoBehaviour
where V : AtomVariable < T , E1 , E2 , F >
where E1 : AtomEvent < T >
where E2 : AtomEvent < T , T >
where F : AtomFunction < T , T >
{
/// <summary>
/// Getter for retrieving the in memory runtime variable.
/// </summary>
public V Variable { get = > _inMemoryCopy ; }
/// <summary>
/// Getter for retrieving the value of the in memory runtime variable.
/// </summary>
public T Value { get = > _inMemoryCopy . Value ; set = > _inMemoryCopy . Value = value ; }
private V _inMemoryCopy ;
/// <summary>
/// The variable that the in memory copy will be based on when created at runtime.
/// </summary>
[SerializeField]
private V _base = null ;
/// <summary>
/// If assigned the in memory copy variable will be added to the collection on Start using the gameObject's instance id as key. The value will also be removed from the collection OnDestroy.
/// </summary>
[SerializeField]
private AtomCollection _syncToCollection = null ;
/// <summary>
/// If assigned the in memory copy variable will be added to the list on Start. The value will also be removed from the list OnDestroy.
/// </summary>
[SerializeField]
private AtomList _syncToList = null ;
private void OnEnable ( )
{
Assert . IsNotNull ( _base ) ;
_inMemoryCopy = Instantiate ( _base ) ;
if ( _base . Changed ! = null )
{
_inMemoryCopy . Changed = Instantiate ( _base . Changed ) ;
}
if ( _base . ChangedWithHistory ! = null )
{
_inMemoryCopy . ChangedWithHistory = Instantiate ( _base . ChangedWithHistory ) ;
}
}
void Start ( )
{
// Adding to the collection on Start instead of OnEnable because of timing issues that otherwise occurs when listeners register themselves OnEnable.
// This is an issue when a Game Object has a Variable Instancer attached to it when the scene starts and at the same time their is an AtomBaseListener listening to the associated Added event to a Collection.
if ( _syncToCollection ! = null )
{
_syncToCollection . Value . Add ( GetInstanceID ( ) . ToString ( ) , _inMemoryCopy ) ;
}
if ( _syncToList ! = null )
{
_syncToList . Value . Add ( _inMemoryCopy ) ;
}
}
private void OnDestroy ( )
{
if ( _syncToCollection ! = null )
{
_syncToCollection . Value . Remove ( GetInstanceID ( ) . ToString ( ) ) ;
}
if ( _syncToList ! = null )
{
_syncToList . Value . Remove ( _inMemoryCopy ) ;
}
}
}
}