So I i’m making an RPG game and I have 3 classes; warrior, mage, assasin made by me following HardlyBrief Programming’s tutorial.
He shows how to do it using the GUI system, but I wanna use canvas.
Can someone tell me the script for example:
if mage class is select, player’s class is mage?
I know the logic but I don’t know how to translate it into C# code.
Pls help.
Create a canvas, add a panel as the back ground. Add another panel and scale it so it’s big enough to show 1 class. Then add 6 Text items to the panel (that’s the name, description and four attributes that Dan showed.
Drag this script onto the panel
using UnityEngine.UI;
using UnityEngine;
using System.Collections;
public class DisplayClass : MonoBehaviour {
public enum CharType {aMage, aWarrior}; // just a list of names that make sense
public CharType myType; // an enum of CharType = this will show in the inspector
private BaseCharacterClass myClass; // a Base Class to be set based on myType
public Text ClassName; // Just a bunch of public text boxes to drag into the inspector
public Text ClassDescription;
public Text ClassStrength;
public Text ClassEndurance;
public Text ClassStamina;
public Text ClassIntelligent;
void Start()
{
if (myType == CharType.aMage)
myClass = new BaseMageClass ();
else if (myType == CharType.aWarrior)
myClass = new BaseWarriorClass ();
else
Debug.LogError("I have no class at all!");
ClassName.text = myClass.CharacterClassName;
ClassDescription.text = myClass.CharacterClassDescription;
ClassStrength.text = myClass.Strength.ToString ();
ClassEndurance.text = myClass.Endurance.ToString ();
ClassStamina.text = myClass.Stamina.ToString ();
ClassIntelligent.text = myClass.Intelligence.ToString ();
}
}
(the panel with all the texts on it) then select that panel and look in the inspector. You should see a load of empty slots in the script attached to the panel so go ahead and drag each text UI item from the hierarchy onto the empty slots in turn.
Select which class type you want in the inspector. Click play and it should have filled in that class.
Now the good bit
Highlight that panel and duplicate it, drag it off to a spare bit of the canvas and from the enum in the inspector select the other class.
I only put 2 in and if you had loads you might want a case statement rather than as if else but with 3 I wouldn’t worry about it.
Sry but now I have another problem:
When I click the image the class changes but the stats no.
So if I click the warrior image the private bool isWarriorClass that I made public changes but the stats don’t.
Here’s the code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class UI_Creazione_P : MonoBehaviour {
private BasePlayer newPlayer;
public bool isWarriorClass;
public bool isMageClass;
public bool isAssassinClass;
public Image Guerriero;
public Image Mago;
public Image Assassino;
public Text Nome_Classe;
public Text Descrizione_Classe;
public Text Forza;
public Text Intelligenza;
public Text Destrezza;
void Start()
{
newPlayer = new BasePlayer();
isWarriorClass = false;
isMageClass = false;
isAssassinClass = false;
if (isWarriorClass == true)
{
newPlayer.ClasseGiocatore = new WarriorClass(); // ClasseGiocatore = CharacterClass
}
if (isMageClass == true)
{
newPlayer.ClasseGiocatore = new MageClass();
}
if (isAssassinClass == true)
{
newPlayer.ClasseGiocatore = new AssassinClass();
}
else
{
Debug.Log("NO CLASS");
}
PlayerStats();
}
public void SetWarriorClassTrue()
{
isMageClass = false;
isAssassinClass = false;
isWarriorClass = true;
}
public void SetMageClassTrue()
{
isMageClass = true;
isWarriorClass = false;
isAssassinClass = false;
}
public void SetAssassinClassTrue()
{
isAssassinClass = true;
isMageClass = false;
isWarriorClass = false;
}
void PlayerStats()
{
// Classe giocatore
// Vita giocatore
newPlayer.VitaAttuale = newPlayer.ClasseGiocatore.VitaAttuale;
newPlayer.VitaMassima = newPlayer.ClasseGiocatore.VitaMassima;
newPlayer.VitaMinima = newPlayer.ClasseGiocatore.VitaMinima;
// Energia giocatore
newPlayer.EnergiaAttuale = newPlayer.ClasseGiocatore.EnergiaAttuale;
newPlayer.EnergiaMassima = newPlayer.ClasseGiocatore.EnergiaMassima;
newPlayer.EnergiaMinima = newPlayer.ClasseGiocatore.EnergiaMinima;
// Forza
newPlayer.Forza = newPlayer.ClasseGiocatore.Forza;
// Intelligenza
newPlayer.Intelligenza = newPlayer.ClasseGiocatore.Intelligenza;
// Destrezza
newPlayer.Destrezza = newPlayer.ClasseGiocatore.Destrezza;
Nome_Classe.text = newPlayer.ClasseGiocatore.Nome;
Descrizione_Classe.text = newPlayer.ClasseGiocatore.Descrizione;
Forza.text = newPlayer.Forza.ToString();
Intelligenza.text = newPlayer.Intelligenza.ToString();
Destrezza.text = newPlayer.Destrezza.ToString();
}
}