Author Topic: Changing widget texture in code  (Read 9887 times)

Ludopathic

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Changing widget texture in code
« on: January 04, 2017, 06:21:22 AM »
Hi, I have the full NGUI from the asset store (version 3.11.1 according to the readme) and I'm confused.

I searched the forum for help about this issue before posting but all the issues people have seem to be from previous versions of NGUI that were free.

I am trying to set the texture of a button or UI2dsprite via code - when I try to do so I get the error "has no mainTexture setter" - I'm not really sure what to do here.

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Changing widget texture in code
« Reply #1 on: January 04, 2017, 09:02:25 AM »
difficult to help without seeing the code that is causing the error...  try this:

  1. transform.GetComponent<UISprite>().spriteName = "sprite_1"

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing widget texture in code
« Reply #2 on: January 04, 2017, 10:02:50 AM »
Sprites use a shared texture. You don't change their texture, you change their sprite.

If you want to change a texture, use UITexture, not a sprite.

Ludopathic

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Changing widget texture in code
« Reply #3 on: January 04, 2017, 10:53:42 AM »
OK, to clarify I have an NGUI button which on click opens a filebrowser. I have code to upload the image selected from the filebrowser as the avatar of the player and then save it to local disk so that it can then be set as the image for the button that was clicked.

I was trying to do this :

  1. ProfilePicButton.maintexture = Resources.Load ("/avatar.png") as Texture;

Where ProfilePicButton has been referenced beforehand. I tried changing the type to various different things without success.

It seems to me from your replies that I might have to add the image somehow to the atlas and set it as a sprite? Is there a more direct approach I can take?


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing widget texture in code
« Reply #4 on: January 04, 2017, 11:03:34 AM »
You're using the wrong widget. As I said, don't use a sprite. use a texture -- UITexture is the widget you should be using.

Ludopathic

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Changing widget texture in code
« Reply #5 on: January 04, 2017, 11:04:44 AM »
Update - I added the UITexture component to the button and remove other components with images, changed the code to reflect the other changes but now I am getting this error:

ArgumentException: texture is null

clicking on the log takes me to the UIDrawCall script and the line

  1. set
  2.                 {
  3.                         mTexture = value;
  4.                         if (mBlock == null) mBlock = new MaterialPropertyBlock();
  5.                         mBlock.SetTexture("_MainTex", value);
  6.                 }

So I'm guessing I'm failing to pass a certain value here?