The Memory Advice API beta is now deprecated, and no longer recommended for use.
适用于 Unity 游戏的 Memory Advice API 使用入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
本指南介绍了如何使用适用于 Unity 的 Memory Advice 插件将 Memory Advice API 集成到您的 Unity 游戏中。
要求
以下版本支持该插件:
安装 Android NDK r19 的 Unity 2019
安装 Android NDK r19 的 Unity 2020
安装 Android NDK r21 的 Unity 2021
安装 Android NDK r23 的 Unity 2022
如果您使用的是其他版本的 Unity 和 Android NDK,可能会遇到意外问题。如需查找您的 Unity 安装使用的 NDK 版本,请参阅适用于 Unity 的 Android 环境设置指南。
下载插件
下载插件。
导入插件
此插件是一个 Unity 软件包,您可以在项目中导入该软件包。如需导入该插件,请依次点击 Assets > Import Package > Custom Package,然后选择您下载的 .unitypackage
文件。您也可以在打开 Unity 项目后双击 .unitypackage
文件。
使用库
本部分介绍如何使用该库。
初始化库
您需要在应用启动时初始化一次库。
为此,请将以下代码添加到项目中:
void Start()
{
MemoryAdviceErrorCode errorCode = MemoryAdvice.Init();
if(errorCode == MemoryAdviceErrorCode.Ok)
{
Debug.Log("Memory advice init successfully");
}
}
内存状态轮询
您可以通过按选择的时间间隔轮询库,来检索应用的内存状态。如果您需要轮询库,请使用 MemoryAdvice_getMemoryState 函数:
MemoryState memoryState = MemoryAdvice.GetMemoryState();
switch (memoryState)
{
case MemoryState.Ok:
//The application can safely allocate memory.
break;
case MemoryState.ApproachingLimit:
// The application should minimize memory allocation.
break;
case MemoryState.Critical:
// The application should free memory as soon as possible
// until the memory state changes.
break;
}
设置观察器
您也可以设置观察器并注册 Memory Advice API,并在状态接近上限或紧急内存状态(但不适用于 ok 状态)时调用 watcher 函数。例如,以下代码会创建一个观察器并每 2 秒请求一次 Memory Advice API 通知:
MemoryAdviceErrorCode errorCode = MemoryAdvice.RegisterWatcher(2000,
new MemoryWatcherDelegateListener((MemoryState state) =>
{
switch (memoryState)
{
case MemoryState.ApproachingLimit:
// The application should minimize memory allocation.
break;
case MemoryState.Critical:
// The application should free memory as soon as possible
// until the memory state changes.
break;
}
})
);
if(errorCode == MemoryAdviceErrorCode.Ok)
{
Debug.Log("Memory Advice watcher registered successfully");
}
后续操作
您可以下载我们的 Unity 示例项目,该项目提供了用于分配和释放内存的简单界面,并使用 Memory Advice API 监控内存状态。
如需查看其他资源和报告问题,请参阅概览。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-26。
[null,null,["最后更新时间 (UTC):2025-08-26。"],[],[],null,["# Get started with the Memory Advice API for Unity games\n\nThis guide describes how to use the Memory Advice plugin for Unity to\nintegrate the\n[Memory Advice API](/games/sdk/memory-advice/overview) into\nyour Unity game.\n\nRequirements\n------------\n\nThe plugin is supported on:\n\n- Unity 2019 with Android NDK r19\n\n- Unity 2020 with Android NDK r19\n\n- Unity 2021 with Android NDK r21\n\n- Unity 2022 with Android NDK r23\n\nYou may face unexpected issues if you are using other versions of Unity and\nthe Android NDK. To find the NDK version used by your Unity installation, see\nthe\n[Android environment setup guide](https://docs.unity3d.com/Manual/android-sdksetup.html)\nfor Unity.\n\nDownload the plugin\n-------------------\n\nDownload [the plugin](https://dl.google.com/developers/android/games/memory_advice/memory-advice-1.0.0-beta01.unitypackage).\n\nImport the plugin\n-----------------\n\nThe plugin is a Unity Package that you can import into your project. To import\nthe plugin, click **Assets \\\u003e Import Package \\\u003e Custom Package** and select the\n`.unitypackage` file you downloaded. You can also double-click the\n`.unitypackage` file after opening your Unity project.\n\nUse the library\n---------------\n\nThis section describes how to use the library.\n\n### Initialize the library\n\nYou need to initialize the library once when the app starts.\nTo do so, add this code to your project: \n\n void Start()\n {\n MemoryAdviceErrorCode errorCode = MemoryAdvice.Init();\n if(errorCode == MemoryAdviceErrorCode.Ok)\n {\n Debug.Log(\"Memory advice init successfully\");\n }\n }\n\n### Poll for memory state\n\nYou can retrieve the memory state of your app by polling the library at the\ninterval of your choosing. Use the [MemoryAdvice_getMemoryState](/reference/games/memory-advice/group/memory-advice#memoryadvice_getmemorystate)\nfunction whenever you need to poll the library: \n\n MemoryState memoryState = MemoryAdvice.GetMemoryState();\n switch (memoryState)\n {\n case MemoryState.Ok:\n //The application can safely allocate memory.\n break;\n case MemoryState.ApproachingLimit:\n // The application should minimize memory allocation.\n break;\n case MemoryState.Critical:\n // The application should free memory as soon as possible\n // until the memory state changes.\n break;\n }\n\n### Set up a watcher\n\nYou can also set up [a watcher](/reference/games/memory-advice/group/memory-advice#memoryadvice_registerwatcher)\nand register the Memory Advice API, and your watcher function\nwill get called when the state is either approaching the limit or the critical\n[memory state](/reference/games/memory-advice/group/memory-advice#memoryadvice_memorystate)\n(but not for the ok state). For example, the following code creates a watcher\nand requests a Memory Advice API notification every 2 seconds: \n\n MemoryAdviceErrorCode errorCode = MemoryAdvice.RegisterWatcher(2000,\n new MemoryWatcherDelegateListener((MemoryState state) =\u003e\n {\n switch (memoryState)\n {\n case MemoryState.ApproachingLimit:\n // The application should minimize memory allocation.\n break;\n case MemoryState.Critical:\n // The application should free memory as soon as possible\n // until the memory state changes.\n break;\n }\n })\n );\n\n if(errorCode == MemoryAdviceErrorCode.Ok)\n {\n Debug.Log(\"Memory Advice watcher registered successfully\");\n }\n\nWhat's next\n-----------\n\nYou can download our [Unity sample project](https://dl.google.com/developers/android/games/memory_advice/sample.zip)\nthat provides a simple UI for allocating and freeing memory, and uses\nMemory Advice API to monitor the memory state.\n\nSee the [overview](/games/sdk/memory-advice/overview) for\n[additional resources](/games/sdk/memory-advice/overview#additional_resources)\nand [reporting issues](/games/sdk/memory-advice/overview#issues_and_feedback)."]]