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 Ruffian0305 · Sep 12, 2015 at 05:47 PM · c#editor-scriptinginheritancepropertydrawerproperty drawer

Property Drawer and Inheritance

Hello, I am trying to create a very basic engine with a custom editor/property drawers and I seem to be messing it up royally. I am semi-familiar with property drawers and editors, as well as how they work together but this really has me stumped.

I have three classes, an Engine (with an array of waypoints), A Waypoint (with an enum to store the waypoint type), and a MoveWaypoint (an example waypoint type with custom data - extends Waypoint).

The engine has a custom inspector that just displays the array of waypoints (for now!) and is working as intended. The Waypoint has a property drawer that displays the enum for the user to choose. What I am trying to do is if the user sets the Waypoint to Move, to show the MoveWaypoint propertydrawer. I have tried a few different ways of doing this (after crawling through lots and lots of forums) but all the information is either out of date or I can't get it to work.

Currently the three scripts are just test beds for the engine, so it may seem silly to go through a lot of effort for just these three scripts. However, I am eventually going to have over two dozen scripts each with their own sets of variables and manipulations so I want to get this figured out (assuming it is possible) before I dive too deep down the rabbit hole!

I appreciate any and all help, all scripts are provided below.

LevelEngine:

 using UnityEngine;
 using System.Collections;
 
 public class LevelEngine: MonoBehaviour {
 
     public WaypointScript[] waypoints;
 }

WaypointScript:

 public enum MovementTypes
 {
     WAIT,
     MOVE
 };
 
 [System.Serializable]
 public class WaypointScript{
 
     public MovementTypes moveType;
 }

and MoveWaypointScript:

 using UnityEngine;
 
 [System.Serializable]
 public class MoveWaypointScript: WaypointScript{
 
     public Vector3 target;
     public float moveTime;
 }


Each has their own custom inspector/editor declared

EngineEditor (Works fine?):

 using UnityEditor;
 
 [CustomEditor(typeof(LevelEngine))]
 public class EngineEditor :  Editor
 { 
 
     public override void OnInspectorGUI()
     {
         serializedObject.Update ();
         SerializedProperty waypointsArray = serializedObject.FindProperty ("waypoints");
         EditorGUILayout.PropertyField(waypointsArray);
         EditorGUILayout.PropertyField(waypointsArray.FindPropertyRelative("Array.size"));
 
         for (int i = 0; i < waypointsArray.arraySize; i++)
         {
             EditorGUILayout.PropertyField(waypointsArray.GetArrayElementAtIndex(i));
         }
 
         serializedObject.ApplyModifiedProperties();
     }
 }
 

WaypointEditorDrawer - Works Fine, but there is where the real problem arises -

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomPropertyDrawer(typeof(ScriptWaypoint), false)]
 public class WaypointEditorDrawer : PropertyDrawer 
 {
     ScriptWaypoint waypointScript;
     float extraHeight = 17f;
     
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.BeginProperty(position, label, property);
         SerializedProperty movementTypes = property.FindPropertyRelative("moveType");
 
         EditorGUILayout.PropertyField(movementTypes);
 
     EditorGUI.EndProperty();
     }
 
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return base.GetPropertyHeight(property, label) + (extraHeight );
     }
 
 
 }

And finally MovementEditorDrawer:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomPropertyDrawer(typeof(ScriptMove))]
 public class MovementEditorDrawer :  PropertyDrawer {
 
     ScriptMove moveScript;
     float extraHeight = 55f;
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
 
         EditorGUI.BeginProperty(position, label, property);
         SerializedProperty moveTime = property.FindPropertyRelative("moveTime");
         EditorGUI.PropertyField(position, moveTime);
         EditorGUI.EndProperty();
     }
 }
 



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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Why is my propertydrawer being automatically disabled? 1 Answer

Using CustomPropertyDrawer in OnGUI of ScriptableWizard 0 Answers

CustomPropertyDrawer: foldout issues when switching properties (simulating weak references) 0 Answers

How to prevent overlap when using nested arrays in CustomPropertyDrawers? 1 Answer


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