---
title: IUnityMemoryManager API 参考
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/zh-cn/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/low-level-native-plugin-memory-manager-api-reference
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/low-level-native-plugin-memory-manager-api-reference.md
generated_at: 2026-11-16T16:31:53.017Z
locale: zh-cn
---

# IUnityMemoryManager API 参考

> 在原生插件中使用内存管理员 API。

本页面提供`IUnityMemoryManager`接口的 API 参考。

## CreateAllocator##createallocator

### 声明##declaration

`UnityAllocator* (UNITY_INTERFACE_API * CreateAllocator)(const char* areaName, const char* objectName);`

### 参数##parameters

| **参数**                      | **描述**              |
| --------------------------- | ------------------- |
| **const char* areaName*\*   | 此分配器的Category的Name。 |
| **const char* objectName*\* | 此指定的分配器的Name。       |

### 描述##description

创建一个可以分配内存的新建配器Object。

## DestroyAllocator##destroyallocator

### 声明##declaration

`void(UNITY_INTERFACE_API * DestroyAllocator)(UnityAllocator * allocator);`

### 参数##parameters

| **参数**                    | **描述**   |
| ------------------------- | -------- |
| **UnityAllocator \* 分配器** | 要删除的分配器。 |

### 描述##description

删除现有的分配器Object。

## 分配##allocate

### 声明##declaration

`void* (UNITY_INTERFACE_API * Allocate)(UnityAllocator * allocator, size_t size, size_t align, const char* file, int32_t line);`

### 参数##parameters

| **参数**                    | **描述**                                 |
| ------------------------- | -------------------------------------- |
| **UnityAllocator \* 分配器** | 使用于分配的分配器。                             |
| **size\_t size**          | 要分配的内存（以字节为单位）。                        |
| **size\_t align**         | 生成的指针的内存地址的 Alignment。                 |
| **const char* 文件*\*       | 用于进行此分配的调用的音频源文件的路径。在此处使用预定义的宏\_文件\_。  |
| **int32\_t 线**            | 音频源文件中用于make此分配的调用的数量。在此处使用预定义的宏\_线\_。 |

### 描述##description

使用现有分配器分内存此方法返回指向新分配的内存的指针。

## 取消分配##deallocate

### 声明##declaration

`void(UNITY_INTERFACE_API * Deallocate)(UnityAllocator * allocator, void* ptr, const char* file, int32_t line);`

### 参数##parameters

| **参数**                    | **描述**                                         |
| ------------------------- | ---------------------------------------------- |
| **UnityAllocator \* 分配器** | 使用于取消分配的分配器。                                   |
| **void* ptr\**            | 指向要取消分配的内存的指针。                                 |
| **const char* 文件*\*       | make此取消分配的调用来自的音频源文件的路径。在此处使用预定义的宏\_文件\_。      |
| **int32\_t 线**            | 音频源文件中的数量，用于make此取消分配的调用来自该文件。在此处使用预定义的宏\_线\_。 |

### 描述##description

取消分配指定指针指向的内存。此操作不会将指针设置为 null。

## 重新分配##reallocate

### 声明##declaration

`void* (UNITY_INTERFACE_API * Reallocate)(UnityAllocator * allocator, void* ptr, size_t size, size_t align, const char* file, int32_t line);`

### 参数##parameters

| **参数**                    | **描述**                                    |
| ------------------------- | ----------------------------------------- |
| **UnityAllocator \* 分配器** | 使用于重新分配操作的分配器。                            |
| **void* ptr\**            | 指向要取消分配的内存的指针。                            |
| **size\_t size**          | 要分配的内存（以字节为单位）。                           |
| **size\_t align**         | 生成的指针的内存地址的 Alignment。                    |
| **const char* 文件*\*       | make此重新分配的调用来自的音频源文件的路径。在此处使用预定义的宏\_文件\_。 |
| **int32\_t 线**            | 音频源文件中用于make此重新分配的调用的数量。在此处使用预定义的宏\_线\_。  |

### 描述##description

重新分配现有指针以指向不同的内存代码块。

## 实现示例##implementation-example

以下是`IUnityMemoryManager`接口的实现示例。

```lang-cs
#include "IUnityInterface.h"
#include "IUnityMemoryManager.h"
#include <cstdint>

static IUnityMemoryManager* s_MemoryManager = NULL;
static UnityAllocator* s_Alloc = NULL;

extern "C" void Unity_INTERFACE_导出 Unity_INTERFACE_API UnityPluginLoad(IUnityInterfaces * unityInterfaces)
{
    s_MemoryManager = unityInterfaces->Get<IUnityMemoryManager>();
    if (s_MemoryManager  == NULL)
	返回;

    // Create an allocator.这允许您在拍摄快照When在性性能分析器中查看分分配。在 Plug-ins-原生 - 插件后端分配器下
   // All memory allocated here also goes under kMemNativePlugin
    s_Alloc = s_MemoryManager->CreateAllocator("plug-ins-native", "Plugin Backend Allocator");
}

extern "C" void Unity_INTERFACE_导出 Unity_INTERFACE_API UnityPluginUnload()
{
    //Free allocator
    s_MemoryManager->DestroyAllocator(s_Alloc);
    s_Alloc = NULL;
    s_MemoryManager = NULL;
}

void DoMemoryOperations()
{  
    // Allocate 1KB memory
    void* mem = s_MemoryManager->Allocate(s_Alloc, 1 * 1024, 16, __FILE__, __LINE__);
     // Reallocate the same pointer with 2KB
    mem = s_MemManager->Reallocate(s_Alloc, mem, 2 * 1024, 16, __FILE__, __LINE__);
    // Delete allocated memory
    s_MemoryManager-> Deallocate(s_Alloc, mem, __文件__, __线__);
}
```

## 其他资源##additional-resources

* [低关卡原生插件渲染扩展](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/low-level-native-plugin-rendering-extensions.md)
* [低关卡原生插件着色器编译器访问](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/low-level-native-plugin-shader-compiler-access.md)
* [低关卡原生插件内存管理员 API](/engine/6000.0/manual/scripting/compilation-and-code-reload/plug-ins/native-plugin-interface/low-level-native-plugin-memory-manager-api.md)
