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
2
Question by swquinn · Jun 06, 2011 at 03:10 AM · editorbuttoneditorwindowreturn

Return value from EditorWindow...?

I'm trying to take a value (or set of values, changes, etc.) from one EditorWindow and reflect those changes back to the editor and subsequently the object the editor is representing.

I feel like I'm missing something very fundamental and basic--and maybe its documented some place and I was just unable to find it. I've thumbed through the answers and glanced through some code but I didn't find what I was looking for. There doesn't seem to be any information that explicitly addresses this.

I suspect that I have to do one of the following:

  • Have a reference to the object be passed along between Editor and EditorWindow

  • Implement return callback (though I haven't seen this model used anywhere that I could tell).

  • ...or rely on an event driven model to send arguments between one window and another.

If anyone could help me out in understanding the basic principal behind communication between editors, editor windows, and the objects that they represent that would be much appreciated.

Specifically, what I have is this: a behavior which has inspector elements allowing me to open a preview window for a specific type of custom asset, where upon clicking "OK" the window will dismiss itself and whatever values were set or changed in the preview window will be applied to the behavior whose editor enabled the launch of the window in the first place.

 public class MyBehaviorEditor : Editor {
   ...
   public override void OnInspectorGUI() {
     ...
     EditorGUILayout.BeginHorizontal();
     EditorGUILayout.Space();
     if (GUILayout.Button("Load", GUILayout.Width(50))) {
       MyBehaviorLoaderWindow window = (MyBehaviorLoaderWindow)
               EditorWindow.GetWindow(typeof(MyBehaviorLoaderWindow));
     }
     EditorGUILayout.EndHorizontal();
   }
 }

and...

 public class MyBehaviorLoaderWindow : EditorWindow {
   ...
   public override void OnInspectorGUI() {
     ...
     if (GUILayout.Button("OK", GUILayout.Width(50))) {
       // What to put here to return a value back to MyBehaviorEditor, if anything?
     }
   }
 }

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

Answer by Bunny83 · Jun 06, 2011 at 04:43 PM

Well, you have to find your own way to handle such information exchange. The main problem is that a custom inspector and an editor window have some restrictions:

  • EditorWindows can't be modal, so the user can do other things in the background, like selecting another object.

  • Custom inspectors are created and destroyed automatically by Unity. If the user deselects the current object the editor instance may get destroyed. Therefore you can't rely on the fact that the inspector is still there or the selected object is still the same.

The best way would be to submit the selected object to the EditorWindow when it's opened. Save your own reference to the selected object in the editorwindow.

Do your changes to the object directly in the EditorWindow class.


Modal windows would be a nice feature but at the moment you can't do something like that.

ps. EditorWindows doesn't have a OnInspectorGUI method, only OnGUI like MonoBehaviours

Comment
Add comment · Show 2 · 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 swquinn · Jun 06, 2011 at 05:02 PM 0
Share

Thanks Bunny, I think the OnInspectorGUI in the EditorWindow was a copy and paste error on my part. Good catch, though.

As for submitting a reference to the selected object to the EditorWindow when its opened, I would assume I'd have to do something like (excuse the possible improper syntax, I don't have my Unity project files to reference in front of me):

 if (GUILayout.Button("Load", GUILayout.Width(50))) {
   $$anonymous$$yBehaviorLoaderWindow window = ($$anonymous$$yBehaviorLoaderWindow)
           EditorWindow.GetWindow(typeof($$anonymous$$yBehaviorLoaderWindow));
   window.SetObjectReference(target);
 }

As far as I can tell from looking at the reference documents and the API there is no way I can pass in the reference at the time of instantiation, it has to be done after the fact, correct?

I'm currently using C# as my language of choice, do you have any thoughts as to the use of delegates to handle "callback" functions from the EditorWindow itself? I suspect that while they may work in some cases they would suffer from the second point you made where an editor instance might be destroyed.

avatar image swquinn · Jun 06, 2011 at 05:04 PM 0
Share

As a quick aside, obviously on the EditorWindow, window, the object reference could be set up as a property so I could assign it using the assignment operator. But still the point stands.

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

2 People are following this question.

avatar image avatar image

Related Questions

Clicking on buttons in editor windows not working without a resize 0 Answers

Unity Factory Reset button not working 0 Answers

Find Objects with particular tag 2 Answers

Unity Editor Event System 1 Answer

Changes to Object made in custom Editor Window don't persist 0 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