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 Croolsby · Dec 30, 2015 at 03:34 PM · assetdatabaseondestroy

Detect when a component has been removed.

I have a component that generates a .asset file when it is attached. If it is removed, then the .asset file must also be deleted otherwise it will become an orphan.

I tried using OnDestroy(), but when switching from play mode or edit mode, OnDestroy() gets called on any component in the scene. I'm guessing Unity is serializing the edit mode objects so that when it exits play mode, it can revert back to the original object's field values, but it has to delete the scene objects during the switch.

Maybe I should be managing my asset files in a different way?

Comment
Add comment · Show 5
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 brunocoimbra · Dec 30, 2015 at 11:49 PM 2
Share

I don't know why are you generating .asset on the fly, but...

After some research, I've done some testing and found out that OnApplicationQuit() is called before OnDestroy() ("But in the Unity $$anonymous$$anual is different!!" well, don't blame me).

So, you could use something like that:

     bool isQuiting;
 
     void OnApplicationQuit()
     {
         isQuiting = true;
     }
 
     void OnDestroy()
     {
         if (isQuiting)
         {
             return;
         }
 
         // Destroy stuff
     }
 
avatar image Croolsby brunocoimbra · Dec 30, 2015 at 11:58 PM 1
Share

Thanks. I'm generating .assets because my component needs access to some data structure that is usually unchanging but might sometimes change. I want it to survive between loads and unloads, so I'm storing it in a .asset. Writing to .asset only happens when the user saves the scene or project.

avatar image brunocoimbra Croolsby · Dec 31, 2015 at 12:27 AM 0
Share

I see, but why not go with DontDestroyOnLoad to persist between scenes and serialization to survive on loads and unloads? I am new to Unity, but I never saw that way of managing data until now.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Croolsby · Jan 07, 2016 at 10:38 AM

Using Application.isPlaying and Application.isEditor is not sufficient because removing a component and switching from edit mode to play mode both make isPlaying == false && isEditor == false.

The solution is to use EditorApplication.isPlayingOrWillChangePlaymode. Then you know a component is being removed (or GO destroyed) by the user in edit mode if isEditor && !isPlaying && !isPlayingOrWillChangePlaymode.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Only PreProcess assets on first import. 1 Answer

Is there a way to build Addressable Assets outside of the Editor? 0 Answers

Instantiate, problem on transform position 1 Answer

When looping through assetbundle assetdatabase how do you reference a material object 0 Answers

Proper way to refresh asset 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