- Home /
C# List and GUI
Greetings! So i have the two scripts below and i was wondering how to go about making some GUI for them. For example, have a button called "Warrior" and when press the Warrior item from the Character.cs script along with its stats is loaded.
RPGClass.cs
using UnityEngine;
using System.Collections;
public class RPGClass : MonoBehaviour{
public string ClassName;
public int Strength;
public int Vitality;
public int Dexterity;
public int Intelligence;
public RPGClass(string cName, int str, int vit, int dex, int intel){
ClassName = cName;
Strength = vit;
Dexterity = dex;
Intelligence = intel;
}
}
Character.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Character : MonoBehaviour{
void Start(){
List<RPGClass> rpgclasses = new List<RPGClass>();
rpgclasses.Add(new RPGClass("Warrior",12,12,10,8));
rpgclasses.Add(new RPGClass("Archer",10,11,12,9));
}
}
Heads up, you might have problems using new with $$anonymous$$onoBehaviours. Unity likes to use start as a constructor for them and will likely protest.
Answer by nesis · Jan 26, 2014 at 06:04 AM
I imagine the most general way to do it would do something tricky with Unity's class serialisation, but I've not dabbled much in that so I won't even begin to suggest how to do it. Tapping into Unity's class serialisation would possibly make this work for other scripts too...?
The less generalisable way I'd go about it is to add a method to RPGClass that generates some GUI stuff to show stats, and call it in an OnGUI() method in the Character class. The Character class would have a boolean flag to determine whether or not the GUI should be drawn.
RPGClass.cs
using UnityEngine;
using System.Collections;
public class RPGClass : MonoBehaviour{
public string ClassName;
public int Strength;
public int Vitality;
public int Dexterity;
public int Intelligence;
public RPGClass(string cName, int str, int vit, int dex, int intel){
ClassName = cName;
Strength = vit;
Dexterity = dex;
Intelligence = intel;
}
//A method to draw the RPGClass' GUI. Note that this
//will work only when called in OnGUI(), since it uses
//GUI.Label()!
public string DrawGUI() {
GUI.Label(Rect(10f,10f,100f,20f),"Class: "+ClassName);
GUI.Label(Rect(10f,30f,100f,20f),"Strength: "+Strength);
GUI.Label(Rect(10f,50f,100f,20f),"Dexterity: "+Dexterity);
GUI.Label(Rect(10f,70f,100f,20f),"Intelligence: "+Intelligence);
}
}
Character.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Character : MonoBehaviour{
//if true, stats will be shown. if false, no stats will be shown.
private bool showStats;
//im guessing you want to store a list of RPGClasses...
//so you should make it a member variable by putting it here!
List<RPGClass> rpgclasses = new List<RPGClass>();
//set this to show the stats of that class when showing stats
RPGClass currentClass;
void Start(){
rpgclasses.Add(new RPGClass("Warrior",12,12,10,8));
rpgclasses.Add(new RPGClass("Archer",10,11,12,9));
//initialise currentClass somehow... you can set this however you want
SetCurrentClass(rpgclasses.Last());
}
//method to let you set currentClass from outside
//this script if wanted
public void SetCurrentClass(RPGClass rpgClass) {
currentClass = rpgClass;
}
public void OnGUI () {
//button to toggle showing stats
if (GUI.Button(Rect(10,10,50,50),"Show Stats")) {
showStats = !showStats;
}
//Draw stats on screen if showStats is true, and if currentClass isn't null.
//Note this is less efficient than just enabling / disabling a GUIText, and
//should ideally only be used if you're wanting the DrawGUI() method to
//create GUI elements that the user can interact with (buttons, text inputs,
//etc).
if (showStats && currentClass) {
currentClass.DrawGUI();
}
}
}
Hello! Thanks for posting that! I tried to use it but i got several errors on both scripts :S Actually, i was thinking about making a third script that would be only the GUI and create boxes and button from there and somehow use the GetComponent to access the stats. Would that be possible?
Answer by aceronn · Jul 05, 2014 at 09:36 AM
If you want to have stuff load like this, your best bet would be to instantiate a prefab. Make a prefab for each class (model, scripts, etc) and on button press, delete the last loaded prefab and instantiate a new one. I did this for models in the past so it should work similar for this project. Let me know if you need help. (ps sorry for late answer)
Answer by Kiwasi · Jul 05, 2014 at 10:58 AM
Here is how I would do it. I'm assuming you know how to get the appropriate references between scripts and declare variables and all that stuff. I'm also assuming you have appropriate code to load the player. The code just covers the GUI. This method is almost independent of the number of classes you have.
void OnGUI () {
foreach (RPGClass currentClass in rpgclasses){
// You could also add other GUI stuff in here
if(GUILayout.Button (currentClass.ClassName){
// Create your player as required here
}
}
}