4.3.1. UnityNativeTool - Stub UnityInterfaces

A one file library that gets us the pointer to the UnityInterfaces. This library is not mocked.

Functions

void UnityPluginLoad(IUnityInterfaces *unityInterfaces)

Called when the plugin is loaded, this can be after/before Initialize()

Declared in IUnityInterface.h

Parameters
  • unityInterfaces: Unity class for accessing minimal Unity functionality exposed to C++ Plugins

IUnityInterfaces *GetUnityInterfacesPtr()

Allow us to retrieve this pointer from C#, so that we can manually call UnityPluginLoad() for mocked libraries

Variables

IUnityInterfaces *s_IUnityInterfaces = NULL

4.3.2. Unity C++ Plugin API

These files contain the (quite limited) Unity C++ API for plugins and is only really intended for special cases. These files are available from any Unity installation. They are located in <Unity Install Dir>/Editor/Data/PluginAPI , e.g. C:\Program Files\Unity\Hub\Editor\2019.3.2f1\Editor\Data\PluginAPI.

Warning

Comments here mainly reflect my understanding of the API! This part is just for reference, but if you really intend on using this you need to look at the source code (which also has a lot more comments).

4.3.2.1. IUnityInterface.h

The main file to handle the interface between the C++ library and Unity.

Defines

UNITY_INTERFACE_API

Contains appropriate calling convention that C# uses. Use this for C# delegates. https://en.wikipedia.org/wiki/X86_calling_conventions#stdcall

UNITY_INTERFACE_EXPORT

Prepend this to an extern “C” function to make it callable from C#.

UNITY_DECLARE_INTERFACE(NAME)
UNITY_REGISTER_INTERFACE_GUID(HASHH, HASHL, TYPE)
UNITY_REGISTER_INTERFACE_GUID_IN_NAMESPACE(HASHH, HASHL, TYPE, NAMESPACE)
UNITY_GET_INTERFACE_GUID(TYPE)
UNITY_GET_INTERFACE(INTERFACES, TYPE)

Typedefs

typedef struct UnityInterfaceGUID UnityInterfaceGUID
typedef void IUnityInterface
typedef struct IUnityInterfaces IUnityInterfaces
typedef struct RenderSurfaceBase *UnityRenderBuffer
typedef unsigned int UnityTextureID

Functions

void UnityPluginLoad(IUnityInterfaces *unityInterfaces)

Called when the plugin is loaded, this can be after/before Initialize()

Declared in IUnityInterface.h

Parameters
  • unityInterfaces: Unity class for accessing minimal Unity functionality exposed to C++ Plugins

void UnityPluginUnload()

Called when the plugin is unloaded, clean up here Declared in IUnityInterface.h

struct UnityInterfaceGUID
#include <IUnityInterface.h>

Public Members

unsigned long long m_GUIDHigh
unsigned long long m_GUIDLow
struct IUnityInterfaces
#include <IUnityInterface.h>

Public Members

IUnityInterface *(*GetInterface)(UnityInterfaceGUID guid)
void (*RegisterInterface)(UnityInterfaceGUID guid, IUnityInterface *ptr)
IUnityInterface *(*GetInterfaceSplit)(unsigned long long guidHigh, unsigned long long guidLow)
void (*RegisterInterfaceSplit)(unsigned long long guidHigh, unsigned long long guidLow, IUnityInterface *ptr)

4.3.2.2. IUnityProfilerCallbacks.h

This can be used to create custom profiling ‘timestamps’

Typedefs

typedef uint32_t UnityProfilerMarkerId
typedef uint16_t UnityProfilerCategoryId
typedef uint64_t UnityProfilerThreadId
typedef struct UnityProfilerCategoryDesc UnityProfilerCategoryDesc
typedef uint16_t UnityProfilerMarkerFlags
typedef uint16_t UnityProfilerMarkerEventType
typedef struct UnityProfilerMarkerDesc UnityProfilerMarkerDesc
typedef uint8_t UnityProfilerMarkerDataType
typedef struct UnityProfilerMarkerData UnityProfilerMarkerData
typedef uint8_t UnityProfilerFlowEventType
typedef struct UnityProfilerThreadDesc UnityProfilerThreadDesc
typedef void (*IUnityProfilerCreateCategoryCallback)(const UnityProfilerCategoryDesc *categoryDesc, void *userData)
typedef void (*IUnityProfilerCreateMarkerCallback)(const UnityProfilerMarkerDesc *markerDesc, void *userData)
typedef void (*IUnityProfilerMarkerEventCallback)(const UnityProfilerMarkerDesc *markerDesc, UnityProfilerMarkerEventType eventType, uint16_t eventDataCount, const UnityProfilerMarkerData *eventData, void *userData)
typedef void (*IUnityProfilerFrameCallback)(void *userData)
typedef void (*IUnityProfilerThreadCallback)(const UnityProfilerThreadDesc *threadDesc, void *userData)
typedef void (*IUnityProfilerFlowEventCallback)(UnityProfilerFlowEventType flowEventType, uint32_t flowId, void *userData)
typedef struct IUnityProfilerCallbacksV2 IUnityProfilerCallbacksV2
typedef struct IUnityProfilerCallbacks IUnityProfilerCallbacks

