- Home /
How to show complex types properties in a Custom Editor
So I'm making some scriptable objects for attacks that go like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CreateAssetMenu(fileName = "New attack", menuName = "Attack")]
public class AttackData : ScriptableObject
{
public enum KnockbackType
{
outwards,
inwards,
customDir
}
public string attackName;
[Tooltip("Leave at 0 if no charging.")]
public AttackPhase chargingPhase;
public AttackPhase startupPhase;
public AttackPhase activePhase;
public AttackPhase recoveryPhase;
[Tooltip("Percentage of time of the last phase (or recovery phase) that we can skip by using an attack different from this one. 0% means we can't skip it at all," +
" 100% means we can fully skip it.")]
[Range(0, 100)]
public float comboDifferentAttackPercent = 50;
[Tooltip("Time the attacks leaves the target stunned.")]
public float stunTime;
[Tooltip("Limited by maxMoveSpeed.")]
public float knockbackSpeed;
[Tooltip("Outwards is the basic one.")]
public KnockbackType knockbackType = KnockbackType.outwards;
[Tooltip("Leave at 0 if knockbackType is not customDir.")]
public Vector3 knockbackDirection = Vector3.zero;
}
The AttackPhase type is a struct that I created like this:
[System.Serializable]
public struct AttackPhase
{
public float duration;
public bool restrictRotation;
public float rotationSpeed;
public GameObject hitboxPrefab;
public AttackPhase(float _duration, bool _restrictRotation, float _rotationSpeed, GameObject _hitboxPrefab)
{
duration = _duration;
restrictRotation = _restrictRotation;
rotationSpeed = _rotationSpeed;
hitboxPrefab = _hitboxPrefab;
}
}
Then I created a custom editor for AttackData like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(AttackData))]
[CanEditMultipleObjects]
public class AttackDataEditor : Editor
{
SerializedProperty attackName;
SerializedProperty chargingPhase;
SerializedProperty startupPhase;
SerializedProperty activePhase;
SerializedProperty recoveryPhase;
SerializedProperty comboDifferentAttackPercent;
SerializedProperty stunTime;
SerializedProperty knockbackSpeed;
SerializedProperty knockbackType;
SerializedProperty knockbackDirection;
AttackData myScript = null;
bool chargingToggle = false;
private void OnEnable()
{
myScript = (AttackData)target;
attackName = serializedObject.FindProperty("attackName");
chargingPhase = serializedObject.FindProperty("chargingPhase");
startupPhase = serializedObject.FindProperty("startupPhase");
activePhase = serializedObject.FindProperty("activePhase");
recoveryPhase = serializedObject.FindProperty("recoveryPhase");
comboDifferentAttackPercent = serializedObject.FindProperty("comboDifferentAttackPercent");
stunTime = serializedObject.FindProperty("stunTime");
knockbackSpeed = serializedObject.FindProperty("knockbackSpeed");
knockbackType = serializedObject.FindProperty("knockbackType");
knockbackDirection = serializedObject.FindProperty("knockbackDirection");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(attackName);
GUILayout.BeginHorizontal();
GUILayout.Label("Has Charging phase", GUILayout.Width(120));
chargingToggle = EditorGUILayout.Toggle(chargingToggle);
GUILayout.EndHorizontal();
if (chargingToggle)
{
EditorGUILayout.PropertyField(chargingPhase);
}
EditorGUILayout.PropertyField(startupPhase);
EditorGUILayout.PropertyField(activePhase);
EditorGUILayout.PropertyField(recoveryPhase);
EditorGUILayout.PropertyField(comboDifferentAttackPercent);
EditorGUILayout.PropertyField(stunTime);
EditorGUILayout.PropertyField(knockbackSpeed);
EditorGUILayout.PropertyField(knockbackType);
EditorGUILayout.PropertyField(knockbackDirection);
serializedObject.ApplyModifiedProperties();
}
}
This makes the inspector look like this:
So my questions are:
1 - How do I show the properties of my AttackPhase type variables? they do not show when I click on it even with the [System.serializable] property.
2 - Is there any way to change only 1 variable with the custom editor? I only want to change the chargingPhase so that it only shows it's properties once the toggle is set to true, instead I find myself writing dozens of apparently useless lines of code.
Your answer
Follow this Question
Related Questions
Custom Editor ScriptableObject List shows "Type Mismatch" after CreateInstance 2 Answers
How to do a custom editor for a ScriptableObject that is a property of a MonoBehaviour 2 Answers
Cannot see serialized object's fields in Inspector 1 Answer
My structs are acting like classes in scriptable object. 1 Answer
Item database with custom editor 0 Answers