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
3
Question by vexe · Feb 16, 2014 at 04:23 PM · editoreditor-scriptingserializedproperty

Do I need to use SetDirty in my editors if I'm using SerializedProperties?

I've been making tons of custom editors. At first I started using target only as SPs was a pain to work with - but then I realized the benefits SPs give, and managed to get along.

But at no point of time, I used SetDirty - I still see it in some editor scripts, but don't really know why they ever need it.

The docs says that you need to SetDirty on a prefab for ex, if you change a value in one of its MonoBehavoiurs for it to be saved to disk.

I haven't found any need to do that. I could change a value in a prefab's MB, enter/exit play mode, exit/re-enter Unity - the value is still there.

So what's the use of SetDirty, and when do I ever need it? Is this some legacy thing that was used when dealing with target directly?

Thanks!

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
7
Best Answer

Answer by MakeCodeNow · Feb 17, 2014 at 02:44 AM

SetDirty is needed if you change values without using SerializedProperty. Pretty much anywhere you'd need to add a manual call to Undo.Record, you'd want to call SetDirty.

As an example, I have some ScriptableObject subclasses with accessor properties that modify private, serialzied members. I have to use SetDirty inside these accessors.

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 vexe · Feb 17, 2014 at 04:02 AM 0
Share

Thanks for your answer.

Indeed, with SPs I didn't seem to need to call SetDirty. But in the doc they mentioned that internally they do call SetDirty for us, so why would they ever expose it to us then?

Well, I made some tests. First a prefab with an $$anonymous$$B, I modified some of its values via its default inspector, enter/exit play mode, restarted unity, the value persisted. no need for SetDirty, But then @Jamora mentioned to me that that's because I was modifying the value via the defauilt inspector. So I tried modifying it directly (just like in the doc example) - it persisted after I enter/exit play mode, but not after a Unity restart! "because it was only loaded in memory, not saved to disk" - Jamora. I tried SetDirty and indeed it did persist after a Unity restart.

"Pretty much anywhere you'd need to add a manual call to Undo" - You mean Undo.Record? or any of the undo methods? How about Undo.DestroyObjectImmediate? how would I SetDirty after that? (Since the object is gone, destroyed?)

avatar image MakeCodeNow · Feb 17, 2014 at 05:35 PM 0
Share

Yes, I meant Undo.Record. I'll update the answer with that.

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

19 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

Related Questions

Dynamic serialized fields based on enum 0 Answers

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

How to initialize array[][] via SerializedProperty? 1 Answer

How to work with custom class (ScriptableObject) SerializedProperty? 1 Answer

What would be the best way to go about loading and saving a Serializable object in JSON with Unity? 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