Javascript functions from C Sharp

I have my C# scripts in the Plugins folder so that the javascript scripts can access them, but what about going back the other way. I have something like this:

	public functions functions;
	void Start () {
		functions = (functions)PlayerObject.GetComponent("functions");
////////////////OTHER STUFF HERE
		functions.SendMessage ("gamestart");
////////////////OTHER STUFF HERE
	}

functions would be a javascript thats outside of the plugins folder because it needs to access the C# files in the plugins folder. I think the problem is that the C# files get loaded first so it doesn’t see the functions script, but they both call each other for different reasons.

I know the best solution would be to rewrite my functions.js in C#, but there is so much code in that and other javascript files that it would create a big delay to re-write them. The C# scripts have to be C# because I’m using the UIToolKit that works best with C#.

Hi…i provides another way to accessing javascript functions from c# here…

Call the InvokeScript method on the HtmlDocument.

C#

namespace System.Windows.Forms
{
public sealed class HtmlDocument
{
public object InvokeScript(string scriptName);
public object InvokeScript(string scriptName, object[ ] args);
}
}

For example, let’s assume that you have a System.Windows.Forms.WebBrowser object named webBrowser1 and you want to call a JavaScript function in the HTML page loaded in your WebBrowser called “showMe()”

JavaScript

function showMe()
{

}

C#

webBrowser1.Document.InvokeScript(“showMe”);

Adding parameters gets a bit more complicated, but not much. You can create an array of parameters as the API suggests and they will be passed in to the JavaScript function. However, there is an even simpler way to do it.

We will start by calling the following function

JavaScript

function showMe(x,y)
{

}

Let’s write a wrapper function in C# using the params keyword to let the compiler do the work for us. It will automatically convert any extra parameters into an array of objects, just like InvokeScript is expecting.

C#

private object MyInvokeScript(string name, params object[ ] args)
{
return webBrowser1.Document.InvokeScript(name, args);
}

int x = 50;
int y = 100;
MyInvokeScript(“showMe”,x, y);

Note that the InvokeScript will return the value that the JavaScript function returns. According to the documentation, if it is a native type such as a number or string, it will be returned as a string, but it can also return an object.

JavaScript

function createPoint(x, y)
{
// Assume I have a Point object
var p = new Point(x,y);
return p;
}

function setPoint (p)
{
// Do something useful with the Point p.
}

C#

object o = MyInvokeScript(“createPoint”, 50, 100);

It is possible to query information about the object using GetType and InvokeMember but I like that the object can be passed back into JavaScript

C#

MyInvokeScript(“setPoint”, o);

This is very powerful and, combined with the ability to call from JavaScript into C#, can allow you to embed many web applications that provide a JavaScript API into your C# application.


Cegonsoft

Try adding a class to whatever variable you want to access and make it static.

If you move the C# scripts into a folder named Editor inside a folder named Plugins according to this you should be able to have your js scripts in either Plugin be available in the C# scripts in Plugins/Editor.

I don’t really know if editor folders reach the end build because i’ve never tried it but hopefully it’ll work fine. I really wish there was less walls like this between languages in unity. You’d think the js scripts being compiled at the same time as the c# scripts would be accessible since their all mono/.net

But at any rate, if this works its probably your best choice, otherwise it sucks having to port languages, but it could be worth the time in the long run.

What about JS Functions?

Yeah I actually like working with C# better because its faster and it seems more structured, but I’m just starting to learn it and switching over to it right now would be a good learning experience, but would delay my progress. I think the Editor folder trick may have worked, but I put my CS scripts in Editor under plugins then my JS scripts in plugins. I didn’t see any errors for the CS scripts anymore.

Nevermind: That brought me back to the problem I had back when I didn’t have the CS scripts in the plugins folder and the Javascripts outside of it:

var controlstouch : controlstouch = fixedupdate.gameobjects["c1"].GetComponent("controlstouch");

Gives me:

The name 'controlstouch' does not denote a valid type ('not found'). Did you mean 'UnityEngine.ControllerColliderHit'?

controlstouch would be a CS script…

You know this used to work on 3.3, but now on 3.4 it seems like its become more strict on my Android build. Like I can’t get away with the stuff I used to do and theres no way around it.

This used to work:

My code was this:

	public GameObject c1;
	void Start () { 
		GameObject c1 = (GameObject)GameObject.Find("c1");
		fixedupdate tmpInfo = c1.GetComponent(typeof(fixedupdate)) as fixedupdate;
	}

That worked before without a problem to pull my fixedupdate script and my Globals script. From there I would be able to update my Javascripts Globals which both my CS and JS scripts work with, but now it realizes that it does not know the type.

The type or namespace name `fixedupdate' could not be found. Are you missing a using directive or an assembly reference?

I was also able to do this before, but now it gives me the error that follows my code below:

	public fixedupdate fixedupdate;

Now I get:

`controlstouch.fixedupdate' is a `field' but a `type' was expected

I’m kinda lost at this point because I thought my workaround will now be to just convert the needed functions from Javascript into CS and use them, but if I can’t update my Globals which my JS scripts rely on then the CS scripts can’t really do much. So the only other option I see is to port all my code to CS or stick with JS which I would rather not use with the UIToolKit.