Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

How Pass C# Object to JavaScript?

Discussion in 'Project Tiny' started by howonice123, Jun 26, 2019.

  1. howonice123

    howonice123

    Joined:
    Sep 27, 2018
    Posts:
    5
    mergeInto(LibraryManager.library, {

    Hello: function () {
    window.alert("Hello, world!");
    },

    HelloString: function (str) {
    window.alert(Pointer_stringify(str));
    },

    PrintFloatArray: function (array, size) {
    for(var i = 0; i < size; i++)
    console.log(HEAPF32[(array >> 2) + i]);
    },

    AddNumbers: function (x, y) {
    return x + y;
    },

    StringReturnValueFunction: function () {
    var returnStr = "bla";
    var bufferSize = lengthBytesUTF8(returnStr) + 1;
    var buffer = _malloc(bufferSize);
    stringToUTF8(returnStr, buffer, bufferSize);
    return buffer;
    },

    BindWebGLTexture: function (texture) {
    GLctx.bindTexture(GLctx.TEXTURE_2D, GL.textures[texture]);
    },

    });

    Then you can call these functions from your C# scripts
    like this:

    using UnityEngine;
    using System.Runtime.InteropServices;

    public class NewBehaviourScript : MonoBehaviour {

    [DllImport("__Internal")]
    private static extern void Hello();

    [DllImport("__Internal")]
    private static extern void HelloString(string str);

    [DllImport("__Internal")]
    private static extern void PrintFloatArray(float[] array, int size);

    [DllImport("__Internal")]
    private static extern int AddNumbers(int x, int y);

    [DllImport("__Internal")]
    private static extern string StringReturnValueFunction();

    [DllImport("__Internal")]
    private static extern void BindWebGLTexture(int texture);

    void Start() {
    Hello();

    HelloString("This is a string.");

    float[] myArray = new float[10];
    PrintFloatArray(myArray, myArray.Length);

    int result = AddNumbers(5, 7);
    Debug.Log(result);

    Debug.Log(StringReturnValueFunction());

    var texture = new Texture2D(0, 0, TextureFormat.ARGB32, false);
    BindWebGLTexture(texture.GetNativeTextureID());
    }
    }

    Above its OK
    but i want pass object or class to javascript it error!!
    how pass object or class to javascript??
    thansks u help^^

    example:


    javascript

    HelloObj: function (obj) {
    window.alert("Hello, world!");
    }

    C#
    public class Data {
    public string s1 = "123";
    public string s2;
    }
    [DllImport("__Internal")]
    private static extern void HelloObj(object obj);

    HelloObj(new Data());