Unity 打包代码到 DLL

Unity 打包代码到 DLL

使用Unity API PlayerBuildInterface.CompilePlayerScripts 将项目中的代码生成为 DLL 程序集

在 Editor 文件夹下新建脚本 CompileDll

using UnityEngine;
using UnityEditor;
using UnityEditor.Build.Player;
using System.IO;

public class CompileDll
{
    // 菜单栏添加按钮
    [MenuItem("Tools/CompileDll")]
    static void Compile()
    {
        // 设置需要打DLL 的平台 
        BuildTarget target = BuildTarget.StandaloneWindows64;
        ScriptCompilationSettings scriptCompilationSettings = new ScriptCompilationSettings();
        scriptCompilationSettings.group = BuildPipeline.GetBuildTargetGroup(target);
        scriptCompilationSettings.target = target;

        // 获取 dll 输出目录
        string outDir = Application.dataPath;
        outDir = outDir.Substring(0, outDir.IndexOf("Assets"));
        outDir = string.Format("{0}DLL", outDir);
        if (!Directory.Exists(outDir))
        {
            Directory.CreateDirectory(outDir);
        }

        // 编译DLL
        ScriptCompilationResult scriptCompilationResult = PlayerBuildInterface.CompilePlayerScripts(scriptCompilationSettings, outDir);
        foreach(var ass in scriptCompilationResult.assemblies)
        {
            Debug.LogFormat("compile assemblies:{0}/{1}", outDir, ass);
        }
    }

}

在这里插入图片描述
点击 Tools->CompileDll 按钮,在 代码中 outDir 目录下生成所有 dll 以及 对应的 .pdb
在这里插入图片描述
如上 DLL 程序集是如何确定名字的?
如上 AA.dll
在 需要生成 AA.dll 的 C# 脚本目录下,新建 AA.asmdef
在 C# 脚本所在目录 右键 Create -> Assembly Definition
在这里插入图片描述
然后生成一个 .asmdef 的文件,选择文件在 Inspector 面板输入名字
在这里插入图片描述

然后在 AA.asmdef 目录新建一个 Npc.cs
在这里插入图片描述
选择 Npc.cs 看 Inspector 面板可以看到 Npc.cs 归属于 AA.dll 程序集
具体细节可以学习了解 Unity asmdef 文件的创建以及作用

在项目中任意选一个 C# 脚本,都可以在 Inspector 面板看到它归属的 dll 程序集
在这里插入图片描述
这就是上面执行编译导出的 dll 的来源

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将C++代码打包Unity调用,可以通过以下步骤: 1. 创建一个C++库项目,并将其编译为DLL文件。 2. 在Unity中创建一个C#脚本,使用DllImport特性引用DLL文件中的函数。 3. 在C#脚本中调用DLL函数。 下面是一个简单的示例: 1. 创建C++库项目,并将其编译为DLL文件。 首先,创建一个C++库项目并实现一些函数。例如,下面是一个简单的Add函数,它将两个整数相加并返回结果: ``` // MyMathLib.h #ifdef MYMATHLIB_EXPORTS #define MYMATHLIB_API __declspec(dllexport) #else #define MYMATHLIB_API __declspec(dllimport) #endif extern "C" MYMATHLIB_API int Add(int a, int b); ``` ``` // MyMathLib.cpp #include "MyMathLib.h" int Add(int a, int b) { return a + b; } ``` 在这里,我们使用了`__declspec(dllexport)`和`__declspec(dllimport)`来指示编译器导出和导入DLL函数。`MYMATHLIB_API`宏用于将函数标记为导出或导入。 2. 在Unity中创建一个C#脚本,使用DllImport特性引用DLL文件中的函数。 在Unity中,我们可以创建一个C#脚本,并使用DllImport特性来引用DLL文件中的函数。例如,下面是一个简单的例子: ``` // MyMathLib.cs using System.Runtime.InteropServices; public static class MyMathLib { [DllImport("MyMathLib.dll")] public static extern int Add(int a, int b); } ``` 在这里,我们使用DllImport特性来引用DLL文件中的Add函数。`"MyMathLib.dll"`是DLL文件的名称。 3. 在C#脚本中调用DLL函数。 现在,我们可以在C#脚本中调用DLL函数。例如,下面是一个简单的测试: ``` // Test.cs using UnityEngine; public class Test : MonoBehaviour { void Start() { int result = MyMathLib.Add(2, 3); Debug.Log("Result: " + result); // Output: Result: 5 } } ``` 在这里,我们使用`MyMathLib.Add`来调用DLL文件中的Add函数,并将结果打印到Unity控制台中。 这就是将C++代码打包Unity调用的基本步骤。当然,具体实现可能因项目而异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值