Author Topic: Issue with scroll view (clipped panel) + instantiated button  (Read 10819 times)

Antares88

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 35
    • View Profile
Issue with scroll view (clipped panel) + instantiated button
« on: April 19, 2013, 12:51:17 PM »
Hi,
i'm creating a multiplayer menu for my game, where you can find a button to find a random opponent and a list of open games to rejoin.
I'm using a vertical scroll view with a clipped panel that I made following the tutorial on the documentation, and everything seems to be ok when I tested it placing only the random opponent button and some clones of it.

The problem comes when I tried to add the rejoin buttons at runtime. These buttons are instantiated from a prefab with this code:

  1.         void CreateOpenGameButton(int gameId)
  2.         {      
  3.                 GameObject g = null;
  4.                 GameObject randomOpponentButton = GameObject.Find("Button_RandomOpponent");
  5.                 Vector3 gPos = new Vector3(randomOpponentButton.transform.localPosition.x,
  6.                                                                         randomOpponentButton.transform.localPosition.y,
  7.                                                                         randomOpponentButton.transform.localPosition.z);
  8.                
  9.                 GameObject draggablePanel = GameObject.Find("Child");
  10.                
  11.                 GameObject newButton = NGUITools.AddChild(draggablePanel, mButtonPrefab);
  12.                 newButton.GetComponent<RejoinGameButton>().setMultiplayerRef(this);
  13.                 newButton.GetComponent<RejoinGameButton>().setGameId(gamePlanId);
  14.                 newButton.name ="Button_ResumeGame_"+ gamePlanId.ToString();
  15.                 newButton.transform.localScale = new Vector3(0.5f, 0.5f, 0);
  16.                
  17.                 mRejoinButtons.Add(newButton);
  18.                
  19.                
  20.                
  21.                 if (mRejoinButtons.Count > 1)
  22.                 {
  23.                         g = mRejoinButtons[mRejoinButtons.Count -2]; //reference to the penultimate button
  24.                         gPos.x = g.transform.localPosition.x;
  25.                         gPos.y = g.transform.localPosition.y;
  26.                         gPos.z = g.transform.localPosition.z;
  27.                 }
  28.                
  29.                 newButton.transform.localPosition = new Vector3(gPos.x, gPos.y -70, gPos.z)
  30.                        
  31.         }

This script instantiates the buttons and places them in the correct positions calculated in relation with a non-runtime-instantiated button called Button_RandomOpponent.

The buttons are generated correctly, they work correctly and the drag still works, but their colliders are fucked up (they are not in the correct places) and their labels appear below their backgrounds.

I solved the problem of the colliders deleting them from the prefab and creating them when the buttons are generated, adding this code to the script:

  1. NGUITools.AddWidgetCollider(newButton);
  2.  

Then I tried the same strategy with the labels, deleting them from the prefab and creating them when the buttons are instantiated, but it didn't work:

  1. UILabel label = newButton.GetComponentInChildren<UILabel>();
  2. label.text = "Resume Game #" + gameId.ToString();
  3.  

Any advice on this ? If needed I can post the hierarchy of the project and the configuration parameters of every gameobject.


 

AndyGFX

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 24
    • View Profile
    • AndyGFX
Re: Issue with scroll view (clipped panel) + instantiated button
« Reply #1 on: April 19, 2013, 02:47:33 PM »
  1. GameObject go_lbl = new GameObject("new_label");
  2.                 go_lbl.AddComponent(typeof(UILabel));  
  3.                 go_lbl.transform.position = Vector3.zero;
  4.                
  5.                 // get fonts
  6.                 UIFont[] fonts = (UIFont[])Resources.FindObjectsOfTypeAll(typeof(UIFont));
  7.                
  8.                 // set font
  9.                 go_lbl.GetComponent<UILabel>().font = fonts[0];
  10.                 go_lbl.GetComponent<UILabel>().text = "Test label";
  11.                
  12.                
  13.                 go_lbl.transform.parent = this.transform;
  14.  
« Last Edit: April 19, 2013, 03:07:21 PM by AndyGFX »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Issue with scroll view (clipped panel) + instantiated button
« Reply #2 on: April 19, 2013, 11:31:02 PM »
You need to use NGUITools.AddWidgetCollider to dynamically update the collider. You should also never use a scale value of 0 (you used 0.5, 0.5, 0). Bad Shitâ„¢ happens when you do.

Antares88

  • Newbie
  • *
  • Thank You
  • -Given: 3
  • -Receive: 0
  • Posts: 35
    • View Profile
Re: Issue with scroll view (clipped panel) + instantiated button
« Reply #3 on: April 20, 2013, 03:23:01 PM »
You need to use NGUITools.AddWidgetCollider to dynamically update the collider.

I did ^^

You should also never use a scale value of 0 (you used 0.5, 0.5, 0). Bad Shitâ„¢ happens when you do.

Thanks ! that solved the issue :)