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
0
Question by Matt1000 · Mar 12, 2017 at 03:18 PM · c#custom editorcustom inspectoreditor window

Calling a custom inspector from an editor window of an instance of a class

I am creating a basic node connector (pretty much like the animator). And when you click a particular node the inspector displays that node options. The Node is a class called Node with some data. Lets say name, ID and color.

 public class Node {
     //Constructor
     string name;
     string ID;
     Color color;
 }

All of these nodes are saved in a scriptableobject, in a list, and i access it through GUI Textures, but in the end i save my node (refernced) in a selectedNode variable. Apart from that i have a custom editor for nodes.

 [CustomEditor (typeof(Node))]
 public class NodeEditor : 
     //I'm not sure how to set this node either scince it is not target, it is not being selected
     Node node;
     public override void OnInspectorGUI ()
     {
         Debug.Log (node.name);
     }
 }

Now, i need whenever this variable is different from null, the inspector to display that particular node editor. I hope it is clear enough.

Comment
Add comment · Show 1
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 Matt1000 · Mar 12, 2017 at 06:07 PM 0
Share

Right now, the only way I found to "call" the inspecctor is to Selection.activeObject = selectedNode but i cannot cast Node to Object... Dead End.

1 Reply

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

Answer by Matt1000 · Mar 14, 2017 at 08:23 AM

I found a quite complex workaround... It seems that it is imposible to show something in the inspector if it isn't through Selection.activeObject. Actually i discovered that this is what the Animator wondow does. That's why when you click a State it takes you (in the project window) where the controller is... It makes a Selection.activeObject = StateObject.

So in order to achieve this effect i had to create a scriptableObject:

 public class NodeInspector : ScriptableObject {
     public Node selectedNode; //public so that it can be set through the main window
 }    

Just with the selected Node. Then, the editor script of this scriptableObject simply displays the options you want for each node. However, in order to access that specific scriptableObject (since it can only be created once), it must be in a Resources folder. I couln't find another way...

Editor script:

 [CustomEditor (typeof (NodeInspector))]
 public class NodeInspectorEditor : Editor {
 
     Node node; //The one whose options will be displayed
     NodeInspector selection;
 
     public void OnEnable () {
         selection = Resources.Load<NodeInspector> ("Node Inspector"); 
     }
 
     public override void OnInspectorGUI ()
     {
         node = selection.selectedNode; 
         //This is on the GUI methods so that if the selected Node changes, this will change too
         if (node != null) {
             //GUI Things
         }
     }
 }
 

And finally, so as to make this object the one you want the inspector to inspect you simply do nodeInspector = Resources.Load<NodeInspector> ("Node Inspector"); and Selection.activeObject = nodeInspector. And it finally works...

However, every time you chose a node it will make the project window go to this scrpitableObject's asset. And i didn't like it a thing. So, to avoid this i did set the selection, locked the inspector and set the selection to null. if the value changed it would simply unlock it.

 void Update () {
     if (checkSelectionChange) {
         if (Selection.activeObject != null) {
             ActiveEditorTracker.sharedTracker.isLocked = false;
             checkSelectionChange = false;
         }
     }
     
     if (selectedGUINode != null) {
         nodeInspector.selectedNode = selectedNode;
         Selection.activeObject = nodeInspector;
         ActiveEditorTracker.sharedTracker.isLocked = true;
         Selection.activeObject = null;
         checkSelectionChange = true;
     }
 }

And that's it

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to create Custom Inspector Labels and I get erros 0 Answers

Disable input to inspector drawn underneath 1 Answer

Show texture (image box) in inspector with custom editor [C#]? 3 Answers

Multiple Cars not working 1 Answer

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