Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Assassinbeast · Oct 10, 2014 at 03:19 PM · editoreditor-scriptingserializationscriptableobjectserializedproperty

How to work with custom class (ScriptableObject) SerializedProperty?

Hello, im trying to make customclasses that are derived from ScriptableObject work with SerializedProperty

Below, i have made a very minimal and simple example that works without deriving from ScriptableObject.

Human.cs

 public class Human : MonoBehaviour
 {
     public Weapon Weapon;
     public int Health;
 }


Weapon.cs

 [System.Serializable]
 public class Weapon
 {
     public int Damage;
     public int Weight;
 }


HumanEditor.cs

 [CustomEditor(typeof(Human))]
 public class HumanEditor : Editor
 {
     SerializedObject sObj;
 
     void OnEnable()
     {
         this.sObj = new SerializedObject(target);
 
         Weapon weapon = ((Human)target).Weapon;
         if (weapon == null)
             weapon = new Weapon();
     }
     public override void OnInspectorGUI()
     {
         this.sObj.Update();
 
         SerializedProperty sHealth = sObj.FindProperty("Health");
         sHealth.intValue = EditorGUILayout.IntField("Health", sHealth.intValue);
 
         SerializedProperty sWeaponDamage = sObj.FindProperty("Weapon.Damage");
         sWeaponDamage.intValue = EditorGUILayout.IntField("Damage", sWeaponDamage.intValue);
 
         SerializedProperty sWeaponWeight = sObj.FindProperty("Weapon.Weight");
         sWeaponWeight.intValue = EditorGUILayout.IntField("Weight", sWeaponWeight.intValue);
 
         this.sObj.ApplyModifiedProperties();
     }
 }

This works. But when i derived weapon from ScriptableObject, and change the OnEnable() function to:

     void OnEnable()
     {
         this.sObj = new SerializedObject(target);
         SerializedProperty sWeapon = sObj.FindProperty("Weapon");
 
         if (sWeapon.objectReferenceValue == null)
             sWeapon.objectReferenceValue = ScriptableObject.CreateInstance<Weapon>();
     }

Then i get an exepction saying its null. What should i do to make it work with ScriptableObject? The reason i need this, is because i want to use polymorphism and inheritance with Weapon.

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 frarees · Oct 11, 2014 at 02:18 PM

ScriptableObjects need to have an asset representation. So, you need to save the instance somewhere (e.g. save as subasset):

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(Human))]
 public class HumanEditor : Editor {
     
     void OnEnable () {
 
         serializedObject.Update ();
 
         SerializedProperty serializedWeapon = serializedObject.FindProperty ("Weapon");
         if (serializedWeapon.objectReferenceValue == null) {
             Weapon w = ScriptableObject.CreateInstance<Weapon> ();
                     // You can configure w.hideFlags here wisely
             AssetDatabase.AddObjectToAsset (w, target);
             serializedWeapon.objectReferenceValue = w;
         }
 
         serializedObject.ApplyModifiedProperties ();
     }
 
     public override void OnInspectorGUI () {
 
         serializedObject.Update ();
 
         SerializedProperty health = serializedObject.FindProperty ("Health");
         health.intValue = EditorGUILayout.IntField ("Health", health.intValue);
 
         SerializedObject weaponSerializedObject = new SerializedObject (serializedObject.FindProperty ("Weapon").objectReferenceValue);
 
         weaponSerializedObject.Update ();
 
         SerializedProperty weaponDamage = weaponSerializedObject.FindProperty ("Damage");
         weaponDamage.intValue = EditorGUILayout.IntField ("Damage", weaponDamage.intValue);
         
         SerializedProperty weaponWeight = weaponSerializedObject.FindProperty ("Weight");
         weaponWeight.intValue = EditorGUILayout.IntField ("Weight", weaponWeight.intValue);
 
         weaponSerializedObject.ApplyModifiedProperties ();
 
         serializedObject.ApplyModifiedProperties();
     }
 }
 

About inheritance and polymorphism, you should look at this.

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 Assassinbeast · Oct 11, 2014 at 02:58 PM 0
Share

Yes, i have done it in the OnInspectorGUI, but i just tried in the OnEnable(), and it still doesn't work :/

But thanks for answering and giving me the link

avatar image frarees · Oct 11, 2014 at 05:44 PM 0
Share

I've edited the answer

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

30 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

Related Questions

Dynamic serialized fields based on enum 0 Answers

How do I associate my custom object for the serializedProperty objectReferenceValue 1 Answer

What would be the best way to go about loading and saving a Serializable object in JSON with Unity? 0 Answers

[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer

How do you save changes made with custom editor? 3 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