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 Reid_Taylor · May 14, 2020 at 05:55 AM · scriptableobjectstaticargument

Static problem with opening custom editor window

Hi... Im creating a Node editor system and im trying to accomplish a shader graph like system where you create a scriptable object then double click on it to open editor. Everything is working so far except for I need to pass graph data from the scriptable object to editor window. But opening the editor window is static method and so I can't pass in (this) or (private Graph graph) it needs to be (static Graph graph) which won't work cause this variable needs to be specefic to the scriptable object. Heres the script

 [CreateAssetMenu(fileName = "New Graph", menuName = "Graph", order = 52)]
 public class Graph : ScriptableObject
 {
     public static Graph instance;
 
     public List<Node> nodes = new List<Node>();
 
     private void OnEnable()
     {
         instance = this;
         Debug.Log(instance.GetInstanceID());
     }
 
     [OnOpenAsset]
     public static bool OpenEditor(int instanceID, int line)
     {
         if (instanceID == instance.GetInstanceID())
         {
             WindowEditor.ShowWindow(instance);
         }
         return false;
     }
 }

Anyone have any ideas for somehow passing through specific Graph data? Thanks

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
0

Answer by Bunny83 · May 14, 2020 at 01:29 PM

Just forget about your whole pseudo singleton approach. The instanceID you get in the OpenEditor method is the actual instance you're interested in. You just use EditorUtility.InstanceIDToObject to actually get a reference to the object the instanceID belongs to.


Be aware that when you implement an Open Asset handler it will be called for "every" asset that is opened. The callback should return true when you actually want to handle the opening for that asset, otherwise you return false so other handlers have the chance of handling the open event. If none of the custom handlers returned true, Unity would do it's "normal thing" and open the asset with whatever is the default.


So your handler has to determine if you want to handle a certain asset or not based on the passed instanceID. Probably something like this:

 [OnOpenAsset]
 public static bool OpenEditor(int instanceID, int line)
 {
     Graph obj = EditorUtility.InstanceIDToObject( instanceID ) as Graph;
     if (obj != null)
     {
         WindowEditor.ShowWindow(obj);
         return true;
     }
     return false;
 }

Here we take advantage of the as cast to result in a value of null if the object can not be casted into a "Graph". So it doesn't matter if the object might be null in the first place or if it's just a completely different object. Whenever the object does not represent a Graph instance we just return false. However if the object is a Graph instance we can directly use that instance reference however you like.


I would recommend to add an ObjectField to your editor window where you display the instance this editor window is currently editing. It allows the user to quickly find that asset in the project and you could also use it to switch the graph you want to edit. Though if the double click works there's probably no reason for that.


Note that there are different approaches for how to open a custom editor window. When you use GetWindow you always only get one window instance. This is often the wanted behaviour. However in some cases it might be useful to allow multiple windows to be opened to edit different graph objects. In that case you can create your editor window manually by using "CreateInstance" and call "Show" on that instance. Of course if you want to support multiple windows, opening the same asset twice should not open two windows. So in this case a bit more work is necessary. However that wasn't the question here so I'll stop here ^^.

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

132 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 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

How does one get sprites into a static class? 1 Answer

Can I active some static function in ScriptableObject? 0 Answers

How do I have an EditorWindow save it's data inbetween opening and closing Unity? C# 5 Answers

How can i use static function in ScriptableObject? 3 Answers

Unity : Singleton ScriptableObjects resets after play 1 Answer


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