I have 3 scripts: GameControl.js, DataManager.cs and GamePause.js.
The GameControl script is in the folder “Plugins”.
//GameControl.js
public var currentXP : int = 0;
public var maxXP : int = 10;
public var currentLevel : int = 1;
public var currentHealth : float = 45;
public var maxHealth : int = 45;
public var currentEnergy : float = 27;
public var maxEnergy : int = 27;
public var currentStrengh : int = 15;
private var maxStrengh : int = 650;
public var currentAttack : int = 11;
private var maxAttack : int = 670;
public var currentDefense : int = 20;
private var maxDefense : int = 760;
private var currentStamina : float = 25;
public var maxStamina : int = 25;
public var currentCritical : int = 5;
private var maxCritical : int = 100;
public var currentSpeed : int = 12;
private var maxSpeed : int = 690;
public var currentHP : float = 15;
public var maxHP : int = 15;
//more stuff...
The DataManager script makes like a Save and Load System, and is located in a normal folder, but this need some variables from GameControl script.
//DataManager.cs
using UnityEngine;
using System.Collections;
public class DataManager : MonoBehaviour
{
public static DataManager dataSaveAndLoad;
GameControl dataVariables;
float healthData;
int maxHealthData;
int experienceData;
int maxExperienceData;
int levelData;
float energyData;
int maxEnergyData;
int attackData;
int strenghData;
int defenseData;
int maxStaminaData;
int criticalData;
int speedData;
float HPData;
int maxHPData;
void Start ()
{
dataVariables = GameObject.Find("Player").GetComponent<GameControl>();
}
void Update ()
{
healthData = dataVariables.currentHealth;
maxHealthData = dataVariables.maxHealth;
experienceData = dataVariables.currentXP;
maxExperienceData = dataVariables.maxXP;
levelData = dataVariables.currentLevel;
energyData = dataVariables.currentEnergy;
maxEnergyData = dataVariables.maxEnergy;
attackData = dataVariables.currentAttack;
strenghData = dataVariables.currentStrengh;
defenseData = dataVariables.currentDefense;
maxStaminaData = dataVariables.maxStamina;
criticalData = dataVariables.currentCritical;
speedData = dataVariables.currentSpeed;
HPData = dataVariables.currentHP;
maxHPData = dataVariables.maxHP;
}
public void Save()
{
//...
}
public void Load()
{
//...
}
}
The problem is…
How to call a C# function/void from a Javascript?
The GamePause have some GUIButtons, and from this script call the DataManager functions/voids.
//GamePause.js
private var isPause : boolean = false;
private var pauseWindow = new Rect (20,20,300,150);
function Start ()
{
pauseWindow = new Rect (20,20,300,150);
}
function Update ()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
isPause = !isPause;
if(isPause == true)
{
Time.timeScale = 0;
Screen.showCursor = true;
}
if(isPause == false)
{
Time.timeScale = 1;
Screen.showCursor = false;
}
}
}
function OnGUI ()
{
if(isPause == true)
{
pauseWindow = GUI.Window(0,pauseWindow,PauseMenu,"Pausa");
GUI.Box(Rect(0,0,Screen.width,Screen.height),"");
}
if(isPause == false)
{
Time.timeScale = 1;
Screen.showCursor = false;
}
}
function PauseMenu ()
{
if(GUILayout.Button("Continue"))
{
isPause = false;
}
GUILayout.Button("Save"); //Call Save void
GUILayout.Button("Load"); //Call Load void
GUILayout.Button("Game Settings");
if(GUILayout.Button("Main Menu"))
{
Application.LoadLevel("MainMenu");
}
}