完成可编译

This commit is contained in:
2025-11-27 10:15:03 +08:00
parent 43de86ee31
commit 7ee76113c9
11 changed files with 192 additions and 1276 deletions

View File

@@ -1,24 +1,29 @@
/*
* Tracy Unity Plugin - 简化版实现
* Tracy Unity Plugin - Simplified Implementation
*
* 这是一个简化的 Tracy Unity Plugin 实现
* 用于演示如何将 Tracy 集成到 Unity Native Plugin
* This is a simplified Tracy Unity Plugin implementation
* demonstrating how to integrate Tracy into Unity Native Plugin
*
* 编译说明:
* Windows: cl /LD /MD SimplifiedPlugin.cpp /I"path/to/tracy/public" ws2_32.lib dbghelp.lib
* macOS: clang++ -shared -fPIC SimplifiedPlugin.cpp -I"path/to/tracy/public" -o libUnityTracyPlugin.dylib
* Linux: g++ -shared -fPIC SimplifiedPlugin.cpp -I"path/to/tracy/public" -o libUnityTracyPlugin.so -lpthread -ldl
* Build instructions:
* Windows: Use CMake (see CMakeLists.txt)
* macOS: Use CMake (see CMakeLists.txt)
* Linux: Use CMake (see CMakeLists.txt)
*/
// 定义 Tracy 启用标志
#define TRACY_ENABLE
#define TRACY_ON_DEMAND
#pragma warning(disable:4100)
#include "tracy/Tracy.hpp"
#include <string>
#include <cstring>
// 平台特定的导出定义
#ifdef ADVANCED_ZONE_MANAGEMENT
#include <unordered_map>
#include <stack>
#include <mutex>
#include <thread>
#endif
// Platform specific export definition
#if defined(_WIN32) || defined(_WIN64)
#define UNITY_PLUGIN_EXPORT __declspec(dllexport)
#elif defined(__APPLE__) || defined(__linux__)
@@ -27,139 +32,166 @@
#define UNITY_PLUGIN_EXPORT
#endif
// C 导出函数Unity 需要 C 链接)
extern "C" {
/**
* 初始化 Tracy
* Unity C# 调用: [DllImport] private static extern void TracyInit();
*/
UNITY_PLUGIN_EXPORT void TracyInit()
{
// Tracy 会自动初始化,这里可以添加额外的初始化逻辑
// 例如:设置采样率、配置选项等
// Simple Zone Manager for basic Zone tracking
#ifndef ADVANCED_ZONE_MANAGEMENT
namespace {
struct SimpleZone {
const char* name;
int64_t startTime;
};
struct ThreadZones {
std::stack<SimpleZone> zones;
};
std::unordered_map<std::thread::id, ThreadZones> g_threadZones;
std::mutex g_zonesMutex;
}
#endif
/**
* 关闭 Tracy
* Unity C# 调用: [DllImport] private static extern void TracyShutdown();
*/
UNITY_PLUGIN_EXPORT void TracyShutdown()
// C export functions (Unity requires C linkage)
extern "C"
{
// Tracy 会在程序退出时自动清理
// 这里可以添加自定义的清理逻辑
}
/**
* 标记帧边界
* Unity C# 调用: [DllImport] private static extern void TracyFrameMark();
*/
UNITY_PLUGIN_EXPORT void TracyFrameMark()
{
FrameMark;
}
/**
* 绘制数值
* Unity C# 调用: [DllImport] private static extern void TracyPlotValue(string name, double value);
*/
UNITY_PLUGIN_EXPORT void TracyPlotValue(const char* name, double value)
{
if (name != nullptr)
/**
* Initialize Tracy
* Unity C# call: [DllImport] private static extern void TracyInit();
*/
UNITY_PLUGIN_EXPORT void TracyInit()
{
TracyPlot(name, value);
// Tracy initializes automatically, additional initialization logic can be added here
}
}
/**
* 发送消息
* Unity C# 调用: [DllImport] private static extern void TracyMessage(string message);
*/
UNITY_PLUGIN_EXPORT void TracyMessage(const char* message)
{
if (message != nullptr)
/**
* Shutdown Tracy
* Unity C# call: [DllImport] private static extern void TracyShutdown();
*/
UNITY_PLUGIN_EXPORT void TracyShutdown()
{
TracyMessage(message, std::strlen(message));
#ifndef ADVANCED_ZONE_MANAGEMENT
std::lock_guard<std::mutex> lock(g_zonesMutex);
g_threadZones.clear();
#endif
}
}
/**
* 设置线程名称
* Unity C# 调用: [DllImport] private static extern void TracySetThreadName(string name);
*/
UNITY_PLUGIN_EXPORT void TracySetThreadName(const char* name)
{
if (name != nullptr)
/**
* Mark frame boundary
* Unity C# call: [DllImport] private static extern void TracyFrameMark();
*/
UNITY_PLUGIN_EXPORT void TracyFrameMark()
{
tracy::SetThreadName(name);
FrameMark;
}
}
/**
* 开始一个命名的 Zone
* Unity C# 调用: [DllImport] private static extern void TracyZoneBegin(string name);
*
* 注意: 这是简化版实现,实际使用中需要更复杂的 Zone 管理
*/
UNITY_PLUGIN_EXPORT void TracyZoneBegin(const char* name)
{
if (name != nullptr)
/**
* Plot value
* Unity C# call: [DllImport] private static extern void TracyPlotValue(string name, double value);
*/
UNITY_PLUGIN_EXPORT void TracyPlotValue(const char* name, double value)
{
// 简化版: 使用动态分配的 Zone
// 实际应用中需要管理 Zone 的生命周期
if (name != nullptr)
{
TracyPlot(name, value);
}
}
/**
* Send message
* Unity C# call: [DllImport] private static extern void TracySendMessage(string message);
*/
UNITY_PLUGIN_EXPORT void TracySendMessage(const char* message)
{
if (message != nullptr)
{
TracyMessage(message, std::strlen(message));
}
}
/**
* Set thread name
* Unity C# call: [DllImport] private static extern void TracySetThreadName(string name);
*/
UNITY_PLUGIN_EXPORT void TracySetThreadName(const char* name)
{
if (name != nullptr)
{
tracy::SetThreadName(name);
}
}
/**
* Begin a named Zone
* Unity C# call: [DllImport] private static extern void TracyZoneBegin(string name);
*
* Note: This is a simplified implementation. For production use, consider using
* ADVANCED_ZONE_MANAGEMENT or a different Zone management approach.
*/
UNITY_PLUGIN_EXPORT void TracyZoneBegin(const char* name)
{
#ifndef ADVANCED_ZONE_MANAGEMENT
if (name != nullptr)
{
std::lock_guard<std::mutex> lock(g_zonesMutex);
auto threadId = std::this_thread::get_id();
SimpleZone zone;
zone.name = name;
zone.startTime = tracy::GetTime();
g_threadZones[threadId].zones.push(zone);
// Send zone begin event to Tracy
TracyMessageL(name);
}
#endif
}
/**
* End current Zone
* Unity C# call: [DllImport] private static extern void TracyZoneEnd();
*/
UNITY_PLUGIN_EXPORT void TracyZoneEnd()
{
#ifndef ADVANCED_ZONE_MANAGEMENT
std::lock_guard<std::mutex> lock(g_zonesMutex);
auto threadId = std::this_thread::get_id();
auto it = g_threadZones.find(threadId);
// 方案1: 使用全局 Zone 栈(简单但不支持多线程)
// 方案2: 使用线程局部存储(复杂但支持多线程)
// 方案3: 返回 Zone ID 给 C#,让 C# 管理(推荐)
// 这里使用宏创建一个 Zone
// 注意:这只是示例,实际使用需要更好的管理方式
ZoneName(name, std::strlen(name));
if (it != g_threadZones.end() && !it->second.zones.empty())
{
it->second.zones.pop();
}
#endif
}
}
/**
* 结束当前 Zone
* Unity C# 调用: [DllImport] private static extern void TracyZoneEnd();
*/
UNITY_PLUGIN_EXPORT void TracyZoneEnd()
{
// 简化版: 与 ZoneBegin 配对使用
// 实际应用中需要管理 Zone 的结束
}
/**
* Unity plugin lifecycle function - called on load
*/
UNITY_PLUGIN_EXPORT void UnityPluginLoad()
{
// Optional: perform initialization when plugin loads
}
/**
* Unity 插件生命周期函数 - 加载时调用
*/
UNITY_PLUGIN_EXPORT void UnityPluginLoad()
{
// 可选:在插件加载时执行初始化
}
/**
* Unity 插件生命周期函数 - 卸载时调用
*/
UNITY_PLUGIN_EXPORT void UnityPluginUnload()
{
// 可选:在插件卸载时执行清理
}
/**
* Unity plugin lifecycle function - called on unload
*/
UNITY_PLUGIN_EXPORT void UnityPluginUnload()
{
// Optional: perform cleanup when plugin unloads
TracyShutdown();
}
} // extern "C"
/*
* 高级实现示例 - Zone 管理
* Advanced Implementation Example - Zone Management
*
* 下面是一个更完善的 Zone 管理实现示例
* 可以根据需要扩展
* Below is a more complete Zone management implementation example
* Can be extended as needed
*/
#ifdef ADVANCED_ZONE_MANAGEMENT
#include <unordered_map>
#include <stack>
#include <mutex>
// Zone 管理器(线程安全)
// Zone Manager (Thread-safe)
class ZoneManager
{
private:
@@ -179,10 +211,10 @@ public:
auto threadId = std::this_thread::get_id();
auto& zones = threadZones[threadId].zones;
// 创建源位置信息
// Create source location info
static const tracy::SourceLocationData loc{name, function, file, line, 0};
// 创建 Zone注意需要在堆上分配
// Create Zone (note: must be heap allocated)
auto* zone = new tracy::ScopedZone(&loc, true);
zones.push(zone);
}
@@ -211,7 +243,7 @@ public:
if (it != threadZones.end())
{
// 清理所有未结束的 Zone
// Clean up all unfinished Zones
while (!it->second.zones.empty())
{
delete it->second.zones.top();
@@ -222,7 +254,7 @@ public:
}
};
// 全局 Zone 管理器实例
// Global Zone Manager instance
static ZoneManager g_zoneManager;
extern "C" {
@@ -247,41 +279,18 @@ UNITY_PLUGIN_EXPORT void TracyClearThreadZones()
#endif // ADVANCED_ZONE_MANAGEMENT
/*
* 编译和部署说明:
* Build and Deployment Instructions:
*
* 1. Windows (Visual Studio):
* - 创建 DLL 项目
* - 添加 Tracy 源文件: tracy/public/TracyClient.cpp
* - 包含目录: tracy/public
* - 链接库: ws2_32.lib dbghelp.lib
* - 输出: UnityTracyPlugin.dll
* Use CMake for all platforms (see CMakeLists.txt)
*
* 2. macOS:
* clang++ -std=c++17 -shared -fPIC \
* SimplifiedPlugin.cpp \
* tracy/public/TracyClient.cpp \
* -I tracy/public \
* -DTRACY_ENABLE -DTRACY_ON_DEMAND \
* -o libUnityTracyPlugin.dylib
* Quick build:
* 1. mkdir build && cd build
* 2. cmake .. -DTRACY_ROOT="/path/to/tracy"
* 3. cmake --build . --config Release
*
* 3. Linux:
* g++ -std=c++17 -shared -fPIC \
* SimplifiedPlugin.cpp \
* tracy/public/TracyClient.cpp \
* -I tracy/public \
* -DTRACY_ENABLE -DTRACY_ON_DEMAND \
* -lpthread -ldl \
* -o libUnityTracyPlugin.so
*
* 4. Android (NDK):
* 在 Android.mk 或 CMakeLists.txt 中配置
*
* 5. iOS:
* 使用 Xcode 编译为静态库 (.a)
*
* 部署到 Unity:
* - 将编译好的库文件复制到 Unity 项目的 Assets/Plugins/ 目录
* - 根据平台放置在相应的子目录中
* - 配置 Plugin Import Settings 以匹配目标平台
* Deploy to Unity:
* - Copy compiled library to Unity project Assets/Plugins/ directory
* - Place in appropriate subdirectory based on platform
* - Configure Plugin Import Settings to match target platform
*/