Author Topic: UITexture  (Read 34562 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
UITexture
« on: November 20, 2013, 08:27:59 PM »
Overview

UITexture is the most basic drawable component in NGUI. It inherits all the functionality of the UIWidget, and adds a visible component -- a texture -- that gets stretched across its dimensions.



  • You can specify a Texture that will be drawn. A default material will be created for you when you do. If you don't specify a texture, the material's texture will be used instead.
  • If you wish, you can specify a Material of your own. Even with the material specified, the Texture you specified above will be used instead of the material's main texture.
  • If you don't want to make a material, you can also just specify a Shader instead.
  • You can specify a UV Rectangle that will control which part of the texture will be drawn. X and Y control the UV offset, and W and H control the width and height of the UVs (texture coordinates). Like all texture coordinates, these values are usually 0 to 1 (with 0 meaning one side of the texture and 1 meaning the other). The UVs are bottom-left based, so if you wanted to draw the top 50% of the texture you'd specify UV Rectangle of (0, 0.5, 1, 0.5).
Choosing the "Make Pixel-Perfect" option from the context menu will make the UITexture assume its original size. So if the source texture happens to be 300 by 200 pixels, the width and height of your UITexture will be adjusted to exactly those dimensions.

Pro-Tip

You can create your entire UI using UITextures then easily add them to the atlas by opening the Atlas Maker (NGUI -> Open -> Atlas Maker), selecting the root of your UI, and hitting the Create button. All your textures will be placed within this atlas and will be replaced with Sprites instead.

Class Documentation

http://tasharen.com/ngui/docs/class_u_i_texture.html

If you have a question regarding this component or would like me to clarify something, just post a reply here.

Brastik

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: UITexture
« Reply #1 on: February 11, 2014, 09:23:26 PM »
Hi Aren,
I used to add UITexture without a texture, only material. But the material itself didn't have a texture either, I only used shader [Unlit/Transparent Colored]. This would draw a rectangle with plain color (and alpha channel, yes), but only in NGUI 2.x, not in 3.x as far as I see.
So can you please give me some guides on how to create a plain colored rectangle with UITexture. Or if it's not possible then, is there any other way to achieve the same? (it's best if I still can control it like a widget, I mean with the depth, width, height...) Thanks in advance :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture
« Reply #2 on: February 11, 2014, 09:39:13 PM »
This happens because the default texture is "black", which means (0, 0, 0, 0). It was done this way so that if there is no texture set, nothing will show.

You can either create a copy of the shader that changes "black" to "white" (right at the top), or just use a small plain white texture (a single 4x4 quad should do just fine).

Brastik

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: UITexture
« Reply #3 on: February 11, 2014, 11:36:55 PM »
Yea I've tried both ways. The first one seems new and yes it worked. Thanks a lot Aren!  ;D

PoN

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 111
    • View Profile
Re: UITexture
« Reply #4 on: February 13, 2014, 08:59:21 PM »
Hi Aren, I use UITexture with material which has my own shader, then i change params , like these :
  1. material.SetColor("_BrdColor",....
  2. material.SetColor("_BGColor",...
  3. material.SetColor("_FGColor",...
  4.  

but UITexture not update or not repainting , and only repainting (display with new params) when i click on UIInput or click on UIPopupList or just disable-enable UITexture component, why? what'w wrong?

P.S. that had been coming from NGUI 2.7 vers.
« Last Edit: February 17, 2014, 03:26:50 AM by PoN »
Worked on Doc&DogAge Of Fury 3D. Actually working on WarMach.

PoN

  • Full Member
  • ***
  • Thank You
  • -Given: 1
  • -Receive: 4
  • Posts: 111
    • View Profile
Re: UITexture
« Reply #5 on: February 17, 2014, 02:42:30 AM »
Up !! Up!!!
Worked on Doc&DogAge Of Fury 3D. Actually working on WarMach.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture
« Reply #6 on: February 17, 2014, 01:47:17 PM »
NGUI makes copies of materials so that it can modify them. You need to use widget.drawCall.material.SetColor instead.

mitay-walle

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: UITexture
« Reply #7 on: June 19, 2015, 02:28:58 AM »
Hello! I'm using UITexture with runtime-imported texture, for some reason it has wrong local scale after import, and I can't change it through code.
https://www.dropbox.com/s/71l3ty9udmosggc/%D0%A1%D0%BA%D1%80%D0%B8%D0%BD%D1%88%D0%BE%D1%82%202015-06-19%2009.57.47.png?dl=0
In a little different situation, Anchors helped, but this is not that case.

What I've tried to do:

            image.mainTexture = texture;

            image.transform.localScale = Vector3.one;  // no effect
            transform.localScale = Vector3.one;  // no effect
            //image.height = NGUIMath.AdjustByDPI(.01f); //this changes not the local scale, but pixel size in widget
            //image.width = NGUIMath.AdjustByDPI(.01f);
            //NGUIMath.MakePixelPerfect(image.uvRect,600,300); // no effect
            //image.transform.localScale = Vector3.one;  // no effect
            transform.localScale = Vector3.one;  // no effect

My main question: How can I change local scale (in transform) of UITexture?
« Last Edit: June 19, 2015, 03:07:21 AM by mitay-walle »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UITexture
« Reply #8 on: June 21, 2015, 08:31:40 PM »
If something is anchored, then you won't be able to change its width or height, and you shouldn't be messing with its scale.

If you want to change dimensions of something not anchored:
  1. image.width = texture.width;
  2. image.height = texture.height;
If you want to change dimensions of something anchored with a bottom-left pivot point:
  1. image.rightAnchor.Set(0f, texture.width);
  2. image.topAnchor.Set(0f, texture.height);