Author Topic: LoadImage into Button texture  (Read 10443 times)

Roots

  • Guest
LoadImage into Button texture
« on: April 17, 2012, 10:34:02 AM »
Hi,

I have seen a few threads on the topic of how to dynamically load images into for instance buttons. I have read the posts but none show any working code samples (at least not the ones i was looking at). From these threads i have compiled the following sample code which is not working though. So hopefully someone can help me out?

// loading the file:
string targetFile = "file://c:/Users/Roots/Documents/UnityProjects/Lost/test.png";
WWW t_load = new WWW(targetFile);

// putting it into the button
Texture2D img = new Texture2D(128, 128);
t_load.LoadImageIntoTexture(img);

// bpButton is an instance of my own Button prefab which is a NGUI Button
UISlicedSprite back = bpButton.GetComponentInChildren<UISlicedSprite>();

So how can I now set the texture for the "back" UISlicedSprite object?

Can i dynamically add it to the material of the atlas with a name - any sample code how that would work?

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: LoadImage into Button texture
« Reply #1 on: April 17, 2012, 06:14:13 PM »
You can't. All widgets work with texture atlases, not textures. The only exception is UITexture, which works with a material of your choice, displaying the material's texture in its entirety. That's what you need.

Roots

  • Guest
Re: LoadImage into Button texture
« Reply #2 on: April 17, 2012, 08:06:11 PM »
Ok, so if i just add an empty gameobject with the UItexture script i put the imagefile in there basically?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: LoadImage into Button texture
« Reply #3 on: April 17, 2012, 09:22:00 PM »
To add it via code, I recommend using AddWidget:
  1. UITexture ut = NGUITools.AddWidget<UITexture>(parentGameObject);
  2. ut.material = new Material(Shader.Find("Unlit/Transparent Colored"));
  3. ut.material.mainTexture = YourTexture;
  4. ut.MakePixelPerfect();

Roots

  • Guest
Re: LoadImage into Button texture
« Reply #4 on: April 17, 2012, 11:53:05 PM »
nice, all working now, thanks for those couple of lines of code, helped solve the last missing pieces.




Roots

  • Guest
Re: LoadImage into Button texture
« Reply #5 on: April 18, 2012, 12:35:28 AM »
Hmm, i am running into a problem. The UITexture is part of a prefab that i load into a Grid list (with a scrollbar). The problem is that initially all loads fine, but when i start scrolling the UItexture doesn't reappear when it is in view again?!
Any thoughts?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: LoadImage into Button texture
« Reply #6 on: April 18, 2012, 12:38:49 AM »
Are you running the latest version 2.0.3?

Roots

  • Guest
Re: LoadImage into Button texture
« Reply #7 on: April 18, 2012, 09:16:56 AM »
Yep, just download and ran it with the 2.0.3.b version and the problem actually got worse. Basically I have a grid item prefab which has a button and a UItexture (and the . The scene has a grid and then via code i load prefab instances into the grid. All of that works fine except the after grid items are out of view and then come back into view both the uitexture.

Now with the new version 2.0.3.b also the button is dissapearing but on top of that as soon as i move the scrollbar all my grid items blank out?!

Any suggestions?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: LoadImage into Button texture
« Reply #8 on: April 18, 2012, 09:23:03 AM »
The latest is 2.0.3c actually, but regardless... items disappearing suggests that you have issues with Z-fighting. It's most often caused by using more than one atlas and not specifying which one is supposed to be on top of the other by adjusting Z. Check out the first reply in this thread: http://www.tasharen.com/forum/index.php?topic=6.0

Roots

  • Guest
Re: LoadImage into Button texture
« Reply #9 on: April 18, 2012, 10:15:00 AM »
Ok so one of the problems is that the buttons are being drawn but runtime the clipping settings (center-x,y) of my uidraggable panel are being changed?. In my case move 50 position up (y+50) which is causing the button to be cut?! I wasn't seeing this behaviour before my upgrade to 2.0.3.c version?!


Not sure about the UITexture, I think this is a seperate issue.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: LoadImage into Button texture
« Reply #10 on: April 18, 2012, 10:39:02 AM »
The way the panels get dragged was completely changed recently, which is why you're seeing a difference. I suggest looking at the provided examples and seeing what's different. One thing that needs to happen is that the clipped area's panel needs to be offset along the Z. I generally set mine to have -1.

Roots

  • Guest
Re: LoadImage into Button texture
« Reply #11 on: April 18, 2012, 09:49:57 PM »
Ok, seem to be able to get this working better, it does feel a little buggy though. So grid items can only be added design time? I tried adding them via code but the scrollbar seems to dissapear. As a work around i put a large sprite e.g. 600 to strech the grid and force the scrollbar. Still a little fumbling with layouts. I might just pre-seed in the editor with say 10 items and just deactivate them. I think the system would be more stable, just a a bit of a limitation (users essentially create these items and therefore i limit them to 10 items as well).

Thanks for the help again.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: LoadImage into Button texture
« Reply #12 on: April 18, 2012, 09:51:12 PM »
After adding anything to a draggable panel with a scroll bar, you need to call UIDraggablePanel's ResetPosition() function.

Roots

  • Guest
Re: LoadImage into Button texture
« Reply #13 on: April 18, 2012, 11:27:40 PM »
ok, so that last one resolved my problems. All is working now. Thanks again for your support.