法一: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.</ProductVersion>

    <SchemaVersion>2.0</SchemaVersion>

    <ProjectGuid>{61A89EEC-36B9-49ED--D6A7DBDD9EA7}</ProjectGuid>

    <OutputType>Library</OutputType>

    <RootNamespace>UnityDLLExample</RootNamespace>

    <AssemblyName>UnityDLLExample</AssemblyName>

    <TargetFrameworkVersion>v3.</TargetFrameworkVersion>

    <TargetFrameworkProfile>

    </TargetFrameworkProfile>

    <FileAlignment></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 .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></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></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
 
 

Unity: How to build and debug external DLLs的更多相关文章

  1. 大杂烩 Classpath / Build path / Debug关联源码 / JDK&JRE区别

    Classpath的理解及其使用方式 原文地址:http://blog.csdn.net/wk1134314305/article/details/77940147?from=bdhd_site 摘要 ...

  2. 从Unity引擎过度到Unreal4引擎(最终版)

    原文地址:http://demo.netfoucs.com/u011707076/article/details/44036839 前言 寒假回家到现在已经有十多天了,这些天回家不是睡就是吃....哎 ...

  3. Unity Glossary

    https://docs.unity3d.com/2018.4/Documentation/Manual/Glossary.html 2D terms 2D Physics terms AI term ...

  4. 面向Unity程序员的Android快速上手教程

    作者:Poan,腾讯移动客户端开发 工程师 商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. WeTest 导读 随着Unity.cocos2dx等优秀跨平台游戏引擎的出现,开发者可以把 ...

  5. How to build the Robotics Library from source code on Windows

    The Robotics Library is an open source C++ library for robot kinematics, motion planning and control ...

  6. Unity中各个平台的预编译的运用方式

    1,unity中官方文档的一个操纵关键词   Platform Dependent Compilation 2,常用的预编译关键词    UNITY_EDITOR    编辑器调用.UNITY_STA ...

  7. Solve Error Debug Assertion Failed Expression vector iterators incompatible Using PCL in Release Mode of VS2010

    When using PCL 1.4.0 in the release mode building under VS2010, we might sometime get the error &quo ...

  8. debug,trace,release项目配置区别

    Debug模式是用来调试用的,它生成的执行文件中含有大量调试信息,所以很大: Release模式生成的执行文件消除了这些调试信息,可用来作为成品发布 Debug只在debug状态下会输出,Trace在 ...

  9. Android 中获取 debug 测试 SHA1 和 release SHA1 证书指纹数据的方法

    百度地图开发的时候要申请KEY,需要提供SHA1证书指纹数据 Eclipse eclipse中直接查看:windows -> preferance -> android -> bui ...

随机推荐

  1. 开启CURL扩展,让服务器支持PHP curl函数(远程采集)

    关于开启Curl的方法模板天下小编在此给大家简单说一下 curl().file_get_contents().snoopy.class.php这三个远程页面抓取或采集中用到的工具,默迹还是侵向于用sn ...

  2. Codeforces Round #215 (Div. 1)

    A Sereja and Algorithm 题意:给定有x,y,z组成的字符串,每次询问某一段s[l, r]能否变成变成zyxzyx的循环体. 分析: 分析每一段x,y,z数目是否满足构成循环体,当 ...

  3. 163. Missing Ranges

    题目: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return it ...

  4. Java按位置解析文本文件(使用Swing选择文件)

    工作中遇到这样的一个需求,按位置解析一些文本文件,它们由头部.详情.尾部组成,并且每一行的长度可能不一样,每一行代表的意思也可能不一样,但是每一行各个位置代表的含义已经确定了. 例如有下面这样一段文本 ...

  5. 通过jQuery或ScriptManager以Ajax方式访问服务

    1.客户端和服务端 服务端对外提供服务时,可以通过handler或者webservice.handler比较轻便,但是难以对外公开,只有程序员自己知道它到底做了些什么工作.webservice可以将服 ...

  6. net中System.Security.Cryptography 命名空间 下的加密算法

    .net中System.Security.Cryptography命名空间 在.NETFramework出现之前,如果我们需要进行加密的话,我们只有各种较底层的技术可以选择,如 Microsoft C ...

  7. ODBC具体使用

    应用程序 应用程序对外提供使用者交谈界面,同时对内执行资料之准备工作数据库系统所传回来的结果在显示给使用者看.简单来说,应用程序即ODBC 界面执行下列主要工作:1. Request a connec ...

  8. Android开发之ListView-BaseAdapter的使用

    ListView优化原则: UI优化: listview条目与条目之间的间隙的分割内容 : android:divider="@android :color/transparent" ...

  9. 机器学习&深度学习经典资料汇总,data.gov.uk大量公开数据

    <Brief History of Machine Learning> 介绍:这是一篇介绍机器学习历史的文章,介绍很全面,从感知机.神经网络.决策树.SVM.Adaboost到随机森林.D ...

  10. Apache ‘mod_pagespeed’模块跨站脚本漏洞

    漏洞名称: Apache ‘mod_pagespeed’模块跨站脚本漏洞 CNNVD编号: CNNVD-201310-677 发布时间: 2013-11-05 更新时间: 2013-11-05 危害等 ...