如何将Unity3D中的脚本打包成为DLL类库?

如果我们想把代码打成DLL,首先需要有一个Assembly和一个合理的代码目录结构规划。

1.将要归为一类的脚本放进同一个文件内,在该文件夹下右键创建一个Assembly Definition,默认它会将同文件夹以及子目录内的脚本归为一个Assembly。

没有描述

2.在Editor下创建一个CompileDll脚本来将我们的脚本一键打包成DLL
using UnityEditor;
using System.IO;
using UnityEngine;
using UnityEditor.Build.Player;

public class CompileDLLHelper
{
    
    
    [MenuItem("HTools/CompileDlls")]
    public static void CompileDll()
    {
    
    

        var tempOutputPath = $"{
      
      Application.dataPath}/../Dlls";
        Directory.CreateDirectory(tempOutputPath);

        ScriptCompilationSettings scriptCompilationSettings = new ScriptCompilationSettings();
        scriptCompilationSettings.group = BuildPipeline.GetBuildTargetGroup(BuildTarget.StandaloneWindows64);
        scriptCompilationSettings.target = BuildTarget.StandaloneWindows64;

        PlayerBuildInterface.CompilePlayerScripts(scriptCompilationSettings, tempOutputPath);
    }
}
3.在编辑窗口点击HTools–>CompileDlls

在这里插入图片描述

最后我们在当前工程目录下的Dlls(与Assets文件夹同目录)文件夹内就可以看到我们打包出的所有DLL了。在这里插入图片描述

TIps(PDB文件时是指“程序数据库”文件,是打包时一起带出来的)

猜你喜欢

转载自blog.csdn.net/qq_30163099/article/details/126263499