Unity 里的代码 直接调用dll里的函数,去在c#逻辑里执行一些操作

//////////////////////////////////////////////////////////////////////////

class SomeScript : MonoBehaviour {

       #if UNITY_IPHONE

       // On iOS plugins are statically linked into

       // the executable, so we have to use __Internal as the

       // library name.

       [DllImport ("__Internal")]

       #else

       // Other platforms load plugins dynamically, so pass the name

       // of the plugin's dynamic library.

       [DllImport ("PluginName")]

扫描二维码关注公众号,回复: 10546141 查看本文章

       #endif

       private static extern float FooPluginFunction ();

 

       void Awake () {

          // Calls the FooPluginFunction inside the plugin

          // And prints 5 to the console

          print (FooPluginFunction ());

       }

    }

 

Unity 里的代码 直接调用dll里的函数,去在c#逻辑里执行一些操作:

可以只在 #if UNITY_EDITOR #endif 里边

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]

Public delegate void CSharpDebugLogDelegate(string str)

static CSharpDebugLogDelegate debugLogFunc = msg => UnityEngine.Debug.Log(msg);

 

Unity里的android下的libMyPlugin.so IOS下libMyPlugin.a x86 x64下有libMyPlugin.dll

!UNITY_EDITOR && UNITY_ANDROID的时候

[DllImport ("MyPlugin", EntryPoint = “SetDebugLogFunc”)]

Private static extern void SetDebugLogFunc(IntPtr ptr);

!UNITY_EDITOR && UNITY_IOS的时候

[DllImport ("__Internal")]

Private static extern void SetDebugLogFunc(IntPtr ptr);

在其它的时候

[DllImport ("libMyPlugin", EntryPoint = “SetDebugLogFunc”)]

Private static extern void SetDebugLogFunc(IntPtr ptr);

 

var callback = new CSharpDebugLogDelegate(debugLogFunc);

var ptr = Marshal.GetFunctionPointerForDelegate(callback);

SetDebugLogFunc(ptr);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

1、先生成一个dll文件


using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary2
{
    public class Class1
    {
        public decimal Add(decimal x, decimal y)
        {
            long timer = System.DateTime.Now.Ticks;
            int k = 0;
            for (int i = 0; i < 100000000; i++)
            {
                k++;
            }
            long timer1 = System.DateTime.Now.Ticks;
            Console.WriteLine(timer1 - timer);
            return timer1 - timer;
        }
        public decimal SubTract(decimal x, decimal y)
        {
            return x - y;
        }
        public decimal Mul(decimal x, decimal y)
        {
            return x * y;
        }
        public decimal Div(decimal x, decimal y)
        {
            return x / y;
        }

    }
}
 

2、在Unity里边创建一个项目,然后

string path = "C:/Users/Administrator/Desktop/OldFile/ClassLibrary2.dll";//文件的位置
    // Use this for initialization
    void Start ()
    {
        Assembly ass = Assembly.LoadFrom(path);
        Type t = ass.GetType("ClassLibrary2.Class1");//获得类型
        System.Object o = Activator.CreateInstance(t);
        MethodInfo ins = t.GetMethod("SubTract");
        object[] obj = new object[2] {(decimal )5, (decimal)10 };
        ins.Invoke(o, obj);

        MethodInfo in3 = t.GetMethod("Mul");
        MethodInfo in4 = t.GetMethod("Div");


        Debug.Log(ins.Invoke(o, obj));
        Debug.Log(in3.Invoke(o, obj));
        Debug.Log(in4.Invoke(o, obj));


    }

3、挂上脚本,运行就可以啦

/////////////////////////////////////////////////////////////////////////////////////////

发布了16 篇原创文章 · 获赞 44 · 访问量 38万+

猜你喜欢

转载自blog.csdn.net/Game_jqd/article/details/89035014
今日推荐