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 idbrii · Nov 13, 2018 at 07:30 PM · editor-scriptingundodirty

Does Undo.RecordObject support marking ScriptableObjects dirty?

As far as I can tell it does not. Here's some editor code for removing weapons from a set:

 public class WeaponSet : ScriptableObject
 public class Weapon : ScriptableObject

 WeaponSet[] unreleased_sets = Resources.LoadAll<WeaponSet>(unreleased_weapons_path);
 foreach (WeaponSet weapon_set in unreleased_sets)
 {
     Undo.RecordObjects(weapon_set.m_WeaponsInSet, "Unlink unreleased weapon set");
     foreach (Weapon weapon in weapon_set.m_WeaponsInSet)
     {
         weapon.m_AssociatedSet = null;
         // Saving does not write changes to disk without SetDirty.
         //~ EditorUtility.SetDirty(weapon);
     }
 }
 AssetDatabase.SaveAssets();

I must use SetDirty for Unity to write these changes to disk. (Otherwise the changes are visible in editor, but lost when Unity is closed.)

SetDirty documentation says:

the only remaining situation in which you should use SetDirty is if you are modifying non-scene objects via some other means, and specifically do not want create an undo entry for your modification.

I want an undo entry (and RecordObject correctly makes an undo entry), so it sounds like I shouldn't use SetDirty. However, I don't understand how to get Undo to mark the ScriptableObject as dirty for me.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by pitchblende · Mar 16, 2021 at 08:44 AM

Sorry for waking up an old thread, but I think you have to do both when modifying a ScriptableObject via an Editor script:

     Undo.RecordObject(myThing, "Do my thing");
     myThing.value = 42;
     EditorUtilities.SetDirty(myThing);
 

In my experiments, I am only able to get the change to a ScriptableObject made via an Editor script to save to disk if I actually perform an Undo operation prior to the manual Save operation. Until then, the data just doesn't get written out.

Modifying the SO via the Inspector instead of an Editor script works as I'd expect - no need to Undo if changes are made there, and they appear on disk immediately following a Save.

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

Answer by Bunny83 · Mar 16, 2021 at 09:06 AM

Since the question got bumped already:

I'm pretty sure one of your issues is that you should use

 Undo.RecordObjects(weapon_set, "Unlink unreleased weapon set");

instead of

 Undo.RecordObjects(weapon_set.m_WeaponsInSet, "Unlink unreleased weapon set");

We don't know what "m_WeaponsInSet" is but it seems like it's a collection of some sort. So probably a List or Array. Though in that case your line of code would not compile at all since RecordObject can only be used on objects derived from UnityEngine.Object. Since your Weapon class is a ScriptableObject on its own, since you modify the Weapon instance you have to call RecordObject on that instance. Nested ScriptableObjects won't propergate the RecordObject down to other referenced assets. Each ScriptableObject is an asset on its own. So calling RecordObject on "weapon_set" would most likely don't do anything in your case since you do not modify any data in your weapon_set object. You do modify your individual Weapon instances which you do not register for Undo operation.


If you want to group all those individual Weapon changes into a single undo action, you should look into undo groups. You still need to register each weapon object but you can combine all those changes into a single undo group.

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

106 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

Related Questions

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

Why is play mode reverting my scriptableobject to a previous serialized state? 2 Answers

Undo SetSiblingIndex for root objects? 1 Answer

How to undo removing components in an editor extension? 0 Answers

Is there a way to register undo for all ColorField changes, or name the individual undos? 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