# 代码示例：在 Unity 中调用 JavaScript 和 C/C++/C# 函数

> Code examples that show interactions between Unity, JavaScript, and C-based code.

您可以使用代码来执行插件与 Unity 代码之间的交互。以下示例展示了如何在 Unity 项目中从 JavaScript 和 C/C++/C# 代码调用各种类型的函数。

## 在 Unity C# 中调用 JavaScript 代码的示例##call-javascript-code-in-unity-c-example

以下代码是 JavaScript 代码示例，Unity C# 脚本可以从中调用函数。

```js
mergeInto(LibraryManager.library, {

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

  HelloString: function (str) {
	window.alert(UTF8ToString(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]);
  },

});
```

以下代码是 Unity C# 代码的示例，用于调用 JavaScript 示例中定义的函数。

```cs
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(1, 1, TextureFormat.ARGB32, false);
        BindWebGLTexture(texture.GetNativeTexturePtr());
    }
}
```

## 在 Unity C# 脚本中调用 C/C++/C# 代码的示例##call-c/c++/c-code-in-unity-c-scripts-example

以下代码是一个 C++ 插件示例，其中包含可在 Unity 项目中调用的简单函数。

```cpp
#include <stdio.h>

extern "C" void Hello ()
{
    printf("Hello, world!\n");
}

extern "C" int AddNumbers (int x, int y)
{
    return x + y;
}
```

然后在 Unity 项目中使用以下 Unity C# 代码来调用 C++ 函数。

```cs
using UnityEngine;
using System.Runtime.InteropServices;

public class NewBehaviourScript : MonoBehaviour {

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

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

    void Start() {
        Hello();
        
        int result = AddNumbers(5, 7);
        Debug.Log(result);  
    }
}
```

## 其他资源##additional-resources

* [与浏览器脚本交互](/engine/6000.0/manual/platform-specific/webgl/develop/interactingwithbrowserscripting.md)

* [设置 JavaScript 插件](/engine/6000.0/manual/platform-specific/webgl/develop/interactingwithbrowserscripting/web-interacting-browser-js.md)

* [从 Unity C# 脚本调用 JavaScript 函数](/engine/6000.0/manual/platform-specific/webgl/develop/interactingwithbrowserscripting/web-interacting-browser-js-to-unity.md)

* [从 JavaScript 调用 Unity C# 脚本函数](/engine/6000.0/manual/platform-specific/webgl/develop/interactingwithbrowserscripting/web-interacting-browser-unity-to-js.md)

* [从 Unity C# 脚本调用 C/C++/C# 函数](/engine/6000.0/manual/platform-specific/webgl/develop/interactingwithbrowserscripting/web-interacting-browsers-c-to-unity.md)
