Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rayeloy · Feb 16, 2019 at 07:25 PM · custom editorscriptable objectstruct

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:

alt text

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.

custom-editor-for-attackdata.png (11.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges