Author Topic: UITexture doesn't show when programmatically created  (Read 5617 times)

DZP

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 17
    • View Profile
UITexture doesn't show when programmatically created
« on: June 06, 2014, 09:33:14 AM »
I have a UITexture in my scene with no variables linked up to it. During runtime, I programmatically create a texture and assign it to the UITexture. I can see the texture that is created and see it linked up in editor. However nothing displays in my scene. I have tried changing the main texture and creating a new material.

Here is my code for setting the texture
  1. public void SetTextureTo(Texture2D tex) {
  2.  
  3.                 /*
  4.                 Material newMat = new Material(Shader.Find("Unlit/Transparent Colored"));
  5.                 newMat.mainTexture = tex;
  6.                 _tex.material = newMat;
  7.                 */
  8.                 _tex.mainTexture = tex;
  9.  
  10.                 _tex.width = tex.width;
  11.                 _tex.height = tex.height;
  12.                 _tex.MarkAsChanged();
  13. }

Interestingly enough, if I call _tex.panel.Refresh(); after the method then the UITexture displays, but I get a null reference exception because _tex.panel is null (I don't have it managed by a panel). If I have a panel for it or create one through CreatePanel() then the error disappears but the image doesn't show again! So really, I can't get my image to display without causing an error  :o

I am running off the latest version of NGUI on Unity 4.3

Any suggestions?

Thanks
« Last Edit: June 06, 2014, 09:46:47 AM by DZP »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture doesn't show when programmatically created
« Reply #1 on: June 06, 2014, 09:25:43 PM »
That's all you need to do. Have a look inside DownloadTexture script for an example. I just double-checked it and it works as expected. My guess is that you're trying to set the texture in Awake() or some other illegal place. Example the DownloadTexture script for a proper example.

DZP

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 17
    • View Profile
Re: UITexture doesn't show when programmatically created
« Reply #2 on: June 24, 2014, 03:32:54 AM »
That was the case, thanks for getting back to me. Is there a list of illegal places to do operations like this documented anywhere so I don't lose time in the future? Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture doesn't show when programmatically created
« Reply #3 on: June 24, 2014, 04:53:44 AM »
No, it's just one of those things you learn as you continue using Unity.

Awake() is only for initializing local values. It's basically the constructor. You should never be referencing other components in Awake().