Enums

enum UnityProfilerMarkerFlag_

Values:

enumerator kUnityProfilerMarkerFlagDefault = 0
enumerator kUnityProfilerMarkerFlagScriptUser = 1 << 1
enumerator kUnityProfilerMarkerFlagScriptInvoke = 1 << 5
enumerator kUnityProfilerMarkerFlagScriptEnterLeave = 1 << 6
enumerator kUnityProfilerMarkerFlagAvailabilityEditor = 1 << 2
enumerator kUnityProfilerMarkerFlagAvailabilityNonDev = 1 << 3
enumerator kUnityProfilerMarkerFlagWarning = 1 << 4
enumerator kUnityProfilerMarkerFlagVerbosityDebug = 1 << 10
enumerator kUnityProfilerMarkerFlagVerbosityInternal = 1 << 11
enumerator kUnityProfilerMarkerFlagVerbosityAdvanced = 1 << 12
enum UnityProfilerMarkerEventType_

Values:

enumerator kUnityProfilerMarkerEventTypeBegin = 0
enumerator kUnityProfilerMarkerEventTypeEnd = 1
enumerator kUnityProfilerMarkerEventTypeSingle = 2
enum UnityProfilerMarkerDataType_

Values:

enumerator kUnityProfilerMarkerDataTypeNone = 0
enumerator kUnityProfilerMarkerDataTypeInstanceId = 1
enumerator kUnityProfilerMarkerDataTypeInt32 = 2
enumerator kUnityProfilerMarkerDataTypeUInt32 = 3
enumerator kUnityProfilerMarkerDataTypeInt64 = 4
enumerator kUnityProfilerMarkerDataTypeUInt64 = 5
enumerator kUnityProfilerMarkerDataTypeFloat = 6
enumerator kUnityProfilerMarkerDataTypeDouble = 7
enumerator kUnityProfilerMarkerDataTypeString = 8
enumerator kUnityProfilerMarkerDataTypeString16 = 9
enumerator kUnityProfilerMarkerDataTypeBlob8 = 11
enum UnityProfilerFlowEventType_

Values:

enumerator kUnityProfilerFlowEventTypeBegin = 0
enumerator kUnityProfilerFlowEventTypeNext = 1
enumerator kUnityProfilerFlowEventTypeEnd = 2

Variables

const UnityInterfaceGUID IUnityProfilerCallbacksV2_GUID = {0x5DEB59E88F2D4571ULL, 0x81E8583069A5E33CULL}
const UnityInterfaceGUID IUnityProfilerCallbacks_GUID = {0x572FDB38CE3C4B1FULL, 0xA6071A9A7C4F52D8ULL}
struct UnityProfilerCategoryDesc
#include <IUnityProfilerCallbacks.h>

Public Members

UnityProfilerCategoryId id
uint16_t reserved0
uint32_t rgbaColor
const char *name
struct UnityProfilerMarkerDesc
#include <IUnityProfilerCallbacks.h>

Public Members

const void *callback
UnityProfilerMarkerId id
UnityProfilerMarkerFlags flags
UnityProfilerCategoryId categoryId
const char *name
const void *metaDataDesc
struct UnityProfilerMarkerData
#include <IUnityProfilerCallbacks.h>

Public Members

UnityProfilerMarkerDataType type
uint8_t reserved0
uint16_t reserved1
uint32_t size
const void *ptr
struct UnityProfilerThreadDesc
#include <IUnityProfilerCallbacks.h>

Public Members

uint64_t threadId
const char *groupName
const char *name
struct IUnityProfilerCallbacksV2
#include <IUnityProfilerCallbacks.h>

Public Members

int (*RegisterCreateCategoryCallback)(IUnityProfilerCreateCategoryCallback callback, void *userData)
int (*UnregisterCreateCategoryCallback)(IUnityProfilerCreateCategoryCallback callback, void *userData)
int (*RegisterCreateMarkerCallback)(IUnityProfilerCreateMarkerCallback callback, void *userData)
int (*UnregisterCreateMarkerCallback)(IUnityProfilerCreateMarkerCallback callback, void *userData)
int (*RegisterMarkerEventCallback)(const UnityProfilerMarkerDesc *markerDesc, IUnityProfilerMarkerEventCallback callback, void *userData)
int (*UnregisterMarkerEventCallback)(const UnityProfilerMarkerDesc *markerDesc, IUnityProfilerMarkerEventCallback callback, void *userData)
int (*RegisterFrameCallback)(IUnityProfilerFrameCallback callback, void *userData)
int (*UnregisterFrameCallback)(IUnityProfilerFrameCallback callback, void *userData)
int (*RegisterCreateThreadCallback)(IUnityProfilerThreadCallback callback, void *userData)
int (*UnregisterCreateThreadCallback)(IUnityProfilerThreadCallback callback, void *userData)
int (*RegisterFlowEventCallback)(IUnityProfilerFlowEventCallback callback, void *userData)
int (*UnregisterFlowEventCallback)(IUnityProfilerFlowEventCallback callback, void *userData)
struct IUnityProfilerCallbacks
#include <IUnityProfilerCallbacks.h>

