Author Topic: AddChild -> anchored, Prefab "Control - Colored Button" on Panel  (Read 4108 times)

michawtn

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 4
    • View Profile
    • Witan Entertainment
AddChild -> anchored, Prefab "Control - Colored Button" on Panel
« on: December 25, 2013, 04:16:48 PM »
Please help me. I think I mix things up.
I’m trying to anchor a prefab object on a not moving panel.
I created a prefab from the “colored Button”
Spawn the prefab with:
  1.  
  2. public GameObject PrefabButton; // Attached the colored Button.prefab
  3. public GameObject TargetPanel;  // Attached the Panel from the UI Root Hirachie
  4.  
  5. void Start ()
  6. {
  7.     GameObject PrefabSpawnButton = NGUITools.AddChild(TargetPanel, PrefabButton) as GameObject;
  8.     // Till this point everything goes well. But the Anchors are on type : none
  9.  
  10.             PrefabSpawnButton.GetComponent<UISprite>().topAnchor.target = TargetPanel.transform;
  11.             PrefabSpawnButton.GetComponent<UISprite>().bottomAnchor.target = TargetPanel.transform;
  12.             PrefabSpawnButton.GetComponent<UISprite>().leftAnchor.target = TargetPanel.transform;
  13.             PrefabSpawnButton.GetComponent<UISprite>().rightAnchor.target = TargetPanel.transform;
  14.  
  15.             PrefabSpawnButton.GetComponent<UISprite>().topAnchor.absolute = -60;
  16.             PrefabSpawnButton.GetComponent<UISprite>().bottomAnchor.absolute = -124;
  17.             PrefabSpawnButton.GetComponent<UISprite>().leftAnchor.absolute = 20;
  18.             PrefabSpawnButton.GetComponent<UISprite>().rightAnchor.absolute = 84;
  19. }
  20.  

BTW : This script is attached to the Panel.
In the inspector the “anchors” are now on “Unified” and the anchor positions are right. But the “PrefabSpawnButton” is (moving) transforming in the X posive and Y negative and I can’t reset or stop it.
 
Witan Entertainment
Entertaining since 1992
www.witan.nl

Micha van der Meer
미사 호수 에서
Producer

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: AddChild -> anchored, Prefab "Control - Colored Button" on Panel
« Reply #1 on: December 26, 2013, 06:37:05 PM »
I don't understand the question What's moving? Why is it moving? If something is anchored, you can't move it via code.

Your anchor coordinates are odd... top and bottom are both negative for some reason, and right and left are both positive? This causes the anchored widget to be outside the target's bounds.

Furthermore, why do you keep doing GetComponent<UISprite>() every time? Why don't you cache this reference like so:

UISprite sp = PrefabSpawnButton.GetComponent<UISprite>();
sp.topAnchor.target = TargetPanel.transform;
sp.bottomAnchor.target = TargetPanel.transform;
...

michawtn

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 4
    • View Profile
    • Witan Entertainment
Re: AddChild -> anchored, Prefab "Control - Colored Button" on Panel
« Reply #2 on: December 30, 2013, 05:24:12 PM »
Thank you very much for your FAST replay  ;D and please accept my apologies for the late reply I was out of the office for Boxing day in London :-[

Your suggestion is working !

I thought you have to point to the GetComponent<UISprite>() every time otherwise NGUI don’t know what Child NGUI  have to change.. I still don’t know why it was moving but your solution is working!

Thanks and have a great new year from the Netherlands!!


Witan Entertainment
Entertaining since 1992
www.witan.nl

Micha van der Meer
미사 호수 에서
Producer