Author Topic: UITexture keeping reference of the texture  (Read 3865 times)

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
UITexture keeping reference of the texture
« on: November 22, 2012, 11:53:02 AM »
Hi Aren,

Is is really needed that UITexture keeps a serialized Texture field ?
I'd like to load the textures when I want using Resources.Load, but as they are serialized, they are loaded by Unity with the UITexture itself.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture keeping reference of the texture
« Reply #1 on: November 22, 2012, 10:52:45 PM »
UITexture references a texture. Why wouldn't it keep it?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: UITexture keeping reference of the texture
« Reply #2 on: November 23, 2012, 02:43:27 AM »
You could always make it programatically. I mean, Resouces.Load etc and instantiate and create an UI Texture in code. Then it won't be loaded by unity. You can just figure out the setup in the editor and just remove it again and put in the values in your code.

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: UITexture keeping reference of the texture
« Reply #3 on: November 23, 2012, 03:05:43 AM »
Well, I know all of these, but that's not my point.

I have all of my UI created in my scene with every textures associated etc...
Before bulding the game, I have an editor script that sets the mainTexture of a bunch of materials to null, so that when the game loads, the textures are not loaded in memory.

Like that, in the game, I'm calling Resources.Load whenever I want and keep track of the memory used by my game.
It's been working GREAT so far for everything atlas related etc... but not why UITexture that keeps reference to the texture, that's why.

And I don't really want to modify my script to go through my scene, search for every UITexture and remove the texture reference. It's way easier to just set the mainTexture of the material used by the UITexture to null.

Plus, if you have a material attached to the UITexture, why do you keep track of the texture ? Can't you just do material.mainTexture ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture keeping reference of the texture
« Reply #4 on: November 24, 2012, 01:55:51 AM »
Long story short: I had to do it this way to make textures stop disappearing when either returning from Play mode, or when scripts get recompiled.

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: UITexture keeping reference of the texture
« Reply #5 on: November 25, 2012, 06:38:56 AM »
lol, ok no problem. But it seems like an issue no ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture keeping reference of the texture
« Reply #6 on: November 25, 2012, 12:07:51 PM »
You're welcome to find a better solution :P I've banged my head against that wall for too long already...

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: UITexture keeping reference of the texture
« Reply #7 on: November 26, 2012, 04:38:52 AM »
héhé, I'll try when I have some time!

But if you failed, I will probably too :)

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: UITexture keeping reference of the texture
« Reply #8 on: December 06, 2012, 08:16:06 AM »
I took some time on this, and it seems there is a reference to the texture itself, in the UITexture script AND in the UIWidget script (because UITexture herits from UIWidget).

Do you think it's a good idea if I make UITexture herits only from Monobehavior and implements its own methods (even if it's kind of duplicating some of the UIWidget methods) ?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture keeping reference of the texture
« Reply #9 on: December 06, 2012, 10:37:03 AM »
Nope, because then it won't show up on GetComponentsInChildren<UIWidget>().

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: UITexture keeping reference of the texture
« Reply #10 on: December 07, 2012, 07:57:32 AM »
The texture might be referenced in a _UIDrawCall as well. try turning on geometry in the relevant panel and check in there.

nah0y

  • Sr. Member
  • ****
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 430
  • \o/
    • View Profile
Re: UITexture keeping reference of the texture
« Reply #11 on: December 10, 2012, 01:31:24 AM »
Well going, through the scene and searching for the GUID of the texture, it seems that the scene itself keeps a reference to the texture.
So maybe I need a custom script, that will go through the scene and remove those references.