我有一个使用dll的Unity项目。
这是我尝试使用的示例:https://www.youtube.com/watch?v=C6V1f86x058
为了使其正常运行,我:
我的脚本是
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Rendering;
public class TestScript : MonoBehaviour
{
//the name of the DLL you want to load stuff from
private const string pluginName = "AndroidNativeLib";
//native interface
[DllImport(pluginName)]
private static extern IntPtr getEventFunction();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void DebugDelegate(string str);
static void CallBackFunction(string str) { Debug.Log(str); }
[DllImport(pluginName)]
public static extern void SetDebugFunction(IntPtr fp);
private CommandBuffer cmd;
// Start is called before the first frame update
void Start()
{
DebugDelegate callback_delegate = new DebugDelegate(CallBackFunction);
// Convert callback_delegate into a function pointer that can be
// used in unmanaged code.
IntPtr intptr_delegate =
Marshal.GetFunctionPointerForDelegate(callback_delegate);
// Call the API passing along the function pointer.
SetDebugFunction(intptr_delegate);
//crating the command buffer and attaching it to camera
cmd = new CommandBuffer();
cmd.name = pluginName;
var camera = Camera.main;
camera.AddCommandBuffer(CameraEvent.AfterGBuffer, cmd);
}
// Update is called once per frame
void Update()
{
cmd.IssuePluginEvent(getEventFunction(), 0);
}
}
我的dll是:
#include "stdafx.h"
#include "IUnityGraphics.h"
//make sure this appears before IUnityGraphicsD3D11
#include "d3d11.h"
#include "IUnityGraphicsD3D11.h"
// debug event
typedef void(*FuncPtr)(const char *);
FuncPtr Debug;
namespace globals {
ID3D11Device *device = nullptr;
ID3D11DeviceContext *context = nullptr;
} // namespace globals
extern "C" {
UNITY_INTERFACE_EXPORT void SetDebugFunction(FuncPtr fp) { Debug = fp; }
// Plugin function to handle a specific rendering event
static void UNITY_INTERFACE_API UNITY_INTERFACE_API OnRenderEvent(int eventID) {
Debug("Hello world");
}
// Unity plugin load event
void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
UnityPluginLoad(IUnityInterfaces *unityInterfaces) {
auto s_UnityInterfaces = unityInterfaces;
IUnityGraphicsD3D11 *d3d11 = unityInterfaces->Get<IUnityGraphicsD3D11>();
globals::device = d3d11->GetDevice();
globals::device->GetImmediateContext(&globals::context);
}
// Unity plugin unload event
void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload() {}
// Freely defined function to pass a callback to plugin-specific scripts
UnityRenderingEvent UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
getEventFunction() {
return OnRenderEvent;
}
}
在运行此示例时,我看不到任何输出,并且看起来不起作用。因此,我尝试对其进行调试。我可以在C#脚本中获取调试点,但是如果我尝试从dll代码中获取调试点,就会注意到。
所以,问题是-如何调试dll代码?
答案 0 :(得分:0)
此帮助吗:https://forum.unity.com/threads/how-to-build-and-debug-external-dlls.161685/
- 创建一个使用通配符查找所有源代码的.csproj文件
- 将此C#项目添加到Visual Studio解决方案中
- 使用MSBuild构建后目标将Visual Studio的PDB符号转换为Mono的MDB格式
- 从Unity正常使用MonoDevelop进行调试
- 源代码不应位于Assets文件夹中,但生成的DLL应该位于