Hello,
I begun with Unity today, so pardon me if I am missing something that is basic.
What I want the program to do now is to check if the user entered the correct values (basic check right now) in the fields, if not; it should show a popup telling me which field is wrong. If all the fields are correct it should tell me that the file is saved.
I would also gladly take any critiques on my code, both the actual code and the structure of it.
Do know, I am not used to code C# either, I begun with that today as well. I know some C and C++ before this.
Here is the code I have.
/// <summary>
/// Title.
/// Attached to MainCamera
/// </summary>
using UnityEngine;
using System.Collections;
using System.IO;
public class CreateCharacter : MonoBehaviour {
public Texture backgroundTexture;
public Texture buttonCreateCharacter;
public Texture buttonLoadCharacter;
public Texture buttonDisplayCharacters;
public Texture buttonCredits;
public Texture buttonExitCharacterCreation;
public float buttonYdisplacement = 1.5f;
private bool ShowPopUp = false;
string Name = "";
int Gender = 0;
string Race = "";
string RaceSub = "";
string[] SelGenders = {"Male", "Female"};
bool isCorrect = true;
void OnGUI() {
// Button Variables
int button_w = 230;
int button_h = 60;
int button_x = (Screen.width - button_w) / 2;
int button_y = 180;
int buttonYdistance = button_h + 10;
// Displays Background Texture
GUI.DrawTexture (new Rect (0, 0, 1280, 720), backgroundTexture);
//// Character Creation Screen
// Set Variables for GUI styling (temporare, will use textures later)
Color a = GUI.color;
GUI.skin.label.fontSize = 20;
GUI.skin.textField.fontSize = 20;
GUI.color = Color.black;
// Name
GUI.Label (new Rect (40, 100, 100, 28) , "Name");
Name = GUI.TextField (new Rect (200, 100, 200, 28), Name, 14);
// Gender
GUI.Label (new Rect (40, 190, 100, 28) , "Gender");
Gender = GUI.SelectionGrid (new Rect (200, 190, 100, 28), 0, SelGenders, 2);
// Race
GUI.Label (new Rect (40, 280, 100, 28) , "Race");
Race = GUI.TextField (new Rect (200, 280, 200, 28), Race, 14);
// Sub Race
GUI.Label (new Rect (40, 370, 100, 28) , "Subrace");
RaceSub = GUI.TextField (new Rect (200, 370, 200, 28), RaceSub, 14);
// Sub Race
GUI.Label (new Rect (40, 460, 100, 28) , "Subrace");
RaceSub = GUI.TextField (new Rect (200, 460, 200, 28), RaceSub, 14);
// Restore Color for Buttons
GUI.color = a;
// Title Screen
if (GUI.Button (new Rect (button_x-button_w, button_y + buttonYdistance * buttonYdisplacement*4, button_w, button_h), "Back to Title")) {
Application.LoadLevel ("SceneTitle");
}
// Save Character
if (GUI.Button (new Rect (button_x+button_w, button_y + buttonYdistance * buttonYdisplacement*4, button_w, button_h), "Save Character")) {
if (SaveFile()) {
string file = "";
// Start
file += "<Character>\n";
// Content
file += "\t<Name>"+Name+"</Name>\n";
file += "\t<Race>\n";
file += "\t\t<Name>"+Race+"</Name>\n";
file += "\t\t<Subrace>"+Race+"</Subrace>\n";
file += "\t</Race>\n";
// End
file += "<Character>";
File.WriteAllText("Characters\\"+Name+".cre", file);
GUI.Window(0, new Rect((Screen.width/2)-150, (Screen.height/2)-75, 300, 250), showGUI, "Saved!");
}
}
}
void showGUI(int number) {
if(isCorrect) {
// You may put a label to show a message to the player
GUI.Label(new Rect(65, 40, 200, 30), "You have formed the correct words!!! :)");
} else {
GUI.Label(new Rect(65, 40, 200, 30), "Sorry!! Please try again!!");
}
// You may put a button to close the pop up too
if (GUI.Button(new Rect(50, 150, 75, 30), "OK")) {
isCorrect = false;
// you may put other code to run according to your game too
}
}
public void Update() {
if (Input.GetKeyDown(KeyCode.Escape)) {
Application.LoadLevel ("SceneTitle");
}
}
private bool SaveFile () {
if (Name.Length < 3) {
PopUp ("Name can not be less than three characters short!");
return false;
}
if (Gender == 0) {
PopUp ("No gender has been specified!");
return false;
}
if (Race == "") {
PopUp ("No race has been specified!");
return false;
}
if (RaceSub == "") {
PopUp ("No sub race has been specified!");
return false;
}
if (File.Exists("Characters\\"+Name+".cre")) {
PopUp ("A creature with that name already exists!");
return false;
}
return true;
}
private void PopUp (string msg) {
GUI.Window(0, new Rect((Screen.width/2)-150, (Screen.height/2)-75, 300, 250), showGUI, msg);
}
}
Thanks in advance.