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");
}
}
記憶體狀態輪詢
以您選擇的時間間隔輪詢程式庫,即可擷取應用程式的記憶體狀態。每當您需要輪詢程式庫時,請使用 MemoryAdtip_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 Adtip API,並在狀態接近限制或重大記憶體狀態 (但不適用於正常狀態) 時呼叫看守工具函式。舉例來說,以下程式碼會建立看守工具並每 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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-26 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-26 (世界標準時間)。"],[],[],null,["| **Note:** The Memory Advice API Beta is now over and the library is deprecated. [learn more about alternative memory management approaches](/games/optimize/memory-allocation).\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\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\nDownload [the plugin](https://dl.google.com/developers/android/games/memory_advice/memory-advice-1.0.0-beta01.unitypackage).\n\nImport the plugin\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\nThis section describes how to use the library.\n\nInitialize 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\nPoll 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\nSet 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\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)."]]