// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2024 Kybernetik // #if UNITY_EDITOR using System.Reflection; using UnityEditor; namespace Animancer.Editor { /// [Editor-Only] Utilities for using s. /// https://kybernetik.com.au/animancer/api/Animancer.Editor/DefaultValues public static class DefaultValues { /************************************************************************************************************************/ /// [Editor-Only] /// If the field represented by the `property` has a , /// this method sets the `value` to its value. /// If it was already at the value, it sets it to the /// value instead. And if the field has no attribute, it uses the default for the type. /// public static void SetToDefault(ref T value, SerializedProperty property) { var accessor = property.GetAccessor(); var field = accessor.GetField(property); if (field == null) accessor.SetValue(property, null); else SetToDefault(ref value, field); } /************************************************************************************************************************/ /// [Editor-Only] /// If the field represented by the `property` has a , /// this method sets the `value` to its value. /// If it was already at the value, it sets it to the /// value instead. And if the field has no attribute, it uses the default for the type. /// public static void SetToDefault(ref T value, FieldInfo field) { var defaults = field.GetAttribute(); if (defaults != null) defaults.SetToDefault(ref value); else value = default; } /************************************************************************************************************************/ /// [Editor-Only] /// Sets the `value` equal to the value. /// If it was already at the value, it sets it equal to the /// value instead. /// public static void SetToDefault(this DefaultValueAttribute attribute, ref T value) { var primary = attribute.Primary; if (!Equals(value, primary)) { value = (T)primary; return; } var secondary = attribute.Secondary; if (secondary != null || !typeof(T).IsValueType) { value = (T)secondary; return; } } /************************************************************************************************************************/ /// [Editor-Only] /// Sets the `value` equal to the `primary` value. /// If it was already at the value, it sets it equal to the `secondary` value instead. /// public static void SetToDefault(ref T value, T primary, T secondary) { if (!Equals(value, primary)) value = primary; else value = secondary; } /************************************************************************************************************************/ } } #endif