Author Topic: Setting width to anchored texture problem  (Read 19183 times)

AGB

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 74
    • View Profile
    • Steam Defense
Re: Setting width to anchored texture problem
« Reply #15 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.
I'm a busy man... I have places to go,monsters to kill...

AGB

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 74
    • View Profile
    • Steam Defense
Re: Setting width to anchored texture problem
« Reply #16 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?
I'm a busy man... I have places to go,monsters to kill...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Setting width to anchored texture problem
« Reply #17 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. }

AGB

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 74
    • View Profile
    • Steam Defense
Re: Setting width to anchored texture problem
« Reply #18 on: November 06, 2014, 02:39:32 AM »
 ;D Thanks, this is exactly what i was looking for!
I'm a busy man... I have places to go,monsters to kill...