- Home /
How do I replace void OnGUI? C#
How do I replace the void OnGUI with buttons I make myself in my code below?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SelectUsername : MonoBehaviour
{
public static int userNumber = -1; //current user (<0 means nothing selected)
public static string userName = "";
string[] usernames;
int numUsers = 0;
// Start is called before the first frame update
void Start()
{
numUsers = PlayerPrefs.GetInt("NumUsers", 0); // how many registered users?
usernames = new string[numUsers + 1]; //create user name list...
for (int n = 1; n <= numUsers; n++)
{
usernames[n] = PlayerPrefs.GetString("User" + n); // ...and load them.
}
}
// Update is called once per frame
void OnGUI()
{
if (userNumber < 0) //if any number isn't defined yet do the following
{
Rect r = new Rect(300, 300, 300, 100); // create basic rectangle
GUI.Label(r, "Select User");
for (int n = 1; n <= numUsers; n++)
{
r.y += 100; // offset rect to the next line
if (GUI.Button(r, usernames[n]))
{
// if user selected...
userNumber = n; // save number...
userName = usernames[n];
}
}
}
}
}
AND how do I display the user's name in my own textbox on the main screen? I know how to do it for only 1 registered user, but am having the worst time telling the PlayerPrefs which user is selected. Can someone PLEASE show mercy on me?
Answer by Hellium · May 06, 2019 at 03:16 PM
1. In your scene, create a canvas if you don't have any :`GameObject` menu > UI
> Canvas
2. Select your canvas, and create a button : GameObject
menu > UI
> Button
3. Click on the button, and drag&drop it in the Project
view, inside a Prefabs
folder)
4. Delete the button from your scene you have added step #2
5. Select your canvas, and add a child gameObject : GameObject
menu > Create empty child
6. Select the created child, and name it UsersList
, resize it and place it wherever you want in your canvas
7. Select the child created step #5, in the inspector, click on AddComponent
> VerticalLayoutGroup
and check Width
ofthe ChildControlsSize
8. Change your code as follow:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SelectUsername : MonoBehaviour
{
public GameObject UserButton;
public LayoutGroup UsersListParent;
public static int userNumber = -1; //current user (<0 means nothing selected)
public static string userName = "";
string[] usernames;
int numUsers = 0;
private Button[] userButtons ;
// Start is called before the first frame update
void Start()
{
numUsers = PlayerPrefs.GetInt("NumUsers", 0); // how many registered users?
usernames = new string[numUsers]; //create user name list...
userButtons = new Button[numUsers];
for (int n = 1; n <= numUsers; n++)
{
usernames[n - 1] = PlayerPrefs.GetString("User" + n); // ...and load them.
}
BuildGUI();
}
// Update is called once per frame
void BuildGUI()
{
for( int i= 0 ; i < usernames.Length ; ++i )
{
AddUserButton( usernames[i], i ) ;
}
}
void AddUserButton( string name, int index )
{
GameObject go = Instantiate( UserButton, UsersListParent.transform ) ;
Text text = go.GetComponentInChildren<Text>();
Button userButton = go.GetComponent<Button>();
text.text = name;
userButton.onClick.AddListener( () => SelectUser( index ) ) ;
userButtons[index] = userButton;
}
void SelectUser( int index )
{
Debug.LogFormat( "User {0} (#{1}) has been selected", usernames[index], index ) ;
// The two following lines are optional
if( userNumber >= 0 )
userButtons[index].GetComponent<Image>().color = Color.white;
userNumber = index;
userName = usernames[index];
// The two following lines are optional
if( userNumber >= 0 )
userButtons[index].GetComponent<Image>().color = Color.red;
}
}
9. Select the gameObject holding the previous script, drag the prefab you have created step #3, and drop it in the UserButton
slot in the inspector
10. Select the gameObject holding the script step #8, drag the gameObject created step #5 (with the VerticalLayout
component), and drop it in the UsersListParent
slot in the inspector
@Hellium I do NOT want to use prefabs. I want to use my already made & displaying buttons. How do I tell PlayerPrefs WHICH BUTTON the player has clicked on AND carry that over to save their gold, exp, etc info in a different panel I made?
Your question is literally "How do I replace the void OnGUI with buttons I make myself in my code below?" so that's why I suggested prefabs.
If you want to use the obsolete OnGUI
anyway :
if (GUI.Button(r, usernames[n]))
{
// if user selected...
userNumber = n; // save number...
userName = usernames[n];
PlayerPrefs.SetInt( "SelectedUserIndex", n );
PlayerPrefs.SetString( "SelectedUserName", userName );
}
If you want to save more date about each user, then do not use PlayerPrefs. This solution is not meant to save game data. It's a quick a dirty way to save small data about the preferences of the game player itself (the executable). You should really consider storing data into files. One file per user for instance, containing all his/her data.
@Hellium I feel more lost than ever. It appears my $$anonymous$$cher has taught how to make a complete the total WRONG way!
Would you please point to a video tutorial on saving different players info the way you are suggesting? I'm sorry, I just feel like I've wasted so much time this past year learning the wrong info.
Your answer
![](https://koobas.hobune.stream/wayback/20220612200823im_/https://answers.unity.com/themes/thub/images/avi.jpg)