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 rpgclasses = new List();
//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();
}
}
}
↧