法一:http://forum.unity3d.com/threads/161685-How-to-build-and-debug-external-DLLs

http://docs.unity3d.com/Documentation/Manual/UsingDLL.html

法二:http://unityvs.com/documentation/dll-debugging/

We all throw dozens or hundreds of scripts into our Assets folder and let Unity build and load them. Usually this works well, but there are a number of reasons for wanting to compile at least some of your code into a DLL. This post outlines a method for building code with Visual Studio and still being able to debug it with MonoDevelop.

For context, I'm using Windows 7 64-bit, Visual C# 2010 Express, Unity 3.5.6 and MonoDevelop 2.8.2 (shipped with Unity 3.5.6). The information should apply to other versions and possibly even to Mac development, but I haven't tested it.

The basic approach is:

  1. create a .csproj file that uses wildcards to find all source code
  2. add this C# project to a Visual Studio solution
  3. use an MSBuild post-build target to convert Visual Studio's PDB symbols to Mono's MDB format
  4. debug with MonoDevelop from Unity, as normal
  5. source code should -not- be in the Assets folder, but the generated DLL should be

Here's a handmade .csproj file that demonstrates how this works:

<?xml version="1.0" encoding="utf-8"?><Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><!-- Common Properties --><PropertyGroup><Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration><Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform><ProductVersion>8.0.30703</ProductVersion><SchemaVersion>2.0</SchemaVersion><ProjectGuid>{61A89EEC-36B9-49ED-8431-D6A7DBDD9EA7}</ProjectGuid><OutputType>Library</OutputType><RootNamespace>UnityDLLExample</RootNamespace><AssemblyName>UnityDLLExample</AssemblyName><TargetFrameworkVersion>v3.5</TargetFrameworkVersion><TargetFrameworkProfile></TargetFrameworkProfile><FileAlignment>512</FileAlignment><!-- Look up Unity install folder, and set the ReferencePath for locating managed assembly references. --><UnityInstallFolder>$([System.IO.Path]::GetDirectoryName($(registry:HKEY_CURRENT_USER\Software\Unity Technologies\Unity Editor 3.x\Location)))</UnityInstallFolder><ReferencePath>$(UnityInstallFolder)\Data\Managed</ReferencePath><MonoMdbGenerator>$(UnityInstallFolder)\Data\MonoBleedingEdge\lib\mono\4.0\pdb2mdb.exe</MonoMdbGenerator></PropertyGroup><!-- Debug Properties --><PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "><PlatformTarget>AnyCPU</PlatformTarget><DebugSymbols>true</DebugSymbols><DebugType>full</DebugType><Optimize>false</Optimize><OutputPath>Assets\Plugins</OutputPath><DefineConstants>DEBUG;UNITY_3_5</DefineConstants><ErrorReport>prompt</ErrorReport><WarningLevel>4</WarningLevel><TreatWarningsAsErrors>true</TreatWarningsAsErrors></PropertyGroup><!-- Release Properties --><PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "><PlatformTarget>AnyCPU</PlatformTarget><DebugType>pdbonly</DebugType><Optimize>true</Optimize><OutputPath>Assets\Plugins</OutputPath><DefineConstants>UNITY_3_5</DefineConstants><ErrorReport>prompt</ErrorReport><WarningLevel>4</WarningLevel></PropertyGroup><PropertyGroup><StartupObject /></PropertyGroup><!-- Microsoft standard stuff. --><Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /><!-- Find all source --><ItemGroup><Compile Include="scripts\**\*.cs" /></ItemGroup><!-- References --><ItemGroup><Reference Include="System" /><Reference Include="UnityEngine"><Private>False</Private></Reference></ItemGroup><!-- Use AfterUnityBuild from Blackbird.CSharp.targets. --><Target Name="AfterBuild"><CallTarget Targets="GenerateMonoSymbols" Condition=" Exists('$(OutputPath)\$(AssemblyName).pdb') " /></Target><Target Name="GenerateMonoSymbols"><Message Text="$(ProjectName) -> $(TargetPath).mdb" Importance="High" /><Exec Command="&quot;$(MonoMdbGenerator)&quot; $(AssemblyName).dll" WorkingDirectory="$(MSBuildProjectDirectory)\$(OutputPath)" /></Target></Project>

To use this build file, save it as "UnityDLLExample.csproj" in a project folder with the following layout:

-project/
-----UnityDLLExample.csproj (from this post)
-----UnityDLLExample.sln (create with Visual Studio)
-----scripts/
---------SomeScript.cs (put your source code in here, use sub-folders if you like)
-----Assets/ (Unity assets folder)
---------Plugins/ (DLL and MDB files go here)

