在Unity里边调用dll里边的方法,dll文件放在项目下(三)

1、先创建一个项目,然后

using System;
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;
        }

    }
}
生成dll文件,把dll文件的后缀改成.bytes,放到Unity3d的Resources文件夹下边,

TextAsset textstr = Resources.Load<TextAsset>("ClassLibrary2");//下载文件
        byte[] bt = textstr.bytes;//文件的内容
        Assembly aa = Assembly.Load(bt);//获取程序集内部信息 
        Type type = aa.GetType("ClassLibrary2.Class1");//类型
        System.Object obj = Activator.CreateInstance(type);//绑定对象
        MethodInfo method = type.GetMethod("Add");//方法
        object[] two = new object[2] { (decimal)5, (decimal)10 };//参数
        method.Invoke(obj, two);//调用

放大想要的位置就好了。

猜你喜欢

转载自blog.csdn.net/wk201403010114/article/details/88842047