Hello Guest

Author Topic: Unload Textures  (Read 18243 times)

Velvety

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Velvety Couch Games
Re: Unload Textures
« Reply #15 on: November 06, 2013, 10:37:16 pm »
No its the reload thing that is causing the issue. Its a bug in Unity which I thought was fixed in 4.2 but clearly not. The reload textures function is only Reloading the first visible texture, thats all.

You could try this instead, its untested but it may work better...
Code: [Select]
void ReloadTextures() {
Texture tex = renderer.sharedMaterial.mainTexture;
foreach (Texture t in Collection.inst.textures) {
renderer.sharedMaterial.mainTexture = t as Texture2D;
}
renderer.sharedMaterial.mainTexture = tex as Texture2D;
}

That change did not seem to fix the issue.  Do I need to loop through animation frames or something?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unload Textures
« Reply #16 on: November 06, 2013, 11:39:06 pm »
No, if you call the old "ReloadTextures" every frame it is likely to fix it. It looks like Unity only reloads the texture after reassigning when drawing it? I'm not sure of performance implications, but its worth trying.

undersan

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Unload Textures
« Reply #17 on: April 16, 2014, 08:37:22 pm »
I thought I'd revive this thread to talk about unloading textures from asset bundles.  I'm on Unity 4.3.4 and I test on Mac and iOS.

>Referencing the asset again should be enough to trigger Unity to load it in again
This is correct if the asset is from the Resources folder.

For a texture from an asset bundle, the behavior I see is that all references to that texture are set to null the instant you call Resources.UnloadAsset.  It's up to you to manually reload the texture and fix up references to the reloaded texture.  Here's what I'm doing:

Code: [Select]
Resources.UnloadAsset(myCollectionData.textures[0]);

// texture is now unloaded and related sprites can't be used (they will display as garbage on Mac and iOS)

// later...

// For now, I've hardcoded the texture I care about. In the future,
// I need a system to know the path for a collection's texture.
string path = "Units/1m_citizen/atlas0.png";

Texture reloadedTexture = assetBundle.Load(path, typeof(Texture)) as Texture;

// manually fix up collection's references to this texture
myCollectionData.materials[0].mainTexture = reloadedTexture;
myCollectionData.textures[0] = reloadedTexture;

So now, given that I need to manually reload collection textures, I find myself leaning towards a refcounting solution like jmcguirk proposed in his initial post.

Any thoughts on this?
« Last Edit: April 16, 2014, 08:42:44 pm by undersan »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unload Textures
« Reply #18 on: April 17, 2014, 02:49:47 am »
Hi,

Yeah once you bring asset bundles into the equation you'll be adding a whole other layer of complexity. Refcounting is one option, but you will need some form of a custom solution here.

TwisterK

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Unload Textures
« Reply #19 on: May 06, 2014, 04:37:39 am »
I'm trying to do this unload texture method, note that my atlas setting is set to PNG to save space, it did work and unload the texture successfully, but when I trying to reload it, it just show me white blob, is there any additional method I need to call?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Unload Textures
« Reply #20 on: May 06, 2014, 11:32:41 am »
Hi, this isn't officially supported on sprites if you already have sprites in the scene, but it can be done.

After you unload, call ResetPlatformData on the sprite collection.
When you need to reload, simply create a new sprite using this collection, or instantiate an existing one. That will force it to reload the texture.

TwisterK

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Unload Textures
« Reply #21 on: May 07, 2014, 08:34:50 am »
Hi, this isn't officially supported on sprites if you already have sprites in the scene, but it can be done.

After you unload, call ResetPlatformData on the sprite collection.
When you need to reload, simply create a new sprite using this collection, or instantiate an existing one. That will force it to reload the texture.

Thanks for the reply, I've tried it, it still showing white sprite. I trying to debug and check what wrong with it. It seem like after I've unload the texture, it won't get init anymore because the variable, materialInsts at the Init method won't become null and it simply skip all the init function. Please advise.

Code: [Select]
void Init()
{
// check if already initialized
if ( materialInsts != null )
return;
}

Update : Found the solution for this, I was unload and reset the wrong collection data all along, thanks for the help!

« Last Edit: May 08, 2014, 11:32:45 am by TwisterK »