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
2
Question by ChristopherCreates · Mar 13, 2016 at 02:31 PM · editorinspectorsavescriptableobjectsave data

Editor: How to save changes to a ScriptableObject loaded from Resources?

I'm making a complex custom inspector and I want to be able to save its settings to different profiles. So I've made a ScriptableObject to store the profiles. I can load the ScriptableObject just fine:

 var settings = Resources.FindObjectsOfTypeAll<SettingsProfiles>();
 if (settings != null)
     _savedSettings = settings[0];

With that, I can read in all the stored settings and I can make changes to them just fine. However, any changes I make are always lost. All of my searches seem to indicate that this is the answer:

 EditorUtility.SetDirty(_savedSettings);
 AssetDatabase.SaveAssets();

But that doesn't seem to do anything at all. I've been searching for hours but I can't find an answer.

Does anyone know how this is supposed to work?

Thanks!

Comment
Add comment · Show 1
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 troien · Mar 13, 2016 at 03:32 PM 0
Share

I'm absolutely not sure, but I think this might have to do with not registering for Undo. How do you edit values of your ScriptableObject?

If you modify it using a SerializedProperty there really shouldn't be any problem as Undo is handled by Unity.

If you set properties by casting it to your ScriptableObject and modify its properties/variables directly, then I believe you need to call Undo.RecordObject before making your changes, in order to let Unity actually set it dirty whenever you call SetDirty after changing. (so call Undo.RecordObject before changing, and EditorUtility.SetDirty afterwards). Also, if this is true then if you don't change anything, make a change and then manually save your project. The change should actually persist already ;)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Mar 13, 2016 at 03:18 PM

That should work. I recently made this ResourceDB which also uses those two lines to actually commit any changes. Keep in mind that when using SerializedObject / SerializedProperties (like the normal inspector does) it should commit the changes whenever you call ApplyModifiedProperties which you usually call at the end when done with the GUI that could possible change something. However if you don't use a SerializedObject you can ignore that.

Have you tried setting the asset serialization mode to "force text" and have a look at the actual serialized data? Are your changes visible there? When do you actually call SetDirty and SaveAssets? How do you actually change your class?

Ohh and finally the most important thing ^^: What data is it that you want to save? Keep in mind that ScriptableObjects are limited to the same serialization rules as any other object in Unity. So the type(s) you want to serialize have to be supported by Unity's serializer. A generic dictionary for example isn't supported.

Also keep in mind that assets can't reference things in the scene, only other assets. Things in scenes aren't always available, only assets are. So assets can only reference other assets but things in a scene can reference both, other things in the same scene as well as assets.

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

46 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

Related Questions

How should I serialize data that is also editable in the Inspector? 2 Answers

Prevent changes in ScriptableObject type Asset in Editor. Dont save it. 0 Answers

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

Why doesn't my ScriptableObject save using a custom EditorWindow? 3 Answers

Referencing / linking a .asset / .prefab file in another .asset / .prefab file programmatically. 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