Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Flokky_ · Sep 18, 2017 at 07:40 PM · serializationcustom editor

Problems with saving data on GameObject using custom EditorWindow

I'm creating my RPG game and I'm just stuck with data saving problem.
I've got a class named Attribute with some private fields in it (such as it's ID, name and value) to expose some attributes of character such as Agility, Strength etc. It is marked as [Serializable]. And also it's private fields are marked with [SerializeField].
Class Attribute has a field Value which is type of class BasedValue that has protected float fields (they also marked as [SerializeField]): min, max and base.
Also, I've created a class CreatureLifeStatus (it extends MonoBehaviour) which has a field - List<Attribute>. And this field marked with [SerializeField].
The next class is CharacterEditor which is used for editing any character on scene. It extends EditorWindow. The first feature I've implemented for it - a simple editor of CreatureLifeStatus: alt text

It takes an object from ObjectField with label "Character object", as you can see. Then, it takes a reference to CreatureLifeStatus instance on that object. And, the last step, is getting all attributes from the list in it and editing them.

 //CreatureLifeStatus has an indexer to get attributes by ID
 BasedValue attributeValue = lifeStatus[id].Value; 
 attributeValue.MinValue = EditorGUILayout.FloatField("Min value:", attributeValue.MinValue);
 attributeValue.BaseValue = EditorGUILayout.FloatField("Base value:", attributeValue.BaseValue);
 attributeValue.MaxValue = EditorGUILayout.FloatField("Max value:", attributeValue.MaxValue);        

So, it's simply changes values in this instance.

The problem is: when I change values, using my custom editor window, I can see those changes on my object in automatically created editor: alt text

But. When (after doing some changes in my custom editor window) I enter the Play mode, all changes are discarded.
Otherwise, after using automatically created editor (on previous screenshot), changes will be saved.

I'm not sure I'm clearely expressed my problem - I'm asking for help here for the first time, but if any question appeares, please let me know :)

1.png (6.6 kB)
2.png (4.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 Flokky_ · Sep 24, 2017 at 10:05 AM

Well, to solve this problem I've desided to rebuild the whole system. And while rebuilding it, I've found the solution for my problem: before doing some changes to my object in EditorWindow, I should write this code:

 Undo.RecordObject(lifeStatus, string.Empty);
 EditorGUI.BeginChangeCheck();

And after doing some changes:

 if (EditorGUI.EndChangeCheck())
 {
     EditorUtility.SetDirty(lifeStatus);
 }

It works just as I need: all the changes I've made to my object are getting saved, and won't reset after entering PlayMode or after reloading Editor.

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

118 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

Related Questions

Serialize child ScriptableObject asset values in parent ScriptableObject asset. 0 Answers

What's a good way to handle 'serialization' of interfaces in Unity ? 8 Answers

Target field of a MonoBehaviour attribute ? 1 Answer

Unity3D ScriptableObject with UnityEvent not saving event parameter 0 Answers

ScriptableObject not Serializing? 0 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