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 McGravity · Sep 27, 2017 at 05:51 PM · editor-scriptingeditorwindowselection

How to display currently selected GameObject in hierarchy in custom EditorWindow?

Hello,

when having the Animator window open and selecting a GameObject in hierarchy which has an Animator component attached the AnimatorController opens up in the Animator window. I have a custom EditorWindow to display behaviour trees and want to recreate the same behaviour.

I attached the method to display the beahviour tree at a delegate:

 Selection.selectionChanged += OpenSelectedObjectInEditorWindow;

In this method I'd need to do something like this:

 BehaviourPlayer player = Selection.activeGameObject.GetComponent<BehaviourPlayer>();

This isn't possible though, as BehaviourPlayer isn't an editor script. Thus I get an error:

The type or namespace name `BehaviourPlayer' could not be found. Are you missing an assembly reference?

Making it one gives the problem that I can't attache it to a GameObejct anymore. I seem to be stuck here. Is there any solution to this?

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

Answer by Bunny83 · Sep 28, 2017 at 11:07 AM

I don't quite get the question here.

 Selection.activeGameObject

This IS the current selected gameobject. EditorWindows are editor scripts and they are not related to any GameObject at all.

In the case of the "AnimationEditor" window, it looks for a runtime component that has to be either the Animation component or an Animator component.

What's the purpose of your EditorWindow? Does your editor window require / expect any certain component on the selected gameobject? If so you should use GetComponent to get that component. Keep in mind that runtime scripts (aka MonoBehaviours) must not be inside an editor folder as they are runtime scripts which are attached to gameobjects.

Note that editor scripts have full access to all runtime scripts. Of course if you placed a runtime script in a namespace you have to add a using statement at the top or use the full classname including it's namespace.

Comment
Add comment · Show 4 · 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 Bunny83 · Sep 28, 2017 at 11:10 AM 0
Share

ps: You don't have to subscribe to the "selectionChanged" delegate as EditorWindows automatically get a message when the selection changed:

EditorWindow.OnSelectionChange

Just define a method like this in your editor window class:

 void OnSelectionChange()
 {
 }

and it will be called automatically when the window is open. The EditorWindow base class also receives some other $$anonymous$$essages which might be interesting.

avatar image McGravity · Sep 28, 2017 at 11:53 AM 0
Share

If so you should use GetComponent to get that component.

BehaviourPlayer is a component and that's exactly what I'm trying to do when you look at the second code line I posted. As far as I know you just can't do that as it is not possible to use Scripts outside the Editor folder (and vice versa).
avatar image Bunny83 McGravity · Sep 28, 2017 at 11:59 AM 0
Share

No, i've edited my answer. Read the last paragraph. Editor scripts (which are actually inside the project as scripts inside an editor folder) have full access to any runtime script in the project.

If it doesn't recognise your classname you either have:

  • misspelled the classname

  • or you have placed the class in a namespace and you forgot to add a using statement

avatar image McGravity Bunny83 · Sep 28, 2017 at 04:05 PM 0
Share

Note that editor scripts have full access to all runtime scripts.

This is what needed to know. The Editors folder was within a Plugins folder which lead to the error. Now everything works fine. Thanks
avatar image
1

Answer by Runemark · Sep 28, 2017 at 01:28 PM

I'm working on a Visual Editor asset, and wanted to create something similar. It's not the same but my solution might help you. I wanted to open my custom editor window when I double click on a scriptable object asset I previously created to store the connections in the node based editor. This is how I achived this. The BehaviourPlayer in this case should be a scriptable object, that is created as an asset.

 public class BehaviourPlayerEditor : EditorWindow
     {
         [OnOpenAssetAttribute(1)]
         public static bool OnOpenAsset(int instanceID, int line)
         {        
             BehaviourPlayer obj = Selection.activeObject as BehaviourPlayer;    
             if (obj != null) 
             {                
                 BehaviourPlayerEditor window = EditorWindow.GetWindow<BehaviourPlayerEditor>();
                 window.Init(obj);
                 return true; //catch open file
             }        
             return false; // let unity open the file
         }
     
         BehaviourPlayer _selectedObject;
         
         public void Init(BehaviourPlayer obj)
         {
             _selectedObject = obj;
         }
     
         void OnGUI()
         {    
             // do whatever you want with the _selectedObject!
         }
     }

(If you are interested in the Visual Editor I'm working on, like and follow my facebook page, I'll post info about it soon!)

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

80 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

Related Questions

Select object in hierarchy before context menu appears? 0 Answers

Custom EditorWindow Scrollbars not working with GUILayout areas 1 Answer

Creating Editor 2D Animation Preview 2 Answers

Enum Value Changing At Runtime On A Prefab 1 Answer

Text in custom editor is displayed/rendered with boxes around characters... 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