Unity使用DLL

引用C#DLL

  1. 首先,我们需要一个库文件,新建一个类库项目,这里就不多介绍了。
  2. 项目建好后,实现库文件功能。

这是一个简单的测试代码

namespace DllTest
{
    public class DllClass
    {
        public int Sum(int a,int b)
        {
            return a+b;
        }

        static public bool isMax(int a,int b)
        {
            return a>b?true:false;
        }
    }
}
  1. 将项目属性 -> 应用程序 -> 目标框架:改为 .NET Framework 3.5或以下 。这一步很重要,因为Unity3D(当前的Unity3D版本是3.5版) 支持的 .Net 是3.5版。

如果选择的是4.0版会出现,错误:
Internal compiler error. See the console log for more information. output was:Unhandled Exception: System.TypeLoadException: Could not load type ‘System.Runtime.Versioning.。

  1. 生成解决方案。到此,C# Dll工作完成,在项目的bin/debug 里面就会看到dll文件。

  2. 导入Dll到Unity中。将Dll拷贝到Assets目录下任何一个目录都可以。一般是在Plugins目录中,因为Dll大多是外部功能块,同时也方便跨语言调用。

  3. 在Unity中编写简单代码。

using UnityEngine;
using System.Collections;
using DllTest; //Dll的命名空间

public class Test : MonoBehaviour
{
    void Start()
    {
        if(DllClass.isMax(10,5))
        {
            DllClass cl = new DllClass();
            Debug.Log(cl.Sum(20,10));
        }
    }
}
  1. 刚才我们使用的是原生的C#,假如我们需要在类库项目写调用Unity3D本身功能的方法怎样办呢?
    在Unity3D的安装目录Editor\Data\Managed里面,找到UnityEditor.dll和UnityEngine.dll两个文件。然后在类库项目里面添加引用,把这两个dll添加进来.

引用C/C++库文件

1.建立C/C++库工程,编译,拿到DLL文件复制到Plugins文件夹下x86或者x86_x64。注意:是64位dll还是32位dll;

2.库工程文件
Test.h

#if _MSC_VER //当使用Visual Studio 编译时存在它
#define EXPORT_API _declspec(dllexport) //Visual Studio 必须用它注释export 函数
#else
#define EXPORT_API // XCode 不需要export函数,ExPoRT_API 为空
#endif

extern "C"
{
    extern EXPORT_API char * PrintHello();

    extern int EXPORT_API PrintANumber();

    extern int EXPORT_API AddTwoIntegers(int a, int b);

    extern float EXPORT_API AddTwoFloats(float a, float b);
}

Test.cpp

#include "TestDLL.h"

extern EXPORT_API char * PrintHello()
{
    return "Hello\0";
}

extern int EXPORT_API PrintANumber()
{
    return 5;
}

extern int EXPORT_API AddTwoIntegers(int a, int b)
{
    return a + b;
}

extern float EXPORT_API AddTwoFloats(float a, float b)
{
    return a + b;
}

Unity调用代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;//托管/非托管模块间的互相调用


public class Test : MonoBehaviour {

    [DllImport("TestDLL")]
    private unsafe static extern char * PrintHello();

    [DllImport("TestDLL")]
    private static extern int PrintANumber();

    [DllImport("TestDLL")]
    private static extern int AddTwoIntegers(int a, int b);

    [DllImport("TestDLL")]
    private static extern float AddTwoFloats(float a, float b);

    private void Start()
    {
        //string hello = PrintHello();
        unsafe
        {
            //在传递字符串时,将字符所在的内存固化,
            //并取出字符数组的指针
            fixed (char* p = &("hello".ToCharArray()[0])){
                //调用函数 p做参数
            }
            char* a = PrintHello();
            Debug.Log(PrintHello()->ToString());
        }


        Debug.Log(PrintANumber());
        Debug.Log(AddTwoIntegers(10, 20));
        Debug.Log(AddTwoFloats(1.1f, 2.2f));
    }
}

猜你喜欢

转载自blog.csdn.net/s17728022507/article/details/75675131