Notes:

  • MonoDevelop doesn't support the Include="**\*.cs" wildcard syntax, so you can't build this project with MonoDevelop
  • you *can* build with the MSBuild command line if you don't want to open Visual Studio
  • if you have editor scripts mixed in with game scripts, you can find them with Include="**\Editor\**\*.cs"
  • if your editor and game scripts are inter-mixed then you need to Include the editor scripts in the editor csproj and Exclude them from the game csproj
  • Unity comes with two versions of pdb2mdb.exe -- only the one in the MonoBleedingEdge folder works for coroutines (that return IEnumerator)
  • for hand-building .csproj files, you can get GUIDs from http://www.guidgen.com/

This thread was started after a bunch of thought and discussion on this other thread. Thanks to @guavaman for getting the ball rolling.

 Originally Posted by amirabiri 
Is anyone else getting this exception?

Code:  
Unhandled Exception: System.TypeInitializationException: The type initializer for 'Mono.Cecil.Metadata.TableHeap' threw an exception. ---> System.ArgumentException: Value does not fall within the expected range.

at System.Runtime.CompilerServices.RuntimeHelpers.InitializeArray(Array array, RuntimeFieldHandle fldHandle)

at Mono.Cecil.Metadata.TableHeap..cctor()

--- End of inner exception stack trace ---

at Mono.Cecil.PE.ImageReader.ReadTableHeap()

at Mono.Cecil.PE.ImageReader.ReadImage()

at Mono.Cecil.PE.ImageReader.ReadImageFrom(Stream stream)

at Mono.Cecil.ModuleDefinition.ReadModule(Stream stream, ReaderParameters parameters)

at Mono.Cecil.ModuleDefinition.ReadModule(String fileName, ReaderParameters parameters)

at Pdb2Mdb.Driver.Main(String[] args)

This doesn't seem to be related to the to the project, the same thing happens when I run the command from the command line.

 Originally Posted by TaiChiAnt 
