- Home /
Automatic GUI Creation When players are Added.
I am trying to write an Application for Mobile Development that uses mainly the GUI system. What I have is a button to add player to the game, and when it is added the GUI will automatically scale to fit each player on screen (Atleast down to a minimum size). What I cant seem to figure out is how to have a GUI generate to fill the rest of the screen when another player is added.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MTGCounterGUI : MonoBehaviour
{
public int numOfPlayers;
public int numOfMonsters;
public int healthCount;
public int poisonCount;
public int monsterCounter;
public bool canInput;
public List<string> textInput;
public List<string> playerNames;
public void CountGUI ()
{
int i = 0;
if(numOfPlayers >= 1){
i = numOfPlayers - 1;
} else {
i = 0;
}
GUI.skin.GetStyle( "Label").alignment = TextAnchor.UpperCenter;
GUI.BeginGroup (new Rect (0, 25, Screen.width / numOfPlayers, Screen.height), "");
GUI.Box (new Rect (0, 0, Screen.width, Screen.height - 25), "");
GUI.Label (new Rect (0, 10, Screen.width / numOfPlayers, 35), playerNames[i]);
GUI.EndGroup ();
}
public void AddPlayer ()
{
int i = 0;
if (GUI.Button (new Rect (0, 0, 25, 25), "+")) {
canInput = true;
textInput.Add("Name " + i);
}
if (canInput) {
textInput[i] = GUI.TextField (new Rect (Screen.width / 4, Screen.height / 2, Screen.width / 2, 25), textInput[i], 25);
if (Input.GetKeyDown (KeyCode.Return)) {
playerNames.Add (textInput[i]);
numOfPlayers++;
i++;
textInput = null;
canInput = false;
}
}
}
void Update(){
}
void OnGUI ()
{
AddPlayer();
if (numOfPlayers > 0) {
CountGUI ();
}
}
}
This is what i have so far.
What happens is the first GUI re-sizes to allow for more to be created. I just don't know where to begin to have more created.
Answer by AndyMartin458 · Jan 16, 2013 at 05:26 AM
If you have a function that draws the GUI, you can do a for loop that iterates for as many times as the player is drawn. You'll have to be clever to place the GUI in a good location. Put this function in your AddPlayer code. However! I would suggest that all this GUI code really go directly inside of OnGUI and have the AddPlayer code only have to do with actually adding the player (not drawing the gui).
int xStart = 0;
int yStart = 0;
int maxX = 100;
for(int i = 0; i < numOfPlayers; i++) {
if (GUI.Button (new Rect (xStart, yStart, 25, 25), "+")) {
canInput = true;
textInput.Add("Name " + i);
}
xStart += 25;
if( xStart * i < maxX) {
xStart = 0;
yStart += 25;
}
}
Thanks. Didnt know it would be that simple XD. Shows that i try to over think things. a Simple foreach on the GUI draw to loop for each player.
So did that work our for you? Noticed it was thumbsed-up, but not selected as the answer.
Your answer
Follow this Question
Related Questions
How do you CORRECTLY call methods from another C# file 1 Answer
Using GUI skin with my GUI Button 1 Answer
Using GUI and GUILayout at the same time 1 Answer
creating a list of texture2d components 0 Answers
FPS keep a loadout 0 Answers