我有一个使用dll的统一项目。
这是我尝试使用的一个示例:https://www.youtube.com/watch?v=C6V1f86x058
为了使它发挥作用,我:
下添加了dll
我的剧本是
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代码?
发布于 2020-09-13 10:30:58
这有帮助吗:https://forum.unity.com/threads/how-to-build-and-debug-external-dlls.161685/
normal
https://stackoverflow.com/questions/63869641
复制相似问题