I have the same problem as well. 2.0 pdb2mdb.exe solved this problem but not the IEnumerator Issue. :(

We had the same problems. Using pdb2mdb.exe from 'C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0\pdb2mdb. exe' worked as long as we did not use IEnumerator and yield. When our *.dlls contained yield statements, pdb2mdb.exe always crashed with the following stacktrace:

Code:  
System.ArgumentNullException: Argument cannot be null.

Parametername: source

at System.Linq.Check.SourceAndPredicate (System.Object source, System.Object predicate) [0x00000] in <filename unknown>:0

at System.Linq.Enumerable.Where[PdbLines] (IEnumerable`1 source, System.Func`2 predicate) [0x00000] in <filename unknown>:0

at Pdb2Mdb.Converter.GetSourceFile (Mono.CompilerServices.SymbolWriter.MonoSymbolWriter mdb, Microsoft.Cci.Pdb.PdbFunction function) [0x00000] in <filename unknown>:0

at Pdb2Mdb.Converter.ConvertFunction (Microsoft.Cci.Pdb.PdbFunction function) [0x00000] in <filename unknown>:0

at Pdb2Mdb.Converter.Convert (Mono.Cecil.AssemblyDefinition assembly, IEnumerable`1 functions, Mono.CompilerServices.SymbolWriter.MonoSymbolWriter mdb) [0x00000] in <filename unknown>:0

at Pdb2Mdb.Driver.Convert (Mono.Cecil.AssemblyDefinition assembly, System.IO.Stream pdb, Mono.CompilerServices.SymbolWriter.MonoSymbolWriter mdb) [0x00000] in <filename unknown>:0

When switching to the other pdb2mdb.exe located at 'C:\Program Files (x86)\Unity\Editor\Data\MonoBleedingEdge\lib\mono\ 4.0\pdb2mdb.exe', it worked for some of our PCs but crashed on others with the error given by amirabiri. Doing some research we found that this is related to the mono version used to execute pdb2mdb.exe. We could fix this issue by using the mono.exe located at 'C:\Program Files (x86)\Unity\Editor\Data\MonoBleedingEdge\bin\mono. exe'. The command looks like (make sure the cwd is where the dll is located)

Code:  
"C:\Program Files (x86)\Unity\Editor\Data\MonoBleedingEdge\bin\mono.exe" "C:\Program Files (x86)\Unity\Editor\Data\MonoBleedingEdge\lib\mono\4.0\pdb2mdb.exe" Your.dll

转载于:https://www.cnblogs.com/pulas/p/3718836.html

Unity: How to build and debug external DLLs相关推荐

  1. Unity Scripts Only Build

    腾讯G6开源Unity下LiveCoding实践 应用性: 在开发阶段,快速验证代码.资源在手机端表现的正确性,C#脚本.资源的热更新是很有必要的功能! Unity2019.1中新增功能: Andro ...

  2. build unity 丢失_Unity Build PS4文件时的一些坑

    一.申请Services ID:(这个步骤是在https://ps4.siedev.net/上创建一个产品时必须要做的,这个Services ID不需要在Unity上填写,但是它是ContenID的组 ...

  3. Unity程序在VR一体机(Android)上卡死(闪退)后怎么办?——用adb查看android上某Unity app的debug信息

    一.之前面临的困境 Unity的程序build到android一体机后,仿佛进入了一个黑箱子,你既看不到脚本的debug报错信息,也看不到任务管理器里的内存和CPU使用情况?如果黑屏.闪屏.花屏怎么办 ...

  4. [unity] build项目报错:Currently selected scripting backend (.NET)is not installed

    [Unity] Currently selected scripting backend .NET is notinstalled Unity build项目报错,没有安装 .NET 问题概况 Uni ...

  5. vscode build debug

    构建工程 Terminal > Configure Default Build Task 提示选择你的编译器,之后自动生成类似如下代码 {"version": "2 ...

  6. unity build设置_Microsoft Build上的Unity:展会上的7个重点

    unity build设置 The Microsoft Build 2018 developer conference just wrapped up in Seattle and Unity was ...

  7. 腾讯游戏学院专家分析:Unity在移动设备的GPU内存机制

    导语CPU和GPU是共享一份内存的吗?腾讯游戏学院专家Donald将在本文尝试以一张贴图纹理的虚拟内存占用为例,解答一些内存方面的问题.本篇主要分析iOS系统,后续会更新安卓篇. 开发手机游戏时,常听 ...

  8. [Unity3D] Unity3D连接安卓设备调试unity程序

    目录 一.手机开启调试模式,确保adb能检测到手机 目的:确保adb能检测到手机,通过adb devices命令能够呈现如下效果 常见问题: 二.unity配置工作 目的:配置unity中Build ...

  9. 1709 ltsb 内存占用_腾讯游戏学院专家分析:Unity在移动设备的GPU内存机制

    导语 CPU和GPU是共享一份内存的吗?腾讯游戏学院专家Donald将在本文尝试以一张贴图纹理的虚拟内存占用为例,解答一些内存方面的问题.本篇主要分析iOS系统,后续会更新安卓篇. 开发手机游戏时,常 ...

最新文章

  1. 快速得到两个list中不同部分的list
  2. nvidia:未找到命令
  3. 美卫星拍最详细3D地图 覆盖地球99%大陆
  4. YCSB benchmark测试mongodb性能——和web服务器测试性能结果类似
  5. 优化Linux下的内核TCP参数来提高服务器负载能力
  6. Android开发之自定义控件的基本介绍(附源码)
  7. JDK源码解析之 java.lang.ClassLoader
  8. Freezer - 备份云硬盘实现
  9. php header apk,php常用的header头
  10. 【古典入门】巴洛克音乐家-斯卡拉蒂
  11. mysql性能优化学习_mysql学习——性能优化之sql优化
  12. Fade out transition effect using CSS3
  13. oracle用户权限管理
  14. BZOJ1486: [HNOI2009]最小圈
  15. iOS:URL Scheme(完结)(18-1-3更)
  16. hex(base16)、base32、base64三种编码方式区别
  17. 计算机信息资源管理岗位,信息资源管理专业的可以报考公务员的哪些职位?
  18. 华为u8500在usb模式下logcat无法打印信息
  19. a8处理器相当于骁龙几_曾受人追捧的A8处理器,放在现在是什么水平了?
  20. Mockito简单实用教程 - 第一部分mock

热门文章

  1. 交易所撮合引擎原理及实现代码
  2. 警惕url跳转到钓鱼网站--淘宝二手经历
  3. 《孙子兵法》十三篇注译(3--作战篇)
  4. layui xm-select用法
  5. ORACLE审计管理
  6. 千锋内部邮件曝光:学员高就业率的背后…
  7. Associations - 关联
  8. android ios 垃圾回收,iOS 面试题(16):解释垃圾回收的原理
  9. Android登陆界面设计demo
  10. Semantic UI 之 手风琴 accordion