x


Is there a way to tell in Unity iOS when onDidReceiveMemoryWarning is received?

I'd like to remove textures from memory, and fall back on different shaders if I receive the onDidReceiveMemoryWarning from the iOS, but right now I do not see any documentation on how to get that message? I was wondering if anyone has done this or if there is a tool//plugin that could assist with this if Unity does not have native support for this?

There are vague mentions on the forums to check UnitySendMessage in the AppController, but I have no real idea on how to do this.

more ▼

asked Jul 27 '12 at 10:23 PM

Ben BearFish gravatar image

Ben BearFish
355 4 6 7

Amazing QA - if only there was the same for Android!

Mar 25 at 04:14 PM Fattie
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I found the answer for anyone interested.

In the AppController.mm

Look for the function:

- (void) applicationDidReceiveMemoryWarning:(UIApplication*)application
{
 printf_console("WARNING -> applicationDidReceiveMemoryWarning()\n");
}

This is the function that is called when your app receives a low memory warning.

Next you must add a UnitySendMessage to your Unity code, so that you can receive the low memory warning message in your Unity code.

So, you must modify the applicationDidReceiveMemoryWarning function by writing this code:

- (void) applicationDidReceiveMemoryWarning:(UIApplication*)application
{
 printf_console("WARNING -> applicationDidReceiveMemoryWarning()\n");

    // This is your added call to send a message to your Unity code.
    // The first parameter is the name of the class you'll be using to receive the
    // low memory warning message. Mine is MemoryManager, yours can be whatever
    // you name your class.
    // The second parameter is the name of the method/function within your class
    // that you will be using to receive the low memory warning and handle it 
    // appropriately however you see fit. Mine is ReceivedMemoryWarning, yours can
    // be whatever you like.
    // Don't forget that your function/method within your Unity C#/Javascript/etc
    // code, must be a void return value with a string parameter.
    // E.g. 
    // public void ReceivedMemoryWarning(string message)
    // The third parameter can be a null string, or whatever string you want your 
    // message that is sent to say.

    UnitySendMessage("MemoryManager", "ReceivedMemoryWarning","");
}

After you add this message call to the AppController.mm file, in your Unity code, add a new file for the class that will be receiving the message. Mine happens to be in C#, but it can be any file type. Mine is MemoyManager.cs.

Then add the code:

using UnityEngine;
using System.Collections;

public class MemoryManager : MonoBehaviour
{
 //Function is called when a lowMemoryWarning is received
 public void ReceivedMemoryWarning(string message)
 {
 Debug.Log("Memory Manager RECEIVED LOW MEMORY WARNING!");
 }
}

This class now receives the message sent from the AppController.mm, and within your own function, mine is ReceivedMemoryWarning, handle how you deal with, free up memory within your game/project.

Also, it's worth mentioning if you want to modify the AppController.mm and have it update automatically in xCode every time, first create a folder in your Assets folder within the Project hierarchy window named "iOS" under the Plugins folder, so the hierarchy directory is Assets/Plugins/iOS. Then drag the AppController.mm file into that folder with your modified code, and Unity will automatically update your changes when it builds to xCode.

I hope that helps for everyone who is looking to do this, whether you're a programmer or not.

more ▼

answered Aug 09 '12 at 05:47 AM

Ben BearFish gravatar image

Ben BearFish
355 4 6 7

Great question and superb self-answer. This should be converted to a documentation page.

May 01 '13 at 05:54 PM DannyB

I found out more about iOS SDK to Unity communication through your answer than through the last 3-4 weeks of reading Unity's documentation and searching elsewhere online. Bravo and thank you.

May 03 '13 at 11:45 PM hyliandanny

Awesome answer!

There's just one issue here: Put the files in Assets/Plugins/iOS not just iOS.

http://docs.unity3d.com/Documentation/Manual/PluginsForIOS.html

Jul 17 '13 at 02:34 PM ZenithCode

Great answer, thanks Ben BearFish!

Dec 13 '13 at 11:04 AM azpt

@ZenithCode, I updated the answer with your note. Thanks.

Dec 13 '13 at 07:32 PM Ben BearFish
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2554
x437
x8
x4

asked: Jul 27 '12 at 10:23 PM

Seen: 3383 times

Last Updated: Mar 25 at 04:14 PM