I have a Dictionary of characters that receives a key that is the name of the character and a value that can be mage or warrior, then i try to show these characters names but all come with the same name.
The process in the code below is that i enter the name of the character in a text box and choose a profession at the class guicontroller, when i press the button Create army the name of the text field become the characters name and in the dictionary.Add itself he calls create"Profession" where profession is the profession int that was choosed.
After that when the players mouse enter over an object this object name is saved on a variable that is used as key in the dictionary to find the script associated with him and than show his name, but all objects are showing the same name.
public class Controller : MonoBehaviour {
//---------Variaveis--------------------------------------------------------------
private float position_x, position_z; //Posicao dos personagens criados.
private string selected_character; //Qual personagem esta selecionado.
private string characterinfo_popup; //Nome do personagem referente ao popup que sera exibido.
private Vector3 character_actionsmenu_info; //Nome do personagem referente ao menu de acoes.
private bool character_actionsmenu_status;
//---------Objetos----------------------------------------------------------------
private Warrior warrior; // Objeto warrior.
private Mage mage; // Objeto mage.
//--------Etruturas de Dados------------------------------------------------------
private Dictionary<string,Character> characters; //Dicionario de Personagens Key = nome do personagem, Personagem.
// Use this for initialization
void Start () {
position_x = 10.5f;
position_z = 0.5f;
selected_character = "";
characterinfo_popup = null;
character_actionsmenu_info = new Vector3();
character_actionsmenu_status = false;
warrior = gameObject.AddComponent<Warrior>();
mage = gameObject.AddComponent<Mage>();
characters = new Dictionary<string, Character>();
}
// Update is called once per frame
void Update () {
}
public void setSelectedCharacter(string character){
selected_character = character;
}
public string getSelectedCharacter(){
return selected_character;
}
public void setCharacterInfoPopupStatus(string cips){
characterinfo_popup = cips;
}
public string getCharacterInfoPopupStatus(){
return characterinfo_popup;
}
public void setCharacterActionsMenuInfo(Vector3 cami){
character_actionsmenu_info = cami;
}
public Vector3 getCharacterActionsMenuInfo(){
return character_actionsmenu_info;
}
public void setCharacterActionsMenuStatus(bool cams){
character_actionsmenu_status = cams;
}
public bool getCharacterActionsMenuStatus(){
return character_actionsmenu_status;
}
public string getProfessionName(int profession){
switch(profession){
case 1:
return "Warrior";
case 2:
return "Mage";
}
return "Error 101"; //Profissao nao encontrada.
}
public Character getCharacter(string key){
//key = nome do personagem.
return characters[key];
}
private void createWarrior(string name){
warrior.setCharacterName(name);
warrior.setCharacterHP(1000);
warrior.setMovementPoints(4);
warrior.setCharacterProfession(1);
}
private void createMage(string name){
mage.setCharacterName(name);
mage.setCharacterHP(500);
mage.setMovementPoints(2);
mage.setCharacterProfession(2);
}
public Character createCharacter(string name, int profession){
switch(profession){
case 1:
createWarrior(name);
return warrior;
case 2:
createMage(name);
return mage;
}
return null;
}
public void saveNewCharacter(string character_name,int character_profession){
characters.Add(character_name,createCharacter(character_name,character_profession));
}
public void instantiateCharacter(string character_name){
string profession = getProfessionName(characters[character_name].getCharacterProfession());
GameObject temp = (GameObject)Instantiate(Resources.Load(profession));
temp.transform.position = new Vector3(position_x,0.5f,position_z);
temp.name = character_name;
(temp.GetComponent(profession)as Character).setCharacterName(characters[character_name].getCharacterName());
position_x += 2;
}
}
Class Character
public class Character : MonoBehaviour {
protected string character_name;
protected int character_hp;
protected int movement_points;
protected int character_profession;
protected int attack_range;
// Use this for initialization
void Start () {
character_name = "";
character_hp = 0;
movement_points = 0;
character_profession = 0;
attack_range = 0;
}
// Update is called once per frame
void Update () {
}
public void setCharacterName(string name){
character_name = name;
}
public string getCharacterName(){
return character_name;
}
public void setCharacterHP(int hp){
character_hp += hp;
}
public int getCharacterHP(){
return character_hp;
}
public void setMovementPoints(int mvp){
movement_points = mvp;
}
public int getMovementPoints(){
return movement_points;
}
public void setCharacterProfession(int profession){
character_profession = profession;
}
public int getCharacterProfession(){
return character_profession;
}
}
Class Warrior
public class Warrior : Character {
Controller controller;
// Use this for initialization
void Start () {
controller = GameObject.Find("Controller").GetComponent<Controller>();
}
// Update is called once per frame
void Update () {
}
void OnMouseOver(){
if (Input.GetMouseButtonDown(1))
{
controller.setCharacterInfoPopupStatus(null);//Remove popup status do personagem.
RaycastHit hit = new RaycastHit(); // Nome do objeto que o raycast esta colidindo.
RaycastHit mousehit = new RaycastHit(); // Nome do objeto clicado com mouse.
Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out mousehit);
//Exibe menu de acoes deste objeto caso estaja no range de acao
if(Physics.Raycast(gameObject.transform.position, Vector3.forward,out hit,2)){//Testa se o Raycast do objeto esta colidindo com algo e transfere este algo para variavel hit.
if(mousehit.transform.name == hit.transform.name){//O nome do objeto clicado com mouse deve ser diferente do nome do proprio objeto
controller.setCharacterActionsMenuInfo(Input.mousePosition);
controller.setCharacterActionsMenuStatus(true);
}
}
}
}
void OnMouseDown(){
controller.setSelectedCharacter(gameObject.name); // Seta variavel personagem selecionado para nome deste objeto.
}
void OnMouseEnter() {
controller.setCharacterInfoPopupStatus(gameObject.name); // Seta variavel info status popup para nome deste objeto.
}
void OnMouseExit(){
controller.setCharacterInfoPopupStatus(null); // Seta variavel info status popup para nulo.
}
}
Class GUIController
public class GUIController : MonoBehaviour {
Controller controller; // Ponteiro para controller.
//-------------Menu criar personagem--------
private string character_name; // Repositorio para o texto field name.
//-------------Radio Buttons-----------------
private int selected_option; // Qual opcao esta selecionada.
private string[] radiobutton_options; // Opcoes dos radio button.
// Use this for initialization
void Start () {
controller = GameObject.Find("Controller").GetComponent<Controller>();
character_name = "";
selected_option = 0;
radiobutton_options = new string[] {"Warrior", "Mage"};
}
// Update is called once per frame
void Update () {
}
void OnGUI () {
GUI.Box(new Rect(10,10,250,200), "Create Army"); // Box inicial para criar personagens.
selected_option = GUI.SelectionGrid(new Rect(30, 80, 120, 30), selected_option, radiobutton_options, 2); // Radio buttons.
character_name = GUI.TextField (new Rect (30, 35, 100, 30), character_name); // Campo de texto para digitar nome do personagem
//Quando precionar o botao create character:
if(GUI.Button(new Rect(30,150,120,40), "Create Character")) {
controller.saveNewCharacter(character_name,selected_option + 1); // Salvar novo personagem no dicionario.
controller.instantiateCharacter(character_name); // Instanciar o prefab referente a este personagem.
}
//Janela popup caso mouse esteja sobre algum personagem.
if(controller.getCharacterInfoPopupStatus() != null){
//Lista as informacoes referentes ao personagem.
Character temp = controller.getCharacter(controller.getCharacterInfoPopupStatus());
GUI.Box(new Rect(Input.mousePosition.x,Input.mousePosition.y,250,200),
temp.getCharacterName()
);
//-------------Fim da listagem---------------
}
//Popup menu de acoes do personagem.
if(controller.getCharacterActionsMenuStatus() == true){
Vector3 temp = controller.getCharacterActionsMenuInfo();
GUI.Box(new Rect(temp.x,temp.y,250,200), "Actions");
if(GUI.Button(new Rect(temp.x,temp.y,120,40), "Attack")){
controller.getCharacter("sdd").setCharacterHP(-100);
controller.setCharacterActionsMenuStatus(false);
}
}
}
}