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:
void CreateOpenGameButton(int gameId)
{
GameObject g = null;
GameObject randomOpponentButton = GameObject.Find("Button_RandomOpponent");
Vector3 gPos
= new Vector3
(randomOpponentButton
.transform.localPosition.x,
randomOpponentButton.transform.localPosition.y,
randomOpponentButton.transform.localPosition.z);
GameObject draggablePanel = GameObject.Find("Child");
GameObject newButton = NGUITools.AddChild(draggablePanel, mButtonPrefab);
newButton.GetComponent<RejoinGameButton>().setMultiplayerRef(this);
newButton.GetComponent<RejoinGameButton>().setGameId(gamePlanId);
newButton.name ="Button_ResumeGame_"+ gamePlanId.ToString();
newButton
.transform.localScale = new Vector3
(0
.5f, 0
.5f,
0);
mRejoinButtons.Add(newButton);
if (mRejoinButtons.Count > 1)
{
g = mRejoinButtons[mRejoinButtons.Count -2]; //reference to the penultimate button
gPos.x = g.transform.localPosition.x;
gPos.y = g.transform.localPosition.y;
gPos.z = g.transform.localPosition.z;
}
newButton
.transform.localPosition = new Vector3
(gPos
.x, gPos
.y -70, gPos
.z)
}
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:
NGUITools.AddWidgetCollider(newButton);
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:
UILabel label = newButton.GetComponentInChildren<UILabel>();
label.text = "Resume Game #" + gameId.ToString();
Any advice on this ? If needed I can post the hierarchy of the project and the configuration parameters of every gameobject.