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 /
  • Help Room /
avatar image
0
Question by bbi-vsolobay · Mar 05, 2019 at 05:07 PM · editor-scriptingassetsapi

What does Object.DestroyImmediate(asset, true) do exactly?

The documentation wording is rather vague.

My assumption is that it would destroy the in-memory representation of an asset and free that memory.

I've seen speculations on the forums that it would physically delete the asset from the disk, but that seems highly unlikely, from the API design standpoint.

My use case: in the editor script (e.g., a custom MenuItem command), I'd like to load an asset using AssetDatabase.LoadAssetAtPath("Assets/SomeAsset.prefab"), perform certain operations on it, and then unload it completely to release the memory it occupies. There's a lot such assets to be processed, so releasing memory after we're done with them is crucial.

Is Object.DestroyImmediate(asset, true) the right choice for this task, and if not - what's the best way to unload assets in the editor script?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Owen-Reynolds · Mar 05, 2019 at 08:58 PM

The middle part of AaronB's answer is mostly correct. As the manual says, destroy has a delay before it does anything. Try this:

 Destroy(g);
 g.transform.position=Vector3.zero; // no error
 if(ggg==null) Debug.Log("null"); // does not print

 DestroyImmediate(g);
 g.transform.position=Vector3.zero; // error - the gameObject you are...

Also, google "Unity destroyImmediate" and you'll find this same Q with more discussion.

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 AaronBacon · Mar 05, 2019 at 07:07 PM

Not exactly, as shown in Unity's Section on Optimization in the docs, Unity will periodically remove anything from Memory that doesn't have any active object referencing it.


So from my understanding, the difference between Destroy and DestroyImmediate is that Unity will normally destroy the object, wait till the end of the frame update, then remove any references to the object in the scene. DestroyImmediate, on the other hand, will remove all references to the object immediately, not waiting for the end of the frame. As displayed in the documentation, it's not recommended to use at runtime, although there are occasions where it can be needed if you have to have an object destroyed before running further code.


So in your case, it probably wouldn't make much difference, so it's probably better to just go with Destroy, since DestroyImmediate can mess with the timings of other functions if overused.

Comment
Add comment · Show 1 · 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 xxmariofer · Mar 05, 2019 at 07:53 PM 0
Share

i have always thought that DestroyInmediate was just a destroy that forced garbage collector. unity is written in c++, with memory managed manually, so i think that the c++ allocated memory gets errased inmediatly but the c# warpping needs to wait for GC, but i would love any clarification since this is just my assumption.

avatar image
0

Answer by bbi-vsolobay · Mar 05, 2019 at 09:27 PM

To clarify, I'm interested specifically in Object.DestroyImmediate(object, bool) overload.

The difference between Object.Destroy() and Object.DestroyImmediate() is pretty clear from the documentation, and I can't use Object.Destroy() anyway, because it's an editor script.

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 Wappenull · Oct 27, 2020 at 08:25 AM

About Object.DestroyImmediate(asset, true)


The overload with 2 arguments would allowing destroy asset object from disk. Which is not you want. If you were to call DestroyImmediate(asset)on object returned from AssetDatabase.LoadAssetAtPath you would get error in editor saying that "destroying asset is prevented, use 2nd overload instead".

.

And that's what 2nd overload do.

.

Then what about correct way to modify prefab content while releasing it properly? See next topic below.


The correct way to modify prefab in editor script


The object returned from AssetDatabase.LoadAssetAtPath is surprisingly, not really a "source" prefab, it is library version of the prefab (the one that unity "imported" from source prefab on the disk)

.

So modifying it would require a roundabout to mark it dirty, then tell unity to save it, then have unity "flush" it back to source version in disk.

.

The very direct and correct way, with proper cleaning, is to use function from PrefabUtility. Read the indepth discussion here: FORUM LINK

 PrefabUtility.LoadPrefabContents
 PrefabUtility.SaveAsPrefabAsset
 PrefabUtility.UnloadPrefabContents

Or if you are already using 2020, unity has a IDisposable scope for you. (see example in API doc)

https://docs.unity3d.com/2020.1/Documentation/ScriptReference/PrefabUtility.EditPrefabContentsScope.html

TIPS:

While editing lots of prefabs, it would reasonable to wrap it around StartAssetEditing and StopAssetEditing so that unity stops AUTO import things while we modifying.

 AssetDatabase.StartAssetEditing( );
 // Modifying code
 AssetDatabase.StopAssetEditing( );

After editing, it would be very reasonable to call

 AssetDatabase.Refresh( );

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

116 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

Related Questions

Reimporting an asset from Editor Code 0 Answers

Calling Unity API Reference from MonoDevelop 2 Answers

ScriptableObjects inside another ScriptableObject not saving changes in Custom Editor Window 0 Answers

How can I get a sub asset? 1 Answer

Is it possible to reimport multiple assets via script? 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