baobao
6/1/2017 - 9:02 AM

Unityネイティブプラグインショートコード

Unityネイティブプラグインショートコード

extern "C"
{
    float _fooPluginFunction ()
    {
        // Unityのメソッドを実行する
        // ネイティブ => C#
        UnitySendMessage("Go", "FromNativeCall", "Native to C#");
        return 5.0F;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;

public class UseTestPlugin : MonoBehaviour 
{
    #if UNITY_IOS
    // static libraryの場合は"__Internal"を指定
    [DllImport("__Internal")]
    #endif

    static extern float _fooPluginFunction();

    public static float FooPluginFunction()
    {
        #if UNITY_EDITOR
        return 0;
        #elif UNITY_IOS
        return _fooPluginFunction();
        #else
        return 0;
        #endif
    }

    void Update () 
    {
        // C# => Native実行
        Debug.Log("Update : " + FooPluginFunction());
    }
    
    // Native => C#
    public void FromNativeCall (string param)
    {
        Debug.Log("FromNativeCall: " + param);
    }
}