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
1
Question by Spartiate0033 · Sep 24, 2016 at 02:05 AM · editorinterfacecustom-inspector

Custom Editor for Interface/Child classes not displaying

Hello everyone! I am currently trying to make a custom editor that all of my AI types will see. I have an AI interface called IAI which several classes derive from. Now, I did try setting the "bool editorForChildClasses to true but this did not work either. The inspector for all of my AI types remained unchanged. My child classes are Monobehaviour classes as well.

Now, I do know that interfaces are generally a big pain in the butt on Unity, so if that is the case I might have to try something else. Any help on getting an editor that would work for all of my AI types would be fantastic.

Thanks everyone!

My AI Interface:

 public interface IAI
             {
                 ReadOnlyCollection<AIStates.IAIState> AIStateCollection
                 {
                     get;
                 }
 
                 float MaxSpeed
                 {
                     get;
                 }
                 Transform PathingTarget
                 {
                     get; set;
                 }
                 Pathfinding.TargetMover TargetMover
                 {
                     get; set;
                 }
                 AILerp AILerp
                 {
                     get; set;
                 }
                 AIStates.IAIState CurrentAIState
                 {
                     get; set;
                 }
 
                 AITypes AIType
                 {
                     get;
                 }
 
                 void SetNewAIState(AIStates.AIStates state);
             }

My Editor:

 [CustomEditor(typeof(AI.AITypes.IAI), true)]
         public class AIEditor : Editor
         {
             private SerializedObject m_Object;

             public void OnEnable()
             {
                 m_Object = new SerializedObject(target);
             }
 
             public override void OnInspectorGUI()
             {
               Rect textBox = GUILayoutUtility.GetRect(0.0f, 35.0f, GUILayout.ExpandWidth(true));
                 GUI.Box(textBox, "The Inspector will enable you to edit individual states.");
             }
 }


Example child class

 public class Chef : MonoBehaviour, IAI
             {
                   // Code here
             }

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
3
Best Answer

Answer by Adam-Mechtley · Sep 24, 2016 at 06:50 PM

Hi @Spartiate0033! The problem is that the CustomEditor attribute only works with, MonoBehaviour- or ScriptableObject-derived classes. You cannot specify an interface or an open generic type. You would need to have something like:

 class AIBehaviour : MonoBehaviour, AI.AITypes.IAI
 {
     // your code here
 }

and correspondingly,

 [CustomEditor(typeof(AIBehaviour), true)]
 class AIEditor : Editor
 {
     // your code here
 }
Comment
Add comment · Show 3 · 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 Spartiate0033 · Sep 25, 2016 at 01:55 AM 0
Share

Hey! Thanks for the response. Unfortunately this does not work (I realize now I did not post a child class so I'll edit my question).

Right now, this is a child class

 public class Chef : $$anonymous$$onoBehaviour, IAI
             {
                      // Code
             }

So I am wondering if it has to do with the IAI being an interface because I know interfaces and Unity's editor don't often mix well.

avatar image Adam-Mechtley Spartiate0033 · Sep 26, 2016 at 06:48 AM 1
Share

Sorry if it got missed. The relevant point I was trying to make is that you cannot do this:

 [CustomEditor(typeof(AI.AITypes.IAI))]

You could only do this:

 [CustomEditor(typeof(Chef))]

As such, depending what you need your class hierarchy to look like, it may be easiest to have:

 public abstract class AIBehaviour : $$anonymous$$onoBehaviour, AI.AITypes.IAI {}
 
 public class Chef : AIBehaviour {}

avatar image Spartiate0033 Adam-Mechtley · Sep 26, 2016 at 03:16 PM 0
Share

I figured as much. I'll go back to the drawing board and see what I can do about making my AI editor. Thank you very much! I marked this as the answer.

avatar image
0

Answer by OndrejP · Feb 14, 2018 at 10:52 PM

For displaying additional info for my interfaces, I'm using following code.

It's not ideal, but works well when I don't want to create separate abstract class for each interface combination.

 public class MyBehaviour : MonoBehaviour, IMyInterface
 {
     public float SomeValue;
     protected MyInterfaceInspectorInfo m_info;
 }

 [Serializable]
 public struct MyInterfaceInspectorInfo
 {
 }

 [CustomPropertyDrawer(typeof(MyInterfaceInspectorInfo))]
 public class MyInterfaceInspectorInfoDrawer : PropertyDrawer
 {
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         var target = property.serializedObject.targetObject as IMyInterface;
         // TODO: Your drawing code here
     }
 }
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

64 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

Related Questions

Empty space at the top of a custom property drawer layout? 2 Answers

Custom inspector for AnimatorStateTransition 0 Answers

Project view thumbnails (Prefabs/Materials/etc...) 0 Answers

CustomAttribute on Class or Method ( EDIT: Button to call methods) 2 Answers

Unity Custom Editor Texture Warning 2 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