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")]

       #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、挂上脚本,运行就可以啦

/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值