Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 gccps3c3c3c · Aug 24, 2017 at 08:43 AM · c#inheritancecustom editorcustom-inspectorabstract

C# unity custom editor multiple different components but same base class

I'm making a strategy game , there are a lot of different space ship type , but all base on one abstract class .

 public abstract class SpaceShip : MonoBehaviour , IHPSystem

example :

 public class BasicShip : SpaceShip

 public class CaptureShip : SpaceShip

 public class EnergyBlasterShip : SpaceShip

I created a custom editor for SpaceShip

     [CustomEditor(typeof(SpaceShip) , true)]
     [CanEditMultipleObjects]
     public class SpaceShipCustomEditor : Editor
     {
 
         public override void OnInspectorGUI()
         {
             EditorGUILayout.HelpBox("This is a custom editor. If anything gone wrong , please go to non custom editor and try.", MessageType.Info);
 
             // . . . . . 
 
 
         }
 
 
 
     }

But when i select two different type of space ship , my custom editor said :

 components that are only on some of the selected objects cannot be multi-edited , 

How can i edit different space ship simultaneously ? I only want to edit fields on the base class , not the fields of inherit class.

Comment
Add comment · Show 3
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 WinterboltGames · Aug 24, 2017 at 12:12 PM 0
Share

Not sure what's your problem but I think that in your OnInspectorGUI() there should be something like this...

 public override void OnInspectorGUI ()
 {
     // you missed this.
     var editedObject = (SpaceShip)target;
     
     // the rest of your code...
 
 }
avatar image Bunny83 WinterboltGames · Aug 24, 2017 at 01:36 PM 0
Share

I think you did not understand the problem. His custom inspector does work for a single instance but he wants to use the multi object edit feature of the inspector. However that's not possible with different classes.

Also using "target" will force your inspector into single object mode as target just represent one object. When you create an inspector with "CanEdit$$anonymous$$ultipleObjects" you have to use the SerializedObject and SerializedProperty classes or use the "targets" array. However multi editing gets quite difficult when you have to handle it yourself. The SerializedProperty mechanism takes care of that. Though only for the same type of objects.

avatar image WinterboltGames Bunny83 · Aug 24, 2017 at 01:54 PM 0
Share

thanks for pointing that out!

2 Replies

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

Answer by Bunny83 · Aug 24, 2017 at 12:48 PM

That's not possible. Unity uses the SerializedObject class which represents the serialized data of a class instance or multiple instances. However when you have two ore more different classes they can't be edited together as their serialized data might not match.

Unity actually encourages to use object composition over inheritance. That means you could create a general SpaceShip class and each ship type will have that component attached. Any ship-type specific behaviour would simply be in a seperate behaviour. That would mean your "CaptureShip" class would also just derive from MonoBehaviour and you would attach it next to the SpaceShip behaviour.

Inheritance limit the flexibility and straight-jacket you. You should focus on actual "features" you want to implement. For example if you implement a "CaptureAbility" behaviour you can attach this component to any ship and give it the ability to capture other ships. Just think about a car. You can attach a tow hitch to a car so it now has the ability to tow a trailer. Using inheritance in such a case makes not much sense. Having a "Car" class and a derived "Cabriolet" and a derived "TwoCar" class would make it impossible to have a cabriolet with a tow hitch. If features are seperated into behaviours that's not a problem. The functionality of a cabriolet would just be encapsulated by a RagTop behaviour and the tow hitch has it's own behaviour. For certain management purposes those behaviours could implement interfaces when you need to handle certain aspects into a generic manner.

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
1

Answer by Vladerx · Apr 26, 2019 at 09:49 AM

In your case i would use RequireComponent like so:
.
.
.
[ Scripts folder ]

 // c#
 using UnityEngine;
 
 interface IHPSystem {
     void Hit( int damage );
 }
 
 public class SpaceShipSuper : MonoBehaviour
 {
     public int speed = 333;        
     public Color color = Color.red;
 }
 
 [RequireComponent( typeof( SpaceShipSuper ) )]
 public abstract class SpaceShip : MonoBehaviour, IHPSystem
 {
     public int health = 200;
 
     public void Hit( int damange ) 
     {
         this.health -= damange;
     }
     public void Apply()
     {
         SpaceShipSuper superS = GetComponent<SpaceShipSuper>();
         GetComponent<Renderer>().material.color = superS.color;
     }
 }
 
 public class SphereShip : SpaceShip {}
 public class CubeShip : SpaceShip {}
 public class CapsuleShip : SpaceShip {}

[ Editor folder ]

 using UnityEditor;
 using UnityEngine;
 using System.Collections.Generic;
 
 // Hide sub class inspector fields 
 [CustomEditor(typeof(SpaceShip) , true)]
 public class SpaceShipCustomEditor : Editor
 {
     public override void OnInspectorGUI() { }
 }
 
 [CustomEditor(typeof(SpaceShipSuper) , true)]
 [CanEditMultipleObjects]
 public class SpaceShipSuperCustomEditor : Editor
 {
     List<SpaceShip> spaceShips;
     List<SpaceShipSuper> superSpaceShips;
 
     public void OnEnable()
     {
         spaceShips = new List<SpaceShip>();
         superSpaceShips = new List<SpaceShipSuper>(); 
 
         foreach( var target in targets )
         {
             SpaceShipSuper superS = ( SpaceShipSuper ) target;
             SpaceShip s = superS.GetComponent<SpaceShip>();
 
             spaceShips.Add( s );
             superSpaceShips.Add( superS );
         }
     }
 
     public override void OnInspectorGUI() 
     {
         if( this.DrawDefaultInspector() )
         {
             for( var i = 0; i < spaceShips.Count; ++i )
             {
                 SpaceShipSuper superS = superSpaceShips[ i ];
                 SpaceShip s = spaceShips[ i ];

                 // #1 update in editor ( my prefered method )
                 UpdateBaseClass( superS, s );

                 // #2 Or update inside space ship class
                 // s.Apply();
             }
         }
     }

     void UpdateBaseClass( SpaceShipSuper superS, SpaceShip s )
     {
         // regular variables like `SpaceShipSuper.speed` are updated automaticlly
         
         // do manual update if necessery 
         s.GetComponent<Renderer>().material.color = superS.color;
     }
 }





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

377 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 avatar image 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

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

Can a class's Editor call its component's Editor? Likewise, can a derived class's Editor call its base class's Editor? 1 Answer

Why is my propertydrawer being automatically disabled? 1 Answer

Cannot get a class to show up correctly in a custom inspector 0 Answers

Multiple Cars not working 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