Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Daniel-Everland · Feb 27, 2015 at 12:27 AM · c#editorinheritance

Inheritance And Editor Targets

The core of the problem is that I need an explicit reference to access variables off of the base class. This isn't an issue if I know exactly what class my custom editor is going to be used on, I'll just cast the target property inherited from Editor - no biggie.

Issues arise when my custom editor is used on derived classes. Consider the following cases.

 public class A
 {
     public int IntegerVariable;
 }
 public class B : A
 {
 
 }

We have two classes, Class A and Class B. The custom inspector is written for Class A, but since Class B inherits Class A we also use it for Class B and any other class that may inherit Class A. So imagine this scenario: We add Class B to a gameobject and use the custom editor tailored for Class A. Now we need an explicit reference to Class A so the editor is able to access IntegerVariable. You'd usually do something along the lines of

 private Target { get { return (A)this.target; } }

but since 'this' is now B, the above code will not work. How do you get a reference to Class A?

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

Answer by prankard · Feb 27, 2015 at 01:34 AM

I think you can get reference to A with your code. As casting B as A is valid.

Just to clarify with the following classes defined.

A.cs

 using UnityEngine;
 
 public class A : MonoBehaviour {
     public int IntegerVariable;
 }

B.cs

 using UnityEngine;
 
 public class B : A {
     
 }

AEditor.cs

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(A), true)]
 public class AEditor : Editor {
 
     public override void OnInspectorGUI ()
     {
         A a = (this.target as A);
         EditorGUILayout.LabelField ("Value: " + a.IntegerVariable);
     }
 }

Now. With this setup, if you wanted you wanted to make the class B inspector to duplicate A. Consider using this tag in A:

 [CustomEditor(typeof(A), true)]

This will make your Class B have the inspector of EditorA.

If you wanted to extend and add more functionality to the inspector in B. Then extend EditorA to reuse the code and base.OnInspectorGUI()

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 [CustomEditor(typeof(B))]
 public class BEditor : AEditor 
 {
     public override void OnInspectorGUI ()
     {
         base.OnInspectorGUI ();
     }
 }

Comment
Add comment · Show 1 · 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 Daniel-Everland · Feb 27, 2015 at 04:05 PM 0
Share

Ah, thanks man, I got it to work.

Just a heads up, in my case at least, creating BEditor actually causes the issue. I have the same setup as described in your answer, but for whatever reason this causes Target to grab B ins$$anonymous$$d of A.

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

20 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

Related Questions

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

Multiple Cars not working 1 Answer

Accessing methods from another script. C# 1 Answer

Set default editor values for public variables in derived classes 0 Answers

Distribute terrain in zones 3 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