Author Topic: Change UITexture runtime  (Read 3467 times)

mimmog

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Change UITexture runtime
« on: December 12, 2014, 09:45:09 AM »
I must change the texture of <UITexture> with an image that I download from web. The code is :

  1.                 userImg.GetComponent<UITexture>().material.mainTexture = SPFacebook.instance.userInfo.GetProfileImage(FacebookProfileImageSize.square);
  2.                 GameObject.Find("UI Root").GetComponent<UIPanel>().Refresh();
  3.  

It's right ? because the texture not change....
« Last Edit: December 12, 2014, 11:44:10 AM by mimmog »

NightPhoenix

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Change UITexture runtime
« Reply #1 on: December 12, 2014, 03:43:08 PM »
Try storing the image in a temp variable first and check if its null or not, like so:

  1.  
  2. Texture fbProfileImage = (Texture)SPFacebook.instance.userInfo.GetProfileImage(FacebookProfileImageSize.square);
  3. userImg.GetComponent<UITexture>().material.mainTexture = fbProfileImage != null ? fbProfileImage : placeHolderProfileImage;

"placeHolderProfileImage" would be your fallback image to display in the case that fbProfileImage does return null, be it because of a connection error or whatever the case may be.

Ferazel

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 2
  • Posts: 150
    • View Profile
Re: Change UITexture runtime
« Reply #2 on: December 12, 2014, 08:26:59 PM »
Don't put the new texture directly into the material's mainTexture as it will not trigger a rebuild of the draw call for the UITexture (which keeps a duplicate material on the backend). My recommendation would be to userImg.GetComponent<UITexture>().mainTexture = fbProfileImage;
Setting the UI Texture's mainTexture property should trigger the change properly.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change UITexture runtime
« Reply #3 on: December 12, 2014, 11:58:46 PM »
Exactly what Ferazel said. You can't modify the material as NGUI creates a copy of it. You should be setting UITexture.mainTexture instead, or updating UITexture's drawCall.mainTexture.