Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: helmesjo on March 26, 2013, 06:12:45 PM

Title: [SOLVEDISH] BUG(?): UITexture sharing material-ref when GameObject duplicated
Post by: helmesjo on March 26, 2013, 06:12:45 PM
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. :(
Title: Re: BUG(?): UITexture sharing material-ref when GameObject duplicated @runtime
Post by: helmesjo on April 10, 2013, 08:16:29 AM
bump
Title: Re: BUG(?): UITexture sharing material-ref when GameObject duplicated @runtime
Post by: ArenMook on April 10, 2013, 03:03:14 PM
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).
Title: Re: BUG(?): UITexture sharing material-ref when GameObject duplicated @runtime
Post by: helmesjo on April 11, 2013, 04:32:36 AM
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! ;)
Title: Re: BUG(?): UITexture sharing material-ref when GameObject duplicated @runtime
Post by: ArenMook on April 11, 2013, 08:38:27 PM
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.
Title: Re: BUG(?): UITexture sharing material-ref when GameObject duplicated @runtime
Post by: helmesjo on April 12, 2013, 04:39:21 AM
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...
Title: Re: BUG(?): UITexture sharing material-ref when GameObject duplicated @runtime
Post by: ArenMook on April 12, 2013, 06:49:53 AM
Ah... runtime. Why not keep the prefab with no texture, and only set the texture after instantiation?
Title: Re: BUG(?): UITexture sharing material-ref when GameObject duplicated @runtime
Post by: helmesjo on April 13, 2013, 09:44:21 AM
Yep, leaving the texture blank also worked :) Will keep that in mind!

TY!