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
1
Question by passovla · Sep 09, 2019 at 03:58 PM · scriptableobjectimporting assetsnative pluginondestroy

How to destroy Scriptable Objects on Reimport

Hi,

I have a native plugin in c++ that creates and manages some allocated user data.

A reference to this data created is held by an scriptable object (which is supposed to be shared among several components) I am taking care of the memory management OnAwake/OnEnable and OnDestroy/OnDisable.

An instance of the scriptable object is created when I import some custom asset format. Everything works fine, until when I need to reimport this asset.

Basically, I create a new instance of the object in the body of the importer, and (I suppose) Unity internally replaces the old reference with the new one.

The problem is when this done, the OnDestroy (or OnDisable) is never getting called again, so my user data is never destroyed and it causes a small memory leak (it happens only on the editor, and when I reimport the asset, so it is not so critical)

as a workaround, I am keeping a static dictionary that holds the asset file name as key, and the scriptable object reference as value. When I detect that a reimported asset, I directly call the destructor of the user data.

besides being cumbersome, when the importer script itself is reloaded, the static data is cleared, so I still have this corner case memory leak.

ideally, i wanted to rely on the OnDestroy only, so how should I properly reimport the asset so that the SO gets destroyed?

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

Answer by YetAnotherKen · Sep 09, 2019 at 07:38 PM

I have noticed that Unity expects an asset not to be destroyed if you want to use it again. You can get around that with Object Pooling.

Another angle is to make sure you have destructors defined for the classes that contain data you want to re-instantiate later. This makes you rely on the garbage collector which is not ideal if you need speed. If you make the class inherit from IDisposable then you can use that to flag the objects for garbage collection and implicitly get the destructor to run.

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 passovla · Sep 10, 2019 at 12:56 AM 0
Share

If this is the case, I guess there is not much else to do (the whole importer thing is still experimental, so I think they will change the API in a near future)

even if they recicle the asset and OnDestroy is not supposed to be called on reimport, at least I would expect OnDisable to be called (since the SO was enabled before)

I don't think the destructor would be nice in my case, because I don't know when Unity is using local instances to copy the data around...

I was watching a talk about SOs and at some point it is mentioned "Resources.UnloadUnusedAssets" could be used for some cleanup. I am new to the API, so I'm not sure how good it is to use it (it would be nice a solution that does not interfere with other unrelated objects)

anyway, thanks. I will wait a bit more for other comments before closing this question.

avatar image YetAnotherKen passovla · Sep 10, 2019 at 02:36 AM 0
Share

What I mean is ins$$anonymous$$d of using Destroy I would deactivate the GameObject by using something like this:

 public void ClearFlora(List<FloraRecord> _floraRecords)
     {
         for (int index = _floraRecords.Count-1; index > -1; index--)
         {
             _floraRecords[index].floraItem.SetActive(false);
         }
     }

Then to reactivate them:

 public void RespawnFlora(List<FloraRecord> _floraRecords)
     {
         StartCoroutine(ProcessRespawn(_floraRecords));
     }
 
     IEnumerator ProcessRespawn(List<FloraRecord> _floraRecords)
     {
         if (meshObject.transform != null)
         {
             if ((meshObject.activeSelf) && (myChunk.initialSpawn))
             {
                 for (int index = _floraRecords.Count - 1; index > -1; index--)
                 {
                     _floraRecords[index].floraItem.SetActive(true);
                     _floraRecords[index].floraItem.transform.parent = meshObject.transform;
                 }
             }
         }
         yield return new WaitForEndOfFrame();
     }


You don't have to use a Coroutine like I have done in this example.

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

114 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

Related Questions

[CreateAssetMenu] For inherited ScriptableObjects 1 Answer

Interface on ScriptableObject 1 Answer

Skill system where every skill has unique functionality? 1 Answer

Assigning scriptable objects to dictionary through array? 0 Answers

Instancing ScriptableObject not saved in assets results in warning message 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