Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sooper1337 · Jan 16, 2013 at 03:58 AM · c#guiapplicationandriod

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
   }    
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sooper1337 · Jan 16, 2013 at 06:43 AM 0
Share

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.

avatar image AndyMartin458 · Jan 16, 2013 at 06:00 PM 0
Share

So did that work our for you? Noticed it was thumbsed-up, but not selected as the answer.

avatar image Sooper1337 · Jan 16, 2013 at 10:49 PM 0
Share

Apologies forgot to mark it as answered =).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges