using System; using UnityEngine; namespace MalbersAnimations.Scriptables { /// Prefab Scriptable Variable. Based on the Talk - Game Architecture with Scriptable Objects by Ryan Hipple [CreateAssetMenu(menuName = "Malbers Animations/Variables/Game Object", order = 3000)] public class GameObjectVar : ScriptableVar { [SerializeField] private GameObject value; /// Invoked when the value changes public Action OnValueChanged; /// Value of the Bool variable public virtual GameObject Value { get => value; set { this.value = value; OnValueChanged?.Invoke(value); //If we are using OnChange event Invoked #if UNITY_EDITOR if (debug) Debug.Log($"{name} -> [ {value} ] "); #endif } } public virtual void SetValue(GameObjectVar var) => Value = var.Value; public virtual void SetNull(GameObjectVar var) => Value = null; public virtual void SetValue(GameObject var) => Value = var; public virtual void SetValue(Component var) => Value = var.gameObject; } [System.Serializable] public class GameObjectReference : ReferenceVar { #pragma warning disable CA2235 // Mark all non-serializable fields public GameObject ConstantValue; [RequiredField] public GameObjectVar Variable; #pragma warning restore CA2235 // Mark all non-serializable fields public GameObjectReference() => UseConstant = true; public GameObjectReference(GameObject value) => Value = value; public GameObjectReference(GameObjectVar value) { Variable = value; UseConstant = false; } public GameObject Value { get => UseConstant ? ConstantValue : (Variable != null ? Variable.Value : null); set { if (UseConstant || Variable == null) { ConstantValue = value; UseConstant = true; } else { Variable.Value = value; } } } } #if UNITY_EDITOR [UnityEditor.CanEditMultipleObjects, UnityEditor.CustomEditor(typeof(GameObjectVar))] public class GameObjectVarEditor : VariableEditor { public override void OnInspectorGUI() { serializedObject.Update(); MalbersEditor.DrawDescription("GameObject/Prefab Variable"); UnityEditor.EditorGUILayout.BeginVertical(UnityEditor.EditorStyles.helpBox); var go = value.objectReferenceValue as GameObject; UnityEditor.EditorGUILayout.BeginHorizontal(); if (go == null || go.IsPrefab()) { UnityEditor.EditorGUILayout.PropertyField(value, new GUIContent("Prefab", "The current value")); } else { if (Application.isPlaying) { UnityEditor.EditorGUILayout.ObjectField("Value ", go, typeof(GameObject), false); } } MalbersEditor.DrawDebugIcon(debug); UnityEditor.EditorGUILayout.EndHorizontal(); UnityEditor.EditorGUILayout.PropertyField(Description); UnityEditor.EditorGUILayout.EndVertical(); serializedObject.ApplyModifiedProperties(); } } #endif }