1. 程式人生 > >Unity 接收 IOS 記憶體不足的事件回撥。

Unity 接收 IOS 記憶體不足的事件回撥。

1. unity 2017 新版本 https://docs.unity3d.com/ScriptReference/Application-lowMemory.html 直接註冊一下事件

2. 5.5 左右的 需要自己和IOS關聯起來。

方法一.用 XUPorter 工具 修改程式碼

讓 UnityAppController 發個訊息過來。

方法二、https://answers.unity.com/questions/291788/is-there-a-way-to-tell-in-unity-ios-when-ondidrece.html

Unity 專案的 Plugins\iOS\隨便建個.mm  
加一下程式碼


 #import "UnityAppController.h"
 
 @interface iOSMemoryManager : UnityAppController {}
 @end
 
 @implementation iOSMemoryManager
 - (void)applicationDidReceiveMemoryWarning:(UIApplication*)application {
     printf_console("WARNING MY OWN APP CONTROLLER -> applicationDidReceiveMemoryWarning()\n");
     UnitySendMessage("GameMain", "ReceiveMemoryWarning", "");
 }
 @end
 
 IMPL_APP_CONTROLLER_SUBCLASS(iOSMemoryManager)



Unity裡
掛在指定名字的指令碼上 實現一下函式


    float lastunload = 0;
    //接收ios拋過來的記憶體不夠警告, 打錯誤日誌,上報到bugly
    public void ReceiveMemoryWarning(string msg)
    {
        Log.Error("LowMemory! GameMain ReceiveMemoryWarning");


        if (lastunload + 10 < Time.time)
        {
            Resources.UnloadUnusedAssets();
            lastunload = Time.time;
        }
    }