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
3
Question by Local-Minimum-2 · Jan 25, 2016 at 12:30 AM · c#editor-scriptinggenerics

Generic CustomEditor

This question borders several previous questions so to be clear: It is not a matter of serialization of generic types, which has been answered elsewhere.

I have a generic class:

 public class EnumeratedPalette<T> : AbstractPalette where T : System.IConvertible
 {

     Color[] colors;

     public static int EnumLength {
         get {
             return System.Enum.GetValues (typeof(T)).Length;
         }
     }

     public static string FieldLabel(int index) {
         return System.Enum.GetName (typeof(T), index);
     }            

     protected void Reset()
     {
         colors = new Color[EnumLength];
     }
 }

And then I make specific versions of it like this:

 public enum FaceColors { Skin, Eye, Hair };

 public class FacePalette : ProcPixel.Fundamentals.EnumeratedPalette<FaceColors> { };

They serialize perfectly and work in unity as expected, getting the right length on colors when added to an GameObject. But instead of having a colors be a simple list in the inspector, I would want a generic CustomEditor for EnumeratedPalette so that I can set nice labels on the property-drawers based on the specific enum ( Color is a struct of mine with its own CustomPropertyDrawer which draws whatever label sent to it):

 [CustomEditor(typeof(EnumeratedPalette<>), true)]
 public class EnumeratedPaletteEditor<T> : Editor where T: System.IConvertible
 {
     public override void OnInspectorGUI ()
     {
         
         int size = EnumeratedPalette<T>.EnumLength;
         for (int index = 0; index < size; index++) {
             EditorGUILayout.PropertyField (serializedObject.FindProperty ("colors").GetArrayElementAtIndex (index), new GUIContent(EnumeratedPalette<T>.FieldLabel (index)));
         }
     }
 }

Edit / Workaround solution

I made a generic helper class:

 public static class EnumEditorHelper<T> where T: System.IConvertible
 {

     public static void UnpackProperty(SerializedProperty prop)
     {
         
         int size = EnumeratedPalette<T>.EnumLength;
         for (int index = 0; index < size; index++) {
             EditorGUILayout.PropertyField (prop.GetArrayElementAtIndex (index), new GUIContent(EnumeratedPalette<T>.FieldLabel (index)));
         }
     }
 }

And then each CustomEditor can look like:

 [CustomEditor(typeof(FacePalette), false)]
 public class FacePaletteEditor : Editor {

     public override void OnInspectorGUI ()
     {
         
         EditorGUI.indentLevel += 1;
         EnumEditorHelper<FaceColors>.UnpackProperty(serializedObject.FindProperty("colors"));

         EditorGUI.indentLevel -= 1;

         serializedObject.ApplyModifiedProperties ();
     }
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Bunny83 · Jan 25, 2016 at 05:05 AM

The Editor class is derived from ScriptableObject and as such it can't be a generic type. You need a concrete class and the class name has to match the filename.

You could have a generic base class like this but the actual editors must not contain a generic parameter. The editor instance has to be serializable as well ^^. Unity serializes all editors and editorwindows (which are also ScriptableObjects) whenever you change playmode or scripts are recompiled.

Even if Unity would support generic types, how should Unity decide which type it should use as generic parameter for your class when it creates an editor instance? The type of the inspected object? That would be "FacePalette" in your case.

What is "AbstractPalette" actually derived from? I guess, due to the "Reset" method, it's a MonoBehaviour? The generic parameter on your "EnumeratedPalette" class might still make problems when it comes to the editor. A generic base class makes it difficult to treat an instance as "base class" when you don't know the generic parameter type.

One solution might be to go another step back and insert a non generic base class infront of the generic which implements the "EnumLength" property and FieldLabel as virtual instance members. The generic class can override those. Inside the editor you can treat all instances as the non generic baseclass so you can use the method / property without knowing the concrete type inside the editor.

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

65 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Custom property drawer for generic 0 Answers

[solved]Using WWW instead of resources.load causes hang 1 Answer

When do the Attributes get set? 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