2019-11-14 12:59:11 +01:00
|
|
|
using System;
|
|
|
|
using System.Reflection;
|
2020-05-10 16:27:46 +02:00
|
|
|
using System.Linq;
|
|
|
|
using System.Collections;
|
2019-11-14 12:59:11 +01:00
|
|
|
using UnityEditor;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace UnityAtoms.Editor
|
|
|
|
{
|
2020-03-02 02:26:06 +01:00
|
|
|
public static class SerializedPropertyExtensions
|
2019-11-14 12:59:11 +01:00
|
|
|
{
|
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-23 02:39:43 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Generic method that tries to retrieve a value from a SerializedProperty of a specfic type.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="property">The SerializedProperty.</param>
|
|
|
|
/// <param name="managedObjectOut">Managed object.</param>
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
/// <returns>The value of type T.</returns>
|
2019-11-14 12:59:11 +01:00
|
|
|
public static T GetGenericPropertyValue<T>(this SerializedProperty property, T managedObjectOut)
|
|
|
|
{
|
|
|
|
object box = managedObjectOut;
|
|
|
|
var type = managedObjectOut.GetType();
|
|
|
|
foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
field.SetValue(box, property.FindPropertyRelative(field.Name).GetPropertyValue());
|
|
|
|
}
|
|
|
|
catch (InvalidOperationException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (T)box;
|
|
|
|
}
|
|
|
|
|
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-23 02:39:43 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Get property value from SerializedProperty.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="property">The SerializedProperty.</param>
|
|
|
|
/// <returns>The value as an object.</returns>
|
2019-11-14 12:59:11 +01:00
|
|
|
public static object GetPropertyValue(this SerializedProperty property)
|
|
|
|
{
|
|
|
|
if (property == null) throw new ArgumentNullException(nameof(property));
|
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-23 02:39:43 +01:00
|
|
|
switch (property.propertyType)
|
2019-11-14 12:59:11 +01:00
|
|
|
{
|
|
|
|
case SerializedPropertyType.ObjectReference: return property.objectReferenceValue;
|
|
|
|
case SerializedPropertyType.ArraySize: return property.arraySize;
|
|
|
|
case SerializedPropertyType.Integer: return property.intValue;
|
|
|
|
case SerializedPropertyType.Boolean: return property.boolValue;
|
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-23 02:39:43 +01:00
|
|
|
case SerializedPropertyType.Float: return property.floatValue;
|
2019-11-14 12:59:11 +01:00
|
|
|
case SerializedPropertyType.String: return property.stringValue;
|
|
|
|
case SerializedPropertyType.Color: return property.colorValue;
|
|
|
|
case SerializedPropertyType.LayerMask: return (LayerMask)property.intValue;
|
|
|
|
case SerializedPropertyType.Enum: return property.enumValueIndex;
|
|
|
|
case SerializedPropertyType.Vector2: return property.vector2Value;
|
|
|
|
case SerializedPropertyType.Vector3: return property.vector3Value;
|
|
|
|
case SerializedPropertyType.Vector4: return property.vector4Value;
|
|
|
|
case SerializedPropertyType.Vector2Int: return property.vector2IntValue;
|
|
|
|
case SerializedPropertyType.Vector3Int: return property.vector3IntValue;
|
|
|
|
case SerializedPropertyType.Quaternion: return property.quaternionValue;
|
|
|
|
case SerializedPropertyType.Rect: return property.rectValue;
|
|
|
|
case SerializedPropertyType.RectInt: return property.rectIntValue;
|
|
|
|
case SerializedPropertyType.BoundsInt: return property.boundsIntValue;
|
|
|
|
case SerializedPropertyType.Bounds: return property.boundsValue;
|
|
|
|
case SerializedPropertyType.Character: return (char)property.intValue;
|
|
|
|
case SerializedPropertyType.AnimationCurve: return property.animationCurveValue;
|
|
|
|
case SerializedPropertyType.FixedBufferSize: return property.fixedBufferSize;
|
|
|
|
case SerializedPropertyType.ExposedReference: return property.exposedReferenceValue;
|
|
|
|
case SerializedPropertyType.Generic:
|
|
|
|
case SerializedPropertyType.Gradient:
|
|
|
|
throw new InvalidOperationException($"Cant handle {property.propertyType} types. for property {property.name}");
|
|
|
|
default:
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-23 02:39:43 +01:00
|
|
|
/// <summary>
|
|
|
|
/// Determine if a SerializedProperty array contains an int value.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="property">The SerializedProperty.</param>
|
|
|
|
/// <param name="value">The value to check if it exists.</param>
|
|
|
|
/// <returns>True if the int exists in the array, otherwise false.</returns>
|
|
|
|
public static bool ArrayContainsInt(this SerializedProperty property, int value)
|
|
|
|
{
|
|
|
|
if (!property.isArray)
|
|
|
|
{
|
|
|
|
throw new ArgumentException("property is not an array");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < property.arraySize; ++i)
|
|
|
|
{
|
|
|
|
var intVal = property.GetArrayElementAtIndex(i).intValue;
|
|
|
|
if (intVal.Equals(value)) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Helper method to remove an element from a SerializedProperty array. Needed because of quirks using DeleteArrayElementAtIndex.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="property">The SerializedProperty.</param>
|
|
|
|
/// <param name="index">The index to delete from array.</param>
|
|
|
|
public static void RemoveArrayElement(this SerializedProperty property, int index)
|
|
|
|
{
|
|
|
|
if (!property.isArray)
|
|
|
|
{
|
|
|
|
throw new ArgumentException("property is not an array");
|
|
|
|
}
|
|
|
|
|
|
|
|
// For some reason you need to set objectReferenceValue to null in order to delete an array element from an array.
|
|
|
|
var itemProp = property.GetArrayElementAtIndex(index);
|
|
|
|
if (itemProp.propertyType == SerializedPropertyType.ObjectReference && itemProp.objectReferenceValue != null)
|
|
|
|
itemProp.objectReferenceValue = null;
|
|
|
|
|
|
|
|
// For some reason it is not possible to delete last element in array with DeleteArrayElementAtIndex.
|
|
|
|
if (index == property.arraySize - 1)
|
|
|
|
{
|
|
|
|
property.arraySize--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
property.DeleteArrayElementAtIndex(index);
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 16:27:46 +02:00
|
|
|
|
|
|
|
public static object GetParent(this SerializedProperty property)
|
|
|
|
{
|
|
|
|
var path = property.propertyPath.Replace(".Array.data[", "[");
|
|
|
|
object obj = property.serializedObject.targetObject;
|
|
|
|
var elements = path.Split('.');
|
|
|
|
foreach (var element in elements.Take(elements.Length - 1))
|
|
|
|
{
|
|
|
|
if (element.Contains("["))
|
|
|
|
{
|
|
|
|
var elementName = element.Substring(0, element.IndexOf("["));
|
|
|
|
var index = Convert.ToInt32(element.Substring(element.IndexOf("[")).Replace("[", "").Replace("]", ""));
|
|
|
|
obj = obj.GetValue(elementName, index);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
obj = obj.GetValue(element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static object GetValue(this object source, string name)
|
|
|
|
{
|
|
|
|
if (source == null)
|
|
|
|
return null;
|
|
|
|
var type = source.GetType();
|
|
|
|
var f = type.GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
|
|
|
|
if (f == null)
|
|
|
|
{
|
|
|
|
var p = type.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
|
|
|
|
if (p == null)
|
|
|
|
return null;
|
|
|
|
return p.GetValue(source, null);
|
|
|
|
}
|
|
|
|
return f.GetValue(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static object GetValue(this object source, string name, int index)
|
|
|
|
{
|
|
|
|
var enumerable = GetValue(source, name) as IEnumerable;
|
|
|
|
var enm = enumerable.GetEnumerator();
|
|
|
|
while (index-- >= 0)
|
|
|
|
enm.MoveNext();
|
|
|
|
return enm.Current;
|
|
|
|
}
|
2019-11-14 12:59:11 +01:00
|
|
|
}
|
|
|
|
}
|