You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.5 KiB
C#

3 years ago
using System;
using UnityEngine;
namespace MalbersAnimations.Scriptables
{
///<summary> Prefab Scriptable Variable. Based on the Talk - Game Architecture with Scriptable Objects by Ryan Hipple </summary>
[CreateAssetMenu(menuName = "Malbers Animations/Variables/Game Object", order = 3000)]
public class GameObjectVar : ScriptableVar
{
[SerializeField] private GameObject value;
/// <summary>Invoked when the value changes </summary>
public Action<GameObject> OnValueChanged;
/// <summary> Value of the Bool variable</summary>
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($"<B>{name} -> [<color=cyan> {value} </color>] </B>");
#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
}