2025-11-26 14:35:58 +08:00
|
|
|
/*
|
2025-11-27 10:15:03 +08:00
|
|
|
* Tracy Unity Plugin - Simplified Implementation
|
2025-11-26 14:35:58 +08:00
|
|
|
*
|
2025-11-27 10:15:03 +08:00
|
|
|
* This is a simplified Tracy Unity Plugin implementation
|
|
|
|
|
* demonstrating how to integrate Tracy into Unity Native Plugin
|
2025-11-26 14:35:58 +08:00
|
|
|
*
|
2025-11-27 10:15:03 +08:00
|
|
|
* Build instructions:
|
|
|
|
|
* Windows: Use CMake (see CMakeLists.txt)
|
|
|
|
|
* macOS: Use CMake (see CMakeLists.txt)
|
|
|
|
|
* Linux: Use CMake (see CMakeLists.txt)
|
2025-11-26 14:35:58 +08:00
|
|
|
*/
|
|
|
|
|
|
2025-11-27 10:15:03 +08:00
|
|
|
#pragma warning(disable:4100)
|
2025-11-26 14:35:58 +08:00
|
|
|
|
|
|
|
|
#include "tracy/Tracy.hpp"
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <cstring>
|
2025-11-27 16:46:51 +08:00
|
|
|
#include <map>
|
2025-11-27 10:15:03 +08:00
|
|
|
#include <stack>
|
|
|
|
|
|
|
|
|
|
// Platform specific export definition
|
2025-11-26 14:35:58 +08:00
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
#define UNITY_PLUGIN_EXPORT __declspec(dllexport)
|
|
|
|
|
#elif defined(__APPLE__) || defined(__linux__)
|
|
|
|
|
#define UNITY_PLUGIN_EXPORT __attribute__((visibility("default")))
|
|
|
|
|
#else
|
|
|
|
|
#define UNITY_PLUGIN_EXPORT
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-11-27 16:46:51 +08:00
|
|
|
std::map<std::string, tracy::SourceLocationData> sourceLocationCache;
|
|
|
|
|
std::map<std::string, std::string> zoneFunctionNameCache;
|
|
|
|
|
std::map<std::string, std::string> zoneFilePathCache;
|
|
|
|
|
std::map<std::string, std::stack<tracy::ScopedZone*>> zoneStackCache;
|
2025-11-26 14:35:58 +08:00
|
|
|
|
2025-11-27 10:15:03 +08:00
|
|
|
// C export functions (Unity requires C linkage)
|
|
|
|
|
extern "C"
|
2025-11-26 14:35:58 +08:00
|
|
|
{
|
2025-11-27 10:15:03 +08:00
|
|
|
/**
|
|
|
|
|
* Initialize Tracy
|
|
|
|
|
* Unity C# call: [DllImport] private static extern void TracyInit();
|
|
|
|
|
*/
|
|
|
|
|
UNITY_PLUGIN_EXPORT void TracyInit()
|
|
|
|
|
{
|
|
|
|
|
// Tracy initializes automatically, additional initialization logic can be added here
|
|
|
|
|
}
|
2025-11-26 14:35:58 +08:00
|
|
|
|
2025-11-27 10:15:03 +08:00
|
|
|
/**
|
|
|
|
|
* Shutdown Tracy
|
|
|
|
|
* Unity C# call: [DllImport] private static extern void TracyShutdown();
|
|
|
|
|
*/
|
|
|
|
|
UNITY_PLUGIN_EXPORT void TracyShutdown()
|
|
|
|
|
{
|
2025-11-27 16:46:51 +08:00
|
|
|
|
2025-11-27 10:15:03 +08:00
|
|
|
}
|
2025-11-26 14:35:58 +08:00
|
|
|
|
2025-11-27 10:15:03 +08:00
|
|
|
/**
|
|
|
|
|
* Mark frame boundary
|
|
|
|
|
* Unity C# call: [DllImport] private static extern void TracyFrameMark();
|
|
|
|
|
*/
|
|
|
|
|
UNITY_PLUGIN_EXPORT void TracyFrameMark()
|
2025-11-26 14:35:58 +08:00
|
|
|
{
|
2025-11-27 10:15:03 +08:00
|
|
|
FrameMark;
|
2025-11-26 14:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-27 10:15:03 +08:00
|
|
|
/**
|
|
|
|
|
* 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)
|
2025-11-26 14:35:58 +08:00
|
|
|
{
|
2025-11-27 10:15:03 +08:00
|
|
|
if (name != nullptr)
|
|
|
|
|
{
|
|
|
|
|
TracyPlot(name, value);
|
|
|
|
|
}
|
2025-11-26 14:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-27 10:15:03 +08:00
|
|
|
/**
|
|
|
|
|
* Send message
|
|
|
|
|
* Unity C# call: [DllImport] private static extern void TracySendMessage(string message);
|
|
|
|
|
*/
|
|
|
|
|
UNITY_PLUGIN_EXPORT void TracySendMessage(const char* message)
|
2025-11-26 14:35:58 +08:00
|
|
|
{
|
2025-11-27 10:15:03 +08:00
|
|
|
if (message != nullptr)
|
|
|
|
|
{
|
|
|
|
|
TracyMessage(message, std::strlen(message));
|
|
|
|
|
}
|
2025-11-26 14:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-27 10:15:03 +08:00
|
|
|
/**
|
|
|
|
|
* Set thread name
|
|
|
|
|
* Unity C# call: [DllImport] private static extern void TracySetThreadName(string name);
|
|
|
|
|
*/
|
|
|
|
|
UNITY_PLUGIN_EXPORT void TracySetThreadName(const char* name)
|
2025-11-26 14:35:58 +08:00
|
|
|
{
|
2025-11-27 10:15:03 +08:00
|
|
|
if (name != nullptr)
|
|
|
|
|
{
|
|
|
|
|
tracy::SetThreadName(name);
|
|
|
|
|
}
|
2025-11-26 14:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-27 10:15:03 +08:00
|
|
|
/**
|
|
|
|
|
* Unity plugin lifecycle function - called on load
|
|
|
|
|
*/
|
|
|
|
|
UNITY_PLUGIN_EXPORT void UnityPluginLoad()
|
|
|
|
|
{
|
|
|
|
|
// Optional: perform initialization when plugin loads
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unity plugin lifecycle function - called on unload
|
|
|
|
|
*/
|
|
|
|
|
UNITY_PLUGIN_EXPORT void UnityPluginUnload()
|
|
|
|
|
{
|
|
|
|
|
// Optional: perform cleanup when plugin unloads
|
|
|
|
|
TracyShutdown();
|
|
|
|
|
}
|
2025-11-26 14:35:58 +08:00
|
|
|
|
2025-11-27 16:46:51 +08:00
|
|
|
UNITY_PLUGIN_EXPORT void TracyZoneScopedBegin(
|
|
|
|
|
const char* name,
|
|
|
|
|
const char* functionName,
|
|
|
|
|
const char* filePath,
|
|
|
|
|
int lineNumber
|
|
|
|
|
)
|
2025-11-26 14:35:58 +08:00
|
|
|
{
|
2025-11-27 16:46:51 +08:00
|
|
|
std::string nameStr(name);
|
|
|
|
|
auto iter = sourceLocationCache.find(nameStr);
|
|
|
|
|
if (iter == sourceLocationCache.end())
|
2025-11-26 14:35:58 +08:00
|
|
|
{
|
2025-11-27 16:46:51 +08:00
|
|
|
zoneFunctionNameCache[nameStr] = functionName;
|
|
|
|
|
zoneFilePathCache[nameStr] = filePath;
|
|
|
|
|
iter = sourceLocationCache.insert(sourceLocationCache.end(), std::make_pair(nameStr, tracy::SourceLocationData{}));
|
|
|
|
|
iter->second.name = iter->first.c_str();
|
|
|
|
|
iter->second.function = zoneFunctionNameCache[nameStr].c_str();
|
|
|
|
|
iter->second.file = zoneFilePathCache[nameStr].c_str();
|
|
|
|
|
iter->second.line = static_cast<uint32_t>(lineNumber);
|
|
|
|
|
iter->second.color = 0;
|
2025-11-26 14:35:58 +08:00
|
|
|
}
|
2025-11-27 16:46:51 +08:00
|
|
|
zoneStackCache[nameStr].push(new tracy::ScopedZone(&iter->second, 0, true));
|
2025-11-26 14:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-27 16:46:51 +08:00
|
|
|
UNITY_PLUGIN_EXPORT void TracyZoneScopedEnd(const char* name)
|
2025-11-26 14:35:58 +08:00
|
|
|
{
|
2025-11-27 16:46:51 +08:00
|
|
|
delete zoneStackCache[name].top();
|
|
|
|
|
zoneStackCache[name].pop();
|
2025-11-26 14:35:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // extern "C"
|