Author Topic: [SOLVEDISH] BUG(?): UITexture sharing material-ref when GameObject duplicated  (Read 7484 times)

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
    Hola!

    Just found a strange behaviour... If I instantiate a gameObject @runtime (same as duplicate, CTRL+D, in editor) which has a "Simple Texture" (UITexture) attached, any changes made to the UITexture.mainTexture on a duplicate will affect all other duplicates' textures (you with me? :) ).

    Basically:
    • Create a "Simple Texture" with a UITexture.
    • Duplicate multiple times (@runtime) using GameObject.Instantiate(gameObjectWithUITexture) as GameObject;
    or just CTRL+D on the gameObject.
Change mainTexture (from code) of any of the instances, see how the texture changes for ALL the instances.

Now, this is not my area of expertice, but I found that the material (mMat in UIWidget) is not being null'd when duplicated, thus all duplicates reference the same material! I fixed it quickly by setting UIWidget.material to null in Awake() in UITexture, but I sense there might be something I'm missing about you caching the material in the first place (to tired to find out)...

Anyways, without rabbling about stuff I don't know, this is my quickfix (in UITexture):

  1. override protected void Awake(){
  2.         base.Awake();
  3.         material = null;
  4. }
  5. //This will make it create a new instance next time mainTexture is set for the duplicated object
  6. [mainTexture...]
  7. if (mMat == null)
  8. {
  9.         mDynamicMat = new Material(shader);
  10.         mDynamicMat.hideFlags = HideFlags.DontSave;
  11.         mMat = mDynamicMat;
  12. }
  13. [...]
  14.  

This will make sure that the material is allways new'd for each new UITexture.

Sorry for not looking into this more thoroughly before posting, just felt lazy today. :(
« Last Edit: April 26, 2013, 07:30:52 AM by helmesjo »

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
bump

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Does this have any negative side-effects, such as textures disappearing on play?

My guess is that it causes an obvious issue when you specify a material for UITexture rather than a texture (which you can do).

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Nah, not that I've noticed. It works as intended as far as I know with my "fix".

Not really sure what you mean, and that you understand what I did... Problem was that I duplicated a UITexture @runtime, and when changing mainTexture of any of the duplicates caused ALL duplicates to change TO THE SAME TEXTURE. As I said, this is not my area of expertice but I found that forcing the material to reinstantiate for each new UITexture fixed the problem... :)

Now, enlighten me! ;)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
You are clearing the material to null. This works fine if you specified a texture, but as I said -- what if you specified a material instead (for example, a bump mapped quad?) Then as soon as you hit Play, the material is gone, and the UITexture will draw nothing.

Duplicating a UITexture issue you noticed is fixed as soon as you hit Play as it resets all material references.

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
I see what you mean with my quickfix destroying the ability to set the material instead, but I'm not sure if you understand the issue I'm having (or is it me not understanding that you understand me? Oo)...

You say that my issue with duplicating UITextures is "fixed as soon as I hit play", but as I've tried to explicitly point out I am duplicating UITextures @RUNTIME (using Instantiate), thus AFTER I've hit play...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Ah... runtime. Why not keep the prefab with no texture, and only set the texture after instantiation?

helmesjo

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 116
    • View Profile
Yep, leaving the texture blank also worked :) Will keep that in mind!

TY!