Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: AGB on October 21, 2014, 08:36:15 AM

Title: Setting width to anchored texture problem
Post by: AGB on October 21, 2014, 08:36:15 AM
When setting width-height to texture - it works ok:
(http://i.imgur.com/gebVtBM.png)

But when i do same thing, when everything is disabled - after enabling -- texture is twice bigger!
(http://i.imgur.com/qLD2XZJ.png)

What i do - i set texture width and height to 100px. But it changes anchors to 50 and -50, as if the anchor target had 0x size.
 
NGUI 3.7.0
Title: Re: Setting width to anchored texture problem
Post by: ArenMook on October 22, 2014, 05:17:37 AM
Looks fine to me. Your original anchor is:

+4 and -4 with a width and height of 162. Add these up to get a total of 170.

Your second anchor is -50 and +50 with a width and height of 270. Add these up and you also get 170. Your anchored target is 170 pixels.

What's the issue exactly? Your anchor changes without you changing it? Most commonly anything related to anchors not having the expected dimensions happens because of either the UICamera not being there, or there being more than one UICamera drawing the same layer -- for example having your main camera draw your UI.
Title: Re: Setting width to anchored texture problem
Post by: AGB on October 23, 2014, 04:21:18 AM
The problem is with second anchor. It was -4, 4, before i set width to 100.

After i set width and height to 100, it automaticly changed anchors to -50, 50
Title: Re: Setting width to anchored texture problem
Post by: ArenMook on October 23, 2014, 03:36:08 PM
Adjusting width/height will adjust the anchors for you. Not sure what else to say there. Why wouldn't? Alternative is that adjusting width and height would do absolutely nothing, as the widget is anchored. You can adjust the anchors directly if you prefer.
Title: Re: Setting width to anchored texture problem
Post by: AGB on October 27, 2014, 01:59:10 PM
> Adjusting width/height will adjust the anchors for you.

Thats right, BUT it works wrong in this case.
Ahchor target with is 170px.
If i set sprite width to 100px - it should automatically change anchors to +35 and -35, because Anchor target is bigger.

But in this case, when i set width to 100px - it makes anchors to -50 and +50 (As if my sprite was BIGGER and anchor width in reality is 0!). Next what happens - Anchor target understands, that its with is not 0, but 170! so it expands itself and it changes width of my sprite from 100 to 270 (Because of wrong set anchors)
Title: Re: Setting width to anchored texture problem
Post by: ArenMook on October 29, 2014, 04:59:28 AM
I think I'm missing something. Can you create a simple example showing the issue using NGUI's atlases?
Title: Re: Setting width to anchored texture problem
Post by: AGB on October 31, 2014, 03:11:56 PM
Not sure, what is the best way to send sources here. Have completely isolated the problem and found simplest way to implement a problem:

  1.         GameObject clone = NGUITools.AddChild(gameObject, originalWidget);
  2.         UITexture uiTexture = clone.GetComponentInChildren<UITexture>();
  3.  
  4.         uiTexture.mainTexture = graphics;
  5.         uiTexture.width = graphics.width;
  6.         uiTexture.height = graphics.height;

originalWidget it this gameobject:
http://gyazo.com/97c33fd8ff45412242f21440f2f27438

it has this UITexture child:
http://gyazo.com/464f2d883ee1b189da5d56a2446a2166
(it is anchored with -4,4)

graphics is just any texture2d :)


As you can see - after cloning gameobject and in same time settings its width/height causes a problem, as if the anching stuff is not happening on awake or something..
Title: Re: Setting width to anchored texture problem
Post by: ArenMook on November 01, 2014, 07:58:56 AM
You shouldn't be trying to clone existing active game objects. You should only instantiate prefabs.
Title: Re: Setting width to anchored texture problem
Post by: AGB on November 01, 2014, 10:03:44 AM
Same thing happens, when instatiating a prefab.   :'( Just with Cloning it can be easier reproduced.
Title: Re: Setting width to anchored texture problem
Post by: AGB on November 01, 2014, 10:08:28 AM
What is the best way to send you a source? if i add a zip with project files, but without NGUI source codes - meta files will disconnect my prefabs from your code..
Title: Re: Setting width to anchored texture problem
Post by: ArenMook on November 02, 2014, 03:17:22 AM
You can make meta files visible under project settings. Best way is to export a package though. But don't include NGUI as a part of that package.
Title: Re: Setting width to anchored texture problem
Post by: AGB on November 02, 2014, 06:39:29 AM
Here is a package. Desired result:
http://gyazo.com/560f811543dacdadc41db5694377478a

What we get:
http://gyazo.com/90aa1fa239962ef8c191c9eda8b95819
Title: Re: Setting width to anchored texture problem
Post by: ArenMook on November 03, 2014, 06:28:36 AM
If you need it to look like that, then don't set width and height. Setting width and height changes anchors.
Title: Re: Setting width to anchored texture problem
Post by: AGB on November 03, 2014, 11:08:29 AM
Yes, but it Changes anchors, as if it was anchored to something Zero sized! There is the problem. I Want to change anchors, but i want them to change correctly, because my texture is anchored to aa widget with size> 0. But just after AddChild widget things, that itw width is 0.

When setting width to 100, anchors will bekome -50 and 50 only if width of anchor-parent is 0. But you see, that it is not.
Title: Re: Setting width to anchored texture problem
Post by: AGB on November 05, 2014, 02:27:34 PM
Another sample of broken anchors, in just created prefab and changed its width-height: http://i.imgur.com/Cp1NI4N.jpg
Title: Re: Setting width to anchored texture problem
Post by: AGB on November 05, 2014, 02:40:41 PM
Problem seems to be here:
UIRect.cs:

public void SetHorizontal (Transform parent, float localPos)


in this function, just after Instatiating, rect is null! But should be anchor target widget.
Title: Re: Setting width to anchored texture problem
Post by: AGB on November 05, 2014, 02:44:08 PM
This code can be a dirty fix:
http://gyazo.com/ca23817b4c307f2f64dcd288a75724bc

  1.                 public void SetHorizontal (Transform parent, float localPos)
  2.                 {
  3.             if (rect == null && parent!=null)
  4.             {
  5.                 rect = parent.GetComponent<UIRect>();
  6.             }
  7.           ....
  8.            

But in reality - why is it happening like this?
Title: Re: Setting width to anchored texture problem
Post by: ArenMook on November 06, 2014, 02:06:09 AM
Because you instantiate the object, then modify it. This means you are making changes right after its Awake() function. Its anchors don't get updated until later. You can update them by calling uiTexture.ResetAndUpdateAnchors() before altering the width and height, but you don't do that.
  1. using UnityEngine;
  2. public class ReceivedGiftPopup : MonoBehaviour
  3. {
  4.     public GameObject originalWidget;
  5.     public Texture2D graphics;
  6.  
  7.     public void SetGiftRandom()
  8.     {
  9.         GameObject clone = NGUITools.AddChild(gameObject, originalWidget);
  10.         UITexture uiTexture = clone.GetComponentInChildren<UITexture>();
  11.  
  12.                 uiTexture.ResetAndUpdateAnchors(); // <-- you are missing this
  13.         uiTexture.mainTexture = graphics;
  14.         uiTexture.width = graphics.width;
  15.         uiTexture.height = graphics.height;
  16.     }
  17. }
Title: Re: Setting width to anchored texture problem
Post by: AGB on November 06, 2014, 02:39:32 AM
 ;D Thanks, this is exactly what i was looking for!