Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Forcing texture unload

Discussion in 'iOS and tvOS' started by mattimus, May 1, 2010.

  1. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    570
    I'm having some trouble unloading unused textures. I have a scene with a single GUITexture object that calls one of my 30+ tutorial images. Each tutorial image takes up the whole screen and needs to be uncompressed to avoid looking terrible.

    When I load a tutorial page, I do the following:

    Code (csharp):
    1.  
    2. GUITexture texObject;
    3.  
    4. void SetTutorialImage(string imageName)
    5. {
    6.   Texture tex = (Texture) Resources.Load("Tutorials/" + imageName);
    7.   texObject.texture = tex;
    8.  
    9.   Application.GarbageCollectUnusedAssets();
    10. }
    I've tried with and without manual gc, but either way my memory keeps inflating with every page I visit. Each tutorial is about .4 MB so if I try to go to all 30 tutorials, I quickly run out of memory and crash even on a 3gs. Is there some way to force the old texture to unload? If not, is there a better way to handle this situation?
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    there is no unload I see there, just load and then you assign it in which case it is used and will not be touched by the collector function at all
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,343
    You need a "Destroy (tex);" in there somewhere if you want to get rid of it.

    --Eric
     
    LiGo likes this.
  4. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    570
    Ah, that's what I was looking for, thanks. Somehow I got it in my head that replacing the texture with a new one would make the old one 'unused' and therefore get unloaded when I gc. Clearly not the case.
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    it normally is but there is one very special restriction: When you work with WWW.texture
    But they are explained in the corresponding documentation :)
     
  6. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    570
    Destroy() is giving me problems with Resources.Load(). The following code works great to fetch the texture the first time. However if I load up a different texture and then revisit the original one it can't be found.

    Code (csharp):
    1. GUITexture textureObj;
    2. void SetTexture (string textureName)
    3. {
    4.   Texture newTexture = Resources.Load("Textures/"+textureName");
    5.  
    6.  if (textureObj.texture != newTexture)
    7.  {
    8.    Destroy(textureObj.texture);
    9.    textureObj.texture = newTexture;
    10.  }
    11. }
    With this code, I tried the following:
    Code (csharp):
    1.   SetTexture("one");
    2.   Debug.Log(textureObj.texture.name);
    3.   SetTexture("two");
    4.   Debug.Log(textureObj.texture.name);
    5.   SetTexture("one");
    6.   Debug.Log(textureObj.texture.name);
    and it output:
    "one"
    "two"
    ""


    If destroy removes an asset permanently, how to I unload a texture but retain the ability to load it again later?
     
  7. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    570
    I've done a lot more experimenting and I still can't get Destroy() to play nice with my textures. Either they get destroyed and can't load again later or they remain in memory.

    The only way I've found to effectively unload a texture is to assign a new Texture2D(1,1) and then call Application.GarbageCollectUnusedAssets(). This works to unload the texture, but causes a long stutter while the garbage collection takes place.
     
  8. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    Actually you'll have to instantiate your texture into a new Texture2D to be able to Destroy it at will without any problem, I guess :idea:

    Texture class very different from Texture2D technically ;)
    Texture seems to be generic, and Texture2D the real container for 2D pictures. So storing your Resource into a Texture would only put a reference to this resource I guess. Which would explain your texture disappearing after that move.

    Use :

    Code (csharp):
    1. Texture2D tex = (Texture2D) Resources.Load("filenameblahblahblah", typeof(Texture2D));
    (C#)
     
  9. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    570
    Here's the latest code using Texture2D. It still only works once. After the first time viewing an image I can no longer load it.

    Code (csharp):
    1. public GUITexture guiTex;
    2. Texture2D emptyTex = new Texture2D (1,1);
    3. Texture2D tex;
    4.  
    5. public void SetTex(string texName)
    6. {  
    7.     //Assign GUITexture to be empty.  Removes GUITexture's reference to the old texture.
    8.     guiTex.texture = emptyTex;
    9.    
    10.     //Destroy the old texture
    11.     DestroyImmediate(tex, false);
    12.  
    13.     //Load new texture
    14.     tex = (Texture2D) Resources.Load("Textures/" + texName, typeof(Texture2D));
    15.        
    16.     //assign new texture to GUITexture
    17.     guiTex.texture = tex;
    18. }
     
  10. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    570
    Ok, I'm clearly doing something wrong here and I'm sick of shooting in the dark. I have attached a screenshot of the game running while viewing allocations. Each peak is when I swapped out a new texture. You can see that the old texture is not getting unloaded.

    I now have a folder full of textures in my Resources folder and a single GUITexture object in a scene. Ignoring any code posted above and starting completely from scratch, how would you load a new texture from the resources folder into the GUITexture and unload the old texture?
     

    Attached Files:

  11. Lukas H

    Lukas H

    Joined:
    Jan 16, 2009
    Posts:
    394
  12. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    570
    I set up a sample scene that illustrates the problem. I also submitted this to Unity as a bug report.

    The attached scene loads a different texture into a GUITexture with each clicked button.

    - If the onscreen Destroy toggle is off, the button simply replaces the GUITexture.texture. As you can see when you do this, the texture count goes up for each new loaded texture. It is not unloading the old texture. If you run this game through xcode with Allocations, you can see a spike on each texture load that doesn't ever go back down.

    -if the onscreen Destroy toggle is on, the button calls Destroy() on the texture before loading up the new one. Texture count decreases and the memory goes back down. However, once you navigate away from that texture you can not call it again. Using Resource.Load(texName) now returns a null value where the same command once called a texture.
     

    Attached Files:

  13. Lukas H

    Lukas H

    Joined:
    Jan 16, 2009
    Posts:
    394
    Exactly!

    The "solution" for the second load being null is to instantiate what comes out of the Resource.Load. The problem this brings is that you now have two times the texture loaded. One thats loaded by Resources.Load and the one thats instantiated and you are using. You destroy the second but the first keeps existing and you have no reference to it at all.

    Its a frustrating topic but a very important one for games. For some reason Unity is ignoring it completely.
     
  14. camille

    camille

    Joined:
    Feb 23, 2009
    Posts:
    47
    I'm having the exact same problem, for quite a while now. I submited a bug report ages ago, but no one seems to care about this...

    This is very frustrating.
     
  15. Johnzim

    Johnzim

    Joined:
    Jan 15, 2009
    Posts:
    10
    Yup, exactly the same problem here.

    Infuriating. I might just have to rebuild my entire How To section.
     
  16. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    570
    Further development. I tried a new method where I instantiated a GUITexture prefab and loaded the new texture into it. When it comes time to load a new image, I destroy the entire prefab and make a new one.


    Code (csharp):
    1.         Destroy(newGuiObj);
    2.         newGuiObj = (GameObject) Instantiate(guiObjPrefab);
    3.         GUITexture newGuiTex = (GUITexture) newGuiObj.GetComponent("GUITexture");
    4.         newGuiTex.texture = (Texture2D) Resources.Load(texName, typeof(Texture2D));
    The result? Complete fail.

    Destroying the instantiated prefab containing the only reference to the texture still does not remove the texture from memory. You have to explicitly destroy the texture, but in doing so you destroy the resource.
     
  17. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Just to keep you updated, this issue has been logged in our bug tracker. If you are interested in following this up, the case number is 344954.
     
  18. Lokken

    Lokken

    Joined:
    Apr 23, 2009
    Posts:
    436
    It's actually quite surprising something like this got into a release build to begin with.

    Resource loading and unloading is paramount on any platform, more-so on a mobile platform.
     
  19. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    570
    Thanks andeee. From the responses I'd say there are several users who will be watching this with keen interest :)
     
  20. Lukas H

    Lukas H

    Joined:
    Jan 16, 2009
    Posts:
    394
    No disrespect but that doesn't give me much more hope. I have made 12 bug reports in the past year, only 4 of them are closed and mostly because of the auto close when a new version is released.

    I have made a bug report of the very same issue we are talking about in this topic. Thats case 209502 Open 5/20/2009 2:24 PM.

    So yea, forgive me if I have absolutely no faith in the bug reporter or the people behind it anymore.
     
  21. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Hey Lukas,

    Sorry to see you disappointed and disillusioned with the way bugs get handled. That's my area of responsibility at Unity. I'll go through all of your bug reports and give you some feedback on the status of each. We are guilty of not always giving customers an email back when we close a bug (whether because we've fixed something or cannot reproduce something.)

    The specific problem in this thread certainly has been looked at - and I trivially reproduced what you sent in in bug 209502 a few moments ago. I'll glue 344954 to it so we can track both together.

    Waiting nearly a year for someone to respond to your bug report is really not acceptable. When you are faced with a problem like this it is hard to understand why it has not been made high priority. I suspect that there is a workaround that involves asset bundles (as per Joachim's comments some time back in a different thread.)

    I have asked one of my QA leads to go back through our bug database looking for open bug reports that contain projects and to make sure each of them has been processed fully.

    Kind regards,
    Graham
     
  22. camille

    camille

    Joined:
    Feb 23, 2009
    Posts:
    47
    Using asset bundles do solve the problem. But asset bundles use more memory / CPU than simply loading from resources. I had to remove almost half of my app's resources to get it working without running low on memory.

    As I said before in this post, this is quite frustrating. I hope it will be fixed in the near future, but it certainly won't make it in time for me (the project I work on has to be finished this friday).

    Unity has been overall a very good / pleasant to use / fast engine, but this problem is seriously very very bad.

    Anyway, I'm glad this problem is finally looked at ! Thanks for keeping us updated.
     
  23. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    570
    Thanks for looking into this, Graham. UT's response time to bugs has always been good to me in the past and I'm glad to see that trend continuing :)

    I would like to point out that asset bundles are only available on pro. As Lokken said, "Resource loading and unloading is paramount on any platform, more-so on a mobile platform." As such, there really should be some way to unload a texture that is friendly to all licenses.
     
  24. Johnzim

    Johnzim

    Joined:
    Jan 15, 2009
    Posts:
    10
    Thanks for the Update :)

    Look ing forward to a nice tasty fix. Although whether it'll come in time to save 'Nova Core Episode 1' is another matter!
     
  25. cedrico

    cedrico

    Joined:
    May 4, 2009
    Posts:
    40
    Hello guys,
    Just wanted to contribute to this thread, just to report we have the same bug on Unity Pro with a big project on a jungle involving images and videos that we get from mysql server. GUI texture are loaded in but memory is never reallocated to the OS once objects have been closed and destroyed. We sincerely hope you guys at the headquarters will have a bug fix pretty soon :)
    THX a lot.

    ps: where is that bug tracker thingie so i can follow up with events in case 344954 ?
     
  26. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can e-mail support@unity3d.com to get a status update on this.
     
  27. cedrico

    cedrico

    Joined:
    May 4, 2009
    Posts:
    40
    Hello Andeeee, thx for your reply, I'm a realistic man, so should we patiently wait till 3.0 or do you guys will make us a treat and find a way out shortly ?
    THX a lot
     
  28. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I think it's more likely a 3.0 thing than an interim release. However, UT will sometimes give users access to an advance version (at their own risk ;-) ) if a bug is causing major production problems. You would need to ask directly for this and it is done entirely at the QA deparment's discretion.
     
  29. TommiH

    TommiH

    Joined:
    Jan 14, 2008
    Posts:
    252
    By the way, does this problem apply only to textures or anything you load by Resources.Load()? We have quite a few audio clips we load. Do they also remain permanently in memory?
     
  30. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    I think this is specific to Texture2D and possibly TerrainData (these assets are permanently altered by changes in a script - this might be related to the problem).

    An update... although I don't get to see what the developers are doing directly, it seems that a plan has been formed to fix this problem and we now have guys working on it.
     
  31. Lukas H

    Lukas H

    Joined:
    Jan 16, 2009
    Posts:
    394
    Thats fantastic news!
     
  32. cedrico

    cedrico

    Joined:
    May 4, 2009
    Posts:
    40
    I confirm this piece of info, some ressources seem to have been assigned to fix it, which is very good news, thx Andeee for keeping us updated.
    THX you guys for the tech support
     
  33. Johnzim

    Johnzim

    Joined:
    Jan 15, 2009
    Posts:
    10
    Great news! Now I just have to work out whether I defer launch or follow through with my not-so-great workaround :/

    Unity 3.0 might be a little too far off to wait for.
     
  34. cedrico

    cedrico

    Joined:
    May 4, 2009
    Posts:
    40
    Hey guys, yes this is exactly where we stand Johnzim, we need the fix but Unity 3.0 isn't coming soon enough to save the day. If you honestly have the option to defer, then do it :) We didn't have this option!

    We really appreciate the tech guys for keeping us informed, hope they will come up with something to save our as...
     
  35. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    570
    I am also hoping for a pre 3.0 fix. My current game is in beta now and is planned to be my final game. If I'm at the end of a dev cycle for my current title and don't plan on making any future ones it seems a little silly to upgrade to 3.0, unless of course the new ToS forces it.
     
  36. Johnzim

    Johnzim

    Joined:
    Jan 15, 2009
    Posts:
    10
    Ugh, Released with a disclaimer that it shouldn't be played on iPhone 3G or below but someone already has bought it and slammed it in review for the crashes.

    If I had the option I would have held off but I risked launching with a disclaimer instead. Got Burned.

    Can't code around it. Can't sell without this bug being fixed. Utterly squashed. Not a happy Unity Developer here - this bug is a MASSIVE failure in an otherwise fantastic Mobile Development Environment.
     
  37. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    570
    I did find a somewhat usable workaround. Basically I assign a new texture to the GUITexture and call garbage collection to get rid of the old one. Normally GC causes a hiccup for a few seconds, so I changed the maximumDeltaTime to 0.1. There is a very brief hiccup when the GC is called, but it's better than inflating your memory to the point of crashing.

    Please note this will not be a viable solution for all your texture swaps. I only use this to load and unload fullscreen menu textures. When a player goes to a different menu screen, this gets called once to unload the old screen.

    (may contain errors, coded on the spot)
    Code (csharp):
    1. GUITexture guiTex; //The GUITexture that shows the texture we want
    2. Texture2D emptyTex = new Texture2D(2,2); //a blank texture
    3.  
    4. void Start()
    5. {
    6.   Time.maximumDeltaTime = .1f; //set max time a frame can take
    7. }
    8.  
    9. void AssignTexture(Texture tex)
    10. {
    11.   guiTex.texture = tex; //assign the texture to the GUITexture
    12. }
    13.  
    14. void RemoveTexture()
    15. {
    16.   guiTex.texture = emptyTex; //assign the blank texture to the GUITexture
    17.   Application.GarbageCollectUnusedAssets(); //GC to remove the old texture
    18. }
     
  38. TommiH

    TommiH

    Joined:
    Jan 14, 2008
    Posts:
    252
    Thanks, Mattimus! Assuming it works, it seems like the exact thing I need at least. (Though I'm confused why it wasn't mentioned before since it looks like the obvious right answer...)
     
  39. cedrico

    cedrico

    Joined:
    May 4, 2009
    Posts:
    40
    Hello guys!
    Just wanted to add that we tried your solution a while back and it didn't work for us. I need to say that we've had no news whatsoever for our case...for about two weeks now. We quickly had to find a way for our very patient client so what we did was:
    moving to gui3d texture (more ram eater friendly) buying xtra ram, new 3d graphic card with xtra ram and, which was a significant xtra cost for us :-( I bet we'll have to wait for Unity 3.0, with xtra cost again to compile and test our project, sincerely hoping it wil work and resolve our issue!
    Sad day for us.
    Good luck to all of you guys!
     
  40. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    I don't know if this will help on the iphone.

    But I did a dynamical asset bundle unload fixing a few weeks back for a customer project on the desktop.
    As some might know, asset bundles there suffer a similar problem when it comes to not get lost at all when being unloaded and destroyed.

    What I did there in the end was an enforced System.GC.Collect(100) basically in addition to the "regular steps" for freeing it.
    I'm aware that this is the bruteforce approach, but better brute than no force

    I've not tried if this works for this case with the textures but if not yet attempted you might want to try it.
     
  41. TommiH

    TommiH

    Joined:
    Jan 14, 2008
    Posts:
    252
    One more question on this: Do the textures loaded via Resources.Load unload even when a new scene is loaded?
     
  42. EricKramp

    EricKramp

    Joined:
    Apr 18, 2007
    Posts:
    54
    Thanks for the hint Mattimus, though I'm having trouble trying to get it to work, in spite of how straight forward it seems.

    I'm not seeing any hiccup at all when I try to call garbarge collection, which makes me wonder if it's happening correctly. And naturally my app is still crashing very consistently at the same place.

    Should there be a hiccup anytime I call that function? or only if it's doing something significant? I didn't implement the frame length cap portion.


    andeeee, I appreciate the updates, I was wondering if there was any news of a possible fix for those of us who won't be able to afford 3.0 for a while.

    Much appreciated!
     
  43. Big Pig

    Big Pig

    Joined:
    Feb 21, 2009
    Posts:
    92
    In 3.0 there's now Resources.UnloadUnusedAssets which solves this issue (didn't test it with textures, but it got rid of other assets that kept on building up in 1.7).
     
  44. CapnCromulent

    CapnCromulent

    Joined:
    Sep 7, 2010
    Posts:
    44
    I'm not having much luck with this, either, and I'm running 3.1.0f3. All I'm trying to do is Resource.Load a handful of textures, the unload them with Resource.UnloadUnusedAssets when I'm done displaying them.

    Here's the relevant code:

    Code (csharp):
    1.    
    2.         Object[] textures;
    3.     GUITexture curTexture;
    4.     int curSlide;
    5.  
    6.     void OnEnable()
    7.     {
    8.         textures = Resources.LoadAll("UI", typeof(Texture2D));
    9.         curTexture = guiTexture;
    10.        
    11.         curSlide = 0;
    12.         if (textures.Length > 0)
    13.         {
    14.             SetSlide(curSlide);
    15.         }
    16.     }
    17.    
    18.     void OnDisable()
    19.     {
    20.                 // Want to remove all references to the texture, yet the editor player complains
    21.                 // "trying to set a null texture on a GUITexture"
    22.         curTexture.texture = null;
    23. #if false
    24.         for (int i = 0; i < textures.Length; ++i)
    25.         {
    26.             Destroy(textures[i]);
    27.         }
    28. #endif
    29.         textures = null;
    30.        
    31.         Resources.UnloadUnusedAssets();
    32.         System.GC.Collect();
    33.     }
    34.    
    35.     void SetSlide(int index)
    36.     {
    37.         curTexture.texture = (Texture2D)textures[index];
    38.     }
    39.  
    When I run on an iPhone, I'll see messages at the appropriate place:

    However, the Insturments Allocation Monitor shows that my memory footprint never decreases when UnloadUnusedAssets is called.

    If I try to Destroy the texture (in the #if block), I can't reload the resource again, much like Mattimus's problem. When I run in the editor, it warns me that "Destroying assets is not permitted to avoid data loss." I suspect that Destroy is actually destroying the original asset, preventing it from being reloaded again.

    Anyhow, what's the right way to do this? I think I'm following the scant documentation closely, but my memory usage just keeps getting higher.
     
  45. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,577
    anybody was able to solve the texture unloading problem?
    We are on unity 3.3 and I don't see this working properly yet.

    we can't do a GUITexture.texture=null and Destroy(GUITexture.texture) wont' allow to reload it. Memory doesn't seem to be reduced either and the iOS is crashing on me.

    Any ideas?
     
  46. marjan

    marjan

    Joined:
    Jun 6, 2009
    Posts:
    563
    Well the following works for me under the following conditions:
    - The Texture is located in the Ressources Folder.
    - The Texture is not connected to any variable of any script.


    Code (csharp):
    1. public function loadATexture() {
    2.         if(!gameobjectMaterial.GetTexture("theTextureNameToLoad")) {
    3.           Debug.Log("Unity3d --> load Texture");
    4.           var contrastTexture : Texture2D;
    5.           contrastTexture  = Resources.Load("theTextureNameToLoad", Texture2D);
    6.           gameobjectMaterial.SetTexture("_TheTextureNameInShader", contrastTexture);
    7.     } else {
    8.         Debug.Log("Unity3d --> Texture was already loaded, don´t load again");
    9.     }
    10. }
    11.  
    12. public function unLoadATexture() {
    13.     gameobjectMaterial.SetTexture("_TheTextureNameInShader", null);
    14.     Resources.UnloadUnusedAssets();
    15. }
    I have no problems setting the Texture to null as you can see. Works on Device without error.
    One way that might lead to not being able to unloud is, you have the texture already connected to some input. Maybe in an Array or Variable, so you can acces it later to load? Well, if such a reference exists, you cannot unload the Ressource, because it is still being used.

    And i think you need to use the Ressources way. Think of it as getting it from nowhere, release it to nowhere.


    To be honest i did not check if memory is going down when i call this. Didn´t check that in xcode.
    But the unloading and nulling works without error here.
     
  47. jite

    jite

    Joined:
    Aug 19, 2012
    Posts:
    6
    It works. But I think it may be better, probably some actions is redundant, may be something missing. Please advice if you have ideas.
    Code (csharp):
    1.  
    2.     /// <summary>
    3.     /// === Works only for textures previously loaded from Resources (see Resources.Load()) ===
    4.     /// Finds all scene's Materials, looking for textures with names from 'texturesNamesToUnloading_', removes founded textures from materials
    5.     /// and try to unload them. After this it finds all scene's Texture2D objects and also try to unload them
    6.     /// </summary>
    7.     public static void TexturesForcedUnloading(List<string> texturesNamesToUnloading_)
    8.     {
    9.         bool isVerbose = true;
    10.  
    11.         Material[] allResMats = Resources.FindObjectsOfTypeAll(typeof(Material)) as Material[];
    12.         if(allResMats != null)
    13.         {
    14.             List<Material> matsToTextureDelete = new List<Material>();
    15.  
    16.             foreach(Material m in allResMats)
    17.                 if(m != null  m.mainTexture != null  texturesNamesToUnloading_.Contains(m.mainTexture.name))
    18.                     matsToTextureDelete.Add(m);
    19.  
    20.             if(matsToTextureDelete.Count > 0)
    21.             {
    22.                 foreach(Material m in matsToTextureDelete)
    23.                 {
    24.                     if(m.mainTexture == null)
    25.                         continue;
    26.  
    27.                     if(isVerbose)
    28.                         Debug.Log(string.Format("Remove texture '{0}' from material '{1}'", m.mainTexture.name, m.name));
    29.  
    30.                     Texture textureToUnload = m.mainTexture;
    31.                     m.mainTexture = null;
    32.  
    33.                     if(isVerbose)
    34.                         Debug.Log(string.Format("Try to unload <Texture> '{0}' (after remove from material)", textureToUnload.name));
    35.                     Resources.UnloadAsset(textureToUnload);
    36.                     textureToUnload = null;
    37.                 }
    38.             }
    39.         }
    40.  
    41.         Texture2D[] allResTextures = Resources.FindObjectsOfTypeAll(typeof(Texture2D)) as Texture2D[];
    42.         if(allResTextures != null)
    43.         {
    44.             List<Texture2D> texturesToUnload = new List<Texture2D>();
    45.             foreach(Texture2D t in allResTextures)
    46.                 if(t != null  texturesNamesToUnloading_.Contains(t.name))
    47.                     texturesToUnload.Add(t);
    48.  
    49.             if(texturesToUnload.Count > 0)
    50.             {
    51.                 foreach(Texture2D t in texturesToUnload)
    52.                 {
    53.                     if(t == null)
    54.                         continue;
    55.  
    56.                     if(isVerbose)
    57.                         Debug.Log(string.Format("Try to unload <Texture2D> '{0}' (as is)", t.name));
    58.                     DestroyImmediate(t, true);
    59.                     Resources.UnloadAsset(t);
    60.                 }
    61.             }
    62.         }
    63.  
    64.         Resources.UnloadUnusedAssets();
    65.         System.GC.Collect();
    66.     }
    67.  
    68.  
     
  48. LiGo

    LiGo

    Joined:
    Jul 21, 2016
    Posts:
    1
    Thanks a lot
     
unityunity