- Home /
Transfer values from C# to Javascript
What would be the easiest way to move values from, for example, a Vector4 array in C#, to the same in Javascript?
The easiest way would be to search this site a bit. There is A LOT of information related to this topic.
I haven't found much, is there some keywords I should be using? The best I've found is not on this site, it's the "[% %]" method, which I already know. I'm wondering what, if any, alternative methods Unity allows. I don't $$anonymous$$d searching, I'm just not doing it well right now, help me out, please.
Now you have triggered my curiosity - what you mean by "[% %]" method? Did you mean
<%= %>
syntax? If yes, then it is used in ASP.NET and it's completely unrelated to Unity. If, on the other hand, you meant something else, could you please add a link to an example?
As to your issue - I assume you have some object with both C# and JavaScript attached and you want to access properties/fields on one in second. If yes, then it's not possible, unless C# script is in for example Plugins folder. For details, check the answer to this question.
If your problem is different, then please post additional details.
Yes that's the method I was referring to, I'm aware of it's limitations that's why I'm looking for a Unity method. I used brackets because I hadn't noticed the blockquote button and it was reading it as html in normal text, removing it from the page.
What I'm trying to do is generate noise values through libnoise-net, which is built on c#, then use those values in a javascript function. For instance, I would fill a Vector4 Array of X size in C#, then utilize the values in javascript to generate terrain.
I'm fairly sure I could simply write the values to an external source and then fill a Uscript vector4 with that source, right? I'm just wondering if Unity allows a simpler, faster method.
This was suggested by another user:
using UnityEngine;
using System.Collections;
public class someCS: $$anonymous$$onoBehaviour {
public Vector4[] someArray;
void Start () {
someArray = new Vector4[1];
someArray[0] = new Vector4( 0, 1, 2, 3 );
}
}
//C# ^ Javascript v
function Start () {
print(GetComponent.<someCS>().someArray[0]);
}
Can I use this method to somehow print the values into a javascript function rather than the display? For instance a uscript Vector4?
Answer by ArkaneX · Sep 07, 2013 at 10:33 PM
After some comments, I have a full view of your problem. Or at least I hope so :)
You have a library written in C#, but for your Unity project you're using JavaScript. In one of your comments you wrote 'uscript Vector4', but there's no difference between Vector4 in UnityScript/JavaScript and Vector4 used in C#. Both are .NET types. It means, if some method in your library returns Vector4 array, you can call it from UnityScript directly, after you put this library in the Assets folder.
For example, assuming that the library contains a class NoiseGenerator with a method
public static Vector4[] Generate()
you can simply use
var someArray : Vector4[] = NoiseGenerator.Generate();
I was wondering if they worked globally like that, thank you very much ArkaneX, I'll give this a try and let you know how it works out.
@Arkanex, putting it in the Asset folder will not solve the issue.
@fafase - why not? We're not talking about script but about third party assembly. It is already compiled.
Oh yes, I skipped the lib part he described. $$anonymous$$y bad.
Your answer
Follow this Question
Related Questions
How to get the World position of a connectedAnchor? (2d) 1 Answer
Distribute terrain in zones 3 Answers
Natural camera sway 1 Answer
Fixed error but error still shows in the console 2 Answers
Get a position relative to a gameobject 0 Answers