- Home /
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?
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.
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.
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).If so you should use GetComponent to get that component.
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
This is what needed to know. The Editors folder was within a Plugins folder which lead to the error. Now everything works fine. ThanksNote that editor scripts have full access to all runtime scripts.
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!)
Your answer
Follow this Question
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