- Home /
I need to call a C# function from Javascript...
I need to write and read config from a external file, the problem is... The function that write changes in the file is written (evidently) in C#, but the GUI script that calls the C# function is written in Js... If I call it like a Js function not works...
c# function is here: (Writer.cs)
using UnityEngine;
using System.Collections;
using System;
using System.IO;
public class Writer : MonoBehaviour {
private FileInfo theSourceFile = null;
private StreamWriter writer = null;
private string sPath = System.Environment.GetEnvironmentVariable("HOMEDRIVE")
+ System.Environment.GetEnvironmentVariable("HOMEPATH");
// Function that saves the settings in the config file...
public void write (int resWidth, int resHeight, int Quality) {
if (Application.platform != RuntimePlatform.OSXWebPlayer &&
Application.platform != RuntimePlatform.WindowsWebPlayer){
if (File.Exists(sPath + "/VHconfig.txt")){
Debug.Log("Configuration file found in " + sPath + "/VHconfig.txt" + ". Deleting...");
File.Delete(sPath + "/VHconfig.txt");
}
theSourceFile = new FileInfo (sPath + "/VHconfig.txt");
writer = theSourceFile.CreateText();
writer.WriteLine(resWidth);
writer.WriteLine(resHeight);
writer.WriteLine(Quality);
writer.Close();
}
}
}
Is called from Js here:
Writer.write(changedResWidth, changedResHeight, changedQuality);
Any way to call it from Js??
Thanks a lot.
Answer by Murcho · Feb 27, 2010 at 10:55 PM
JS files are compiled before C# files, so I'm going to make the assumption that you are getting a warning something to the effect "Script doesn't exist" seeing as you haven't provided the exact problem here. The way you're calling it should be right.
To get the C# script compiled before the JS script, place the C# script into the Standard Assets folder. Scripts inside this folder are compiled before scripts in any other folders you make, with the except of a few other special folders. You can find the full compilation order information here.