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 the_genius · Sep 19, 2017 at 10:51 PM · editoreditor-scriptingserializationevents

UnityEvent Serialization Problem

Hello, I have an issue relating to using UnityEvents in a Custom Inspector.

I've managed to get it into some shorter code.

This is my script:

 using UnityEngine;
 using UnityEngine.Events;
 
 public class Test : MonoBehaviour {
 
     [SerializeField]
     public MyEvent myEvent;
 
     [System.Serializable]
     public class MyEvent : UnityEvent { }
 }

On its own the script works as expected. But when using the Custom Inspector script bellow there is a problem.

 using UnityEngine;
 using UnityEngine.Events;
 using UnityEditor;
 using UnityEditor.SceneManagement;
 
 [CustomEditor(typeof(Test))]
 public class TestEditor : Editor {
 
     public override void OnInspectorGUI() {
         GUILayout.Label("This is a label just to show the Custom Inspector is working.");
         Test myTarget = (Test)target;
         SerializedObject s = new UnityEditor.SerializedObject(myTarget);
         s.Update();
         EditorGUI.BeginChangeCheck();
         EditorGUILayout.PropertyField(s.FindProperty("myEvent"), true);
         s.ApplyModifiedProperties();
         if (EditorGUI.EndChangeCheck())
         {
             EditorUtility.SetDirty(target);
             EditorSceneManager.MarkAllScenesDirty();
         }
     }
 }

The problem is when entering a value for a static parameter into the Editor. For example, I can add a method to call such as 'GameObject > int layer' and enter a value like bellow.

alt text

Then I can click on another GameObject in the scene. When I go back to this script the value gets replaced with 0 (as bellow). I assume it is not being serialized somehow. But the default Unity Editor can serialize it so it must be possible. Is my code wrong?

alt text

Thank you

example1.png (8.8 kB)
example2.png (8.9 kB)
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
Best Answer

Answer by the_genius · Sep 19, 2017 at 11:16 PM

After some more googling I got there. It was just a little mistake in my code.

This line:

 SerializedObject s = new UnityEditor.SerializedObject(myTarget);

means I am creating a new SerializedObject and the editor is serializing the UnityEvent to that object. It is not saving it to the actual SerializedObject the editor uses.

So it should be:

 SerializedObject s = serializedObject;

Its been a long day :(

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

99 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

Related Questions

How do you save changes made with custom editor? 3 Answers

Preview procedural texture in scene without serializing it? 1 Answer

OnSerialize event 2 Answers

How do components(e.g transform) save data? 1 Answer

SerializedProperty with children class fields 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