# コード例: 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

以下のコードは、Unity C# スクリプトが関数を呼び出すことができる JavaScript コードの例です。

```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]);
  },

});
```

以下のコードは、JavaScript の例で定義された関数を呼び出す Unity C# コードの例です。

```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

以下のコードは、Unity プロジェクトで呼び出し可能な簡単な関数を持つ C++ プラグインの例です。

```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)