Public Members

int (*RegisterCreateCategoryCallback)(IUnityProfilerCreateCategoryCallback callback, void *userData)
int (*UnregisterCreateCategoryCallback)(IUnityProfilerCreateCategoryCallback callback, void *userData)
int (*RegisterCreateMarkerCallback)(IUnityProfilerCreateMarkerCallback callback, void *userData)
int (*UnregisterCreateMarkerCallback)(IUnityProfilerCreateMarkerCallback callback, void *userData)
int (*RegisterMarkerEventCallback)(const UnityProfilerMarkerDesc *markerDesc, IUnityProfilerMarkerEventCallback callback, void *userData)
int (*UnregisterMarkerEventCallback)(const UnityProfilerMarkerDesc *markerDesc, IUnityProfilerMarkerEventCallback callback, void *userData)
int (*RegisterFrameCallback)(IUnityProfilerFrameCallback callback, void *userData)
int (*UnregisterFrameCallback)(IUnityProfilerFrameCallback callback, void *userData)
int (*RegisterCreateThreadCallback)(IUnityProfilerThreadCallback callback, void *userData)
int (*UnregisterCreateThreadCallback)(IUnityProfilerThreadCallback callback, void *userData)

4.3.2.3. IUnityGraphics.h

Gives access to the graphics API, i.e. DirectX, OpenGL, Vulkan or Metal. These each have their own specialised implementation files. The file source/sample/CustomUploadMesh.cpp tries to use the native graphics API to access and update the GPU buffer directly. This is related to the RenderAPI

Typedefs

typedef enum UnityGfxRenderer UnityGfxRenderer
typedef enum UnityGfxDeviceEventType UnityGfxDeviceEventType
typedef void (*IUnityGraphicsDeviceEventCallback)(UnityGfxDeviceEventType eventType)
typedef struct IUnityGraphics IUnityGraphics
typedef void (*UnityRenderingEvent)(int eventId)
typedef void (*UnityRenderingEventAndData)(int eventId, void *data)

Enums

enum UnityGfxRenderer

Values:

enumerator kUnityGfxRendererD3D11 = 2
enumerator kUnityGfxRendererNull = 4
enumerator kUnityGfxRendererOpenGLES20 = 8
enumerator kUnityGfxRendererOpenGLES30 = 11
enumerator kUnityGfxRendererPS4 = 13
enumerator kUnityGfxRendererXboxOne = 14
enumerator kUnityGfxRendererMetal = 16
enumerator kUnityGfxRendererOpenGLCore = 17
enumerator kUnityGfxRendererD3D12 = 18
enumerator kUnityGfxRendererVulkan = 21
enumerator kUnityGfxRendererNvn = 22
enumerator kUnityGfxRendererXboxOneD3D12 = 23
enum UnityGfxDeviceEventType

Values:

enumerator kUnityGfxDeviceEventInitialize = 0
enumerator kUnityGfxDeviceEventShutdown = 1
enumerator kUnityGfxDeviceEventBeforeReset = 2
enumerator kUnityGfxDeviceEventAfterReset = 3

Variables

const UnityInterfaceGUID IUnityGraphics_GUID = {0x7CBA0A9CA4DDB544ULL, 0x8C5AD4926EB17B11ULL}
struct IUnityGraphics
#include <IUnityGraphics.h>

Public Members

UnityGfxRenderer (*GetRenderer)()
void (*RegisterDeviceEventCallback)(IUnityGraphicsDeviceEventCallback callback)
void (*UnregisterDeviceEventCallback)(IUnityGraphicsDeviceEventCallback callback)
int (*ReserveEventIDRange)(int count)

4.3.3. Unity C++ Render API

This is an example use case by Unity of the IUnityGraphics.h from the PluginAPI, sourced from Github Unity-Technologies/NativeRenderingPlugin.

4.3.3.1. RenderAPI.h

Functions

RenderAPI *CreateRenderAPI(UnityGfxRenderer apiType)
class RenderAPI
#include <RenderAPI.h>

Public Functions

~RenderAPI()
void ProcessDeviceEvent(UnityGfxDeviceEventType type, IUnityInterfaces *interfaces) = 0
bool GetUsesReverseZ() = 0
void DrawSimpleTriangles(const float worldMatrix[16], int triangleCount, const void *verticesFloat3Byte4) = 0
void *BeginModifyTexture(void *textureHandle, int textureWidth, int textureHeight, int *outRowPitch) = 0
void EndModifyTexture(void *textureHandle, int textureWidth, int textureHeight, int rowPitch, void *dataPtr) = 0
void *BeginModifyVertexBuffer(void *bufferHandle, size_t *outBufferSize) = 0
void EndModifyVertexBuffer(void *bufferHandle) = 0