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 Azazel_azton · Mar 23, 2016 at 03:12 PM · c#editoreditorwindowmethodcompile

Saved MethodInfo variable resets on compile

I'm trying to save a MethodInfo to a script through an editor window. When I create the script that should remember the method info it works well but whenever unity recompiles, (Run-Time, script has changed or unity restarts) the MethodInfo variable has been reset to null. I've tried serializing the class that saves it and making the MethodInfo a SerializedField,

What happens precisely: I select a Component belonging to a GameObject and a MethodInfo belonging to that component. Then create a new GameObject containing a script that has variables for GameObject, Component & MethodInfo. I then set those variables to the selected vars. Untill now it works. When unity compiles again the GameObject and Component are still saved but the MethodInfo has turned to null.

Any help would be greatly appreciated.

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
5

Answer by Bunny83 · Mar 23, 2016 at 05:05 PM

MethodInfo is not serializable by Unity. You would have to serialize it yourself. I recently created two helper classes to tackle this in Unity:

  • SerializableType // can serialize any System.Type object, even wild generic constructs

  • SerializableMethodInfo // can serialize a MethodInfo value.

SerializableMethodInfo depends on SerializableType so you would need both.

You can simply define a variable in a MonoBehaviour or ScriptableObject (which includes the Editor and EditorWindow class) like this:

 public SerializableMethodInfo method;

In MonoBehaviour derived classes the inspector will already create an instance for you. So you can simply assign a MethodInfo to the public "methodInfo" variable:

 MethodInfo info;
 method.methodInfo = info;

At other places like inside an editor / editorwindow you might want to simply create a new instance like that:

 MethodInfo info;
 method = new SerializableMethodInfo(info);

This should serialize it automatically into primitive types that Unity can serialize and also deserialize it automatically whenever Unity deserializes that field.

The class serializes the containing class type as "SerializableType" and the method name as string. In addition it also serializes the parameter types as a List of SerializableTypes. That way it should be possible to serialize most kinds of MethodInfos. At the moment i think it doesn't support generic parameters on the method itself (like GetComponent<T>()). For that we would probably need to include the generic type parameter definition and reconstruct the MethodInfo from the generic MethodInfo using the MakeGenericMethod method.

edit
Of course Editor and EditorWindow instances only hold information for the current session / viewed object. If you want information to persist you would have to create a ScriptableObject which you store as asset in your project which you can reload next time.

Comment
Add comment · Show 1 · 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 oroora6_unity · Oct 19, 2018 at 12:11 AM 0
Share

Hey bunny, your links are down

avatar image
0

Answer by Tarnthelos · Mar 23, 2016 at 04:47 PM

I'm not 100% certain, but I think in order for variables to be saved between sessions and compilation, they have to be written to a file and then read from later.

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

SerializedPropertyTable in custom editor window 1 Answer

When is the best time to call Repaint(); in an editor window 1 Answer

Undo.RecordObject and Constructor of Serializable Class 0 Answers

Detect Enter Key Event with GUILayout.TextField Focused 1 Answer

Multiple Cars not working 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