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 Zale15Droid · Apr 28, 2020 at 08:51 PM · editor-scripting

Serializing UnityEvents housed in generic class from Custom Inspector

basically I have this issue, the Example class has its event and I want to display all the events in the List in the ExampleMono inspector. The issue arises because Example can't be turned into a SerializedObject and thus I can't get the serialized property for Update1D. It also can't be a scriptable object because I want the list to be dynamic and I don't want to keep deleting and adding objects to the project.

 public class Example //just holds Events and refrences 
 {
   [Serializable]
   public class UnityEventFloat : UnityEvent<float> { }
 
   public UnityEventFloat Update1D = new UnityEventFloat();
 }
 
 
 public class ExampleMono : Monobehavior //designed to hold a list of the Example class
 {
     List<Example> examples;
 }
 
 [CustomEditor(typeof(ExampleMono))]
 public class ExampleMonoInpector : Editor
 {
     override OnInspectorGUI()
     {
          SerializedObject mapObject = new SerializedObject(examples[0]);
          SerializedProperty tempProp = mapObject.FindProperty("Update1D");
          EditorGUILayout.PropertyField(tempProp);
     }
 }
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 Cassos · Apr 28, 2020 at 08:56 PM

Damn why can't people just formulate simple questions -.- I need more info, what is in the example class.

Could help you, just wrote a custom editor with serialized events too.

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 Zale15Droid · Apr 28, 2020 at 09:03 PM 0
Share

the Example class is really just a collection of UnityEvents, and the $$anonymous$$ono is just there to hold a list of them. there is a lot more to it but all of that works great, the issue is that you need to use the PropertyField to show UnityEvents and the Example class doesn't inherit from UnityEngine.Object so SerializedObject(examples[0]) can't work. trust me the rest of the script isn't relevant to the issue.

avatar image Cassos Zale15Droid · Apr 28, 2020 at 09:23 PM 0
Share

Yes I know, as I said I had my own trouble with that sh*t serializable in custom inspector system provided by Unity on my own. Did cost me 1 week of my lifetime.

So here is what you have to do:

     SerializedProperty examples;
 
     private void OnEnable()
     {
         examples = this.serializedObject.FindProperty("examples"); // Finds your list of example events
     }
 
     public override void OnInspectorGUI()
     {
         FallOnCollision t = (FallOnCollision)target; // Just example for a script, used an old one for this so don't worry, just important for the values
 
         serializedObject.Update(); // You have to do that before you do the next step to get the current values
 
         for (int i = 0; i < t.examples.Length; i++) // ALWAYS USE ARRAYS FOR SERIALIZED STUFF CAUSE UNITY WILL $$anonymous$$ETE LISTS - Don't know why, just do it that way!
         {
             SerializedProperty _e = examples.GetArrayElementAtIndex(i); // Gets the event out of the list of examples
 
             EditorGUILayout.PropertyField(_e); // Serialize the Event
         }
 
         serializedObject.Apply$$anonymous$$odifiedProperties(); // Does what it says. Applies the modified event values! ^^
     }

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

136 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 avatar image avatar image avatar image avatar image

Related Questions

Values changed with Editor Script don't get saved 1 Answer

Undo.RecordObject is too slow on large arrays, alternatives? 1 Answer

How to change settings on editor using Enum with an array? 0 Answers

Detect Play mode about to be entered 3 Answers

Bug with displaying components in Inspector after hideFlags.NotEditable? 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