The Memory Advice API beta is now deprecated, and no longer recommended for use.
بدء استخدام واجهة برمجة تطبيقات Memory Advice API لألعاب Unity
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يوضّح هذا الدليل كيفية استخدام إضافة Memory Advice في Unity لدمج Memory Advice API في لعبة Unity.
المتطلبات
يتوافق المكوّن الإضافي مع:
Unity 2019 مع الإصدار 19 من Android NDK
Unity 2020 مع الإصدار 19 من حزمة تطوير البرامج (NDK) لنظام التشغيل Android
Unity 2021 مع الإصدار 21 من حزمة تطوير البرامج الأصلية (NDK) لنظام التشغيل Android
Unity 2022 مع Android NDK r23
قد تواجه مشاكل غير متوقّعة إذا كنت تستخدم إصدارات أخرى من Unity وAndroid NDK. لمعرفة إصدار NDK المستخدَم في عملية تثبيت Unity، راجِع دليل إعداد بيئة Android في Unity.
تنزيل المكوّن الإضافي
نزِّل المكوّن الإضافي.
استيراد المكوّن الإضافي
المكوّن الإضافي هو حزمة Unity يمكنك استيرادها إلى مشروعك. لاستيراد المكوّن الإضافي، انقر على مواد العرض (Assets) > استيراد حزمة (Import Package) > حزمة مخصّصة (Custom Package)، ثم اختَر ملف .unitypackage
الذي نزّلته. يمكنك أيضًا النقر مرّتين على ملف
.unitypackage
بعد فتح مشروع Unity.
استخدام المكتبة
يوضّح هذا القسم كيفية استخدام المكتبة.
تهيئة المكتبة
يجب إعداد المكتبة مرة واحدة عند بدء تشغيل التطبيق.
لإجراء ذلك، أضِف الرمز التالي إلى مشروعك:
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، وسيتم استدعاء وظيفة المراقب عند اقتراب الحالة من الحد أو عند الوصول إلى حالة الذاكرة الحرجة (ولكن ليس في حالة الذاكرة العادية). على سبيل المثال، ينشئ الرمز التالي أداة مراقبة
ويطلب إشعارًا من واجهة برمجة التطبيقات 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)."]]