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
2
Question by StormMuller · Jul 22, 2017 at 10:31 AM · c#inspectoreditor-scriptingunityengine.object

How to make custom Inspectors for serialized classes

I have 2 simple pieces of code:

Unity Objects:

 using System;
 using UnityEngine;
 
 public class ModularJoiner : MonoBehaviour
 {
     public ModularJoint joint;
 }
 
 [Serializable]
 public class ModularJoint : UnityEngine.Object
 {
     public Vector3 Position;
     public Vector3 NormalRotation;
 }

And then my custom inspector for my Serializable class:

 using UnityEditor;
 
 [CustomEditor(typeof(ModularJoint))]
 public class ModularJointEditor : Editor
 { 
     public override void OnInspectorGUI()
     {
         var joint = (ModularJoint)target;
 
         joint.Position = EditorGUILayout.Vector3Field("Position", joint.Position);        
         joint.NormalRotation = EditorGUILayout.Vector3Field("Normal", joint.NormalRotation);
     }
 }

This is the result in the inspector:

Inspector

And this is what I want, excluding the expanding thing(Highlighted yellow):

Inspector

Please, I don't want to add the ModularJoint fields into the ModularJoiner class. Because I am actually using an array of joints. But I kept it simple so that my code is clearer.

It seems that by inheriting from UnityEngine.Object causes the issue. But I need to do this if I wish to do this: var joint = (ModularJoint)target;

When looking online I keep seeing Property Drawers which is not quite what I want to achieve.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Bunny83 · Jul 22, 2017 at 11:23 AM

You can't because custom serializable classes are not serialized on their own. They are just part of the MonoBehaviour that containst a variable of that type. Custom inspectors only work for classes derived from UnityEngine.Object. The only classes you can use as base classes which are derived fro UnityEngine.Object are MonoBehaviour or ScriptableObject. You should not derive your own class from UnityEngine.Object. Things derived from "UnityEngine.Object" are serialized on their own.

You have to use a property drawer here. Why do you think that a property drawer is not quite qhat you want? To me it looks like it's exactly what you need -.-

What's your problem with using a property drawer? Keep in mind if you need a multi-line property you have to override the GetPropertyHeight method as well and provide a proper height value.

edit

For example:

 using UnityEditor;
 using UnityEngine;
 
 [CustomPropertyDrawer(typeof(ModularJoint))]
 public class ModularJointDrawer : PropertyDrawer
 {
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return 16f * 2;
     }
     public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
     {
         var positionProperty = property.FindPropertyRelative("Position");
         var normalProperty = property.FindPropertyRelative("NormalRotation");
         
         EditorGUIUtility.wideMode = true;
         EditorGUIUtility.labelWidth = 70;
         rect.height /= 2;
         positionProperty.vector3Value = EditorGUI.Vector3Field(rect, "Position", positionProperty.vector3Value);
         rect.y += rect.height;
         normalProperty.vector3Value = EditorGUI.Vector3Field(rect, "Normal", normalProperty.vector3Value);
     }
 }


If instead you want a single Joint to occupy a single line you can set the height to 16f instead of 16*2 and use something like this instead:

     EditorGUIUtility.wideMode = true;
     EditorGUIUtility.labelWidth = 30;
     EditorGUI.indentLevel = 0;
     rect.width /= 2;
     positionProperty.vector3Value = EditorGUI.Vector3Field(rect,"pos", positionProperty.vector3Value);
     rect.x += rect.width;
     normalProperty.vector3Value = EditorGUI.Vector3Field(rect, "norm", normalProperty.vector3Value);

Here's how it would look like. The first has the two fields on top of each other, the second has both in one line

alt text


propertydrawerexample.png (26.2 kB)
Comment
Add comment · Share
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
avatar image
0

Answer by alussam · Jan 06, 2019 at 07:18 PM

https://docs.unity3d.com/ScriptReference/PropertyDrawer.html https://docs.unity3d.com/ScriptReference/PropertyDrawer.html

Comment
Add comment · Share
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

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

364 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 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 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 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 classes, inheritance and the inpector 0 Answers

Custom ordering of variables in inspector when using base and derived classes 1 Answer

How to access one class instance in editor script? 1 Answer

Multiple Cars not working 1 Answer

Make the Inspector only show a specific CustomEditor 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