// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using UltimateXR.Core.Components;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace UltimateXR.Editor
{
public static partial class UxrEditorUtils
{
#region Public Types & Data
///
/// Name of the field that stores the unique ID, to be used by serialized properties.
///
public const string PropertyUniqueId = "_uxrUniqueId";
///
/// Name of the field that stores the prefab Guid.
///
public const string PropertyPrefabGuid = "__prefabGuid";
///
/// Name of the field that stores whether the component is stored in a prefab.
///
public const string PropertyIsInPrefab = "__isInPrefab";
#endregion
#region Public Methods
///
/// Assigns a serialized property value.
///
/// The object (GameObject or component) with the serialized property
/// The property name
/// Action that gets the serialized property as argument and enables to assign any value
///
///
/// UxrEditorUtils.AssignSerializedProperty(component, "_myBoolVar", p => p.boolValue = true);
///
///
public static void AssignSerializedProperty(Object obj, string propertyName, Action assigner)
{
SerializedObject serializedObject = new SerializedObject(obj);
SerializedProperty serializedProperty = serializedObject.FindProperty(propertyName);
if (serializedProperty == null)
{
Debug.LogError($"{nameof(AssignSerializedProperty)}(): Cannot find property {propertyName}");
return;
}
assigner.Invoke(serializedProperty);
serializedObject.ApplyModifiedProperties();
}
///
/// Appends a new element in a serialized array and optionally allows to assign its new value.
///
/// Serialized array property
///
/// An optional assign action that will receive the new serialized property appended as
/// parameter
///
public static void AppendSerializedArrayElement(SerializedProperty arrayProperty, Action assignAction = null)
{
int arraySize = arrayProperty.arraySize;
arrayProperty.InsertArrayElementAtIndex(arraySize);
assignAction?.Invoke(arrayProperty.GetArrayElementAtIndex(arraySize));
}
///
/// Gets a serialized array as an IEnumerable.
///
/// Serialized array property
/// Function that given the array element as a serialized property returns the given target object
///
///
/// IEnumerable<string> scenePaths = UxrEditorUtils.GetSerializedArrayAsEnumerable(propertyScenePathsArray, p => p.stringValue);
///
///
public static IEnumerable GetSerializedArrayAsEnumerable(SerializedProperty arrayProperty, Func assigner)
{
for (int i = 0; i < arrayProperty.arraySize; ++i)
{
T element = assigner(arrayProperty.GetArrayElementAtIndex(i));
yield return element;
}
}
///
/// Stores a set of elements in an array serialized property. The elements to store derive from
/// .
///
/// Type of elements to store
/// The to assign
/// The elements to store
public static void AssignSerializedPropertyArray(SerializedProperty propertyArray, IEnumerable elements) where T : Object
{
propertyArray.ClearArray();
propertyArray.arraySize = elements.Count();
int index = 0;
foreach (T element in elements)
{
propertyArray.GetArrayElementAtIndex(index).objectReferenceValue = element;
index++;
}
}
///
/// Stores a set of elements in an array serialized property. The elements to store should be of a simple type (bool,
/// int, float, string) or Unity type (Vector3, Color).
///
/// Type of elements to store
/// The to assign
/// The elements to store
public static void AssignSerializedPropertySimpleTypeArray(SerializedProperty propertyArray, IEnumerable elements)
{
propertyArray.ClearArray();
propertyArray.arraySize = elements.Count();
int index = 0;
foreach (T element in elements)
{
if (element == null)
{
continue;
}
SerializedProperty property = propertyArray.GetArrayElementAtIndex(index);
switch (element)
{
case bool b:
property.boolValue = b;
break;
case int i:
property.intValue = i;
break;
case float f:
property.floatValue = f;
break;
case string s:
property.stringValue = s;
break;
case Color col:
property.colorValue = col;
break;
case Vector3 v3:
property.vector3Value = v3;
break;
default: throw new NotSupportedException($"Conversion to {typeof(T)} serialized property array is not supported yet");
}
index++;
}
}
#endregion
}
}