Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Subclassing AppController.mm and call to applicationDidReceiveMemoryWarning not working

Discussion in 'Scripting' started by SidarVasco, Jun 3, 2016.

  1. SidarVasco

    SidarVasco

    Joined:
    Feb 9, 2015
    Posts:
    163
    I have the following mm file in Assets/Plugins/iOS

    Code (csharp):
    1.  
    2. #import "UnityAppController.h"
    3.  
    4.  
    5. @interface MemoryAppController : UnityAppController
    6.  
    7. - (void)applicationDidReceiveMemoryWarning:(UIApplication*)application;
    8.  
    9. @end
    10.  
    11.  
    12. @implementation MemoryAppController
    13.  
    14. - (void)applicationDidReceiveMemoryWarning:(UIApplication*)application {
    15.  
    16.     printf_console("Chicken nuggets ON A STICK \n");
    17.  
    18.     UnitySendMessage("MemoryHandlerIOS", "OnReceivedMemoryWarning","");
    19.  
    20. }
    21.  
    22. @end
    23.  
    24.  
    25. IMPL_APP_CONTROLLER_SUBCLASS(MemoryAppController)
    26.  

    I have the following Script:

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5. using VascoGames;
    6.  
    7. public class ReceiveMemoryWarningHandler : Singleton<ReceiveMemoryWarningHandler> {
    8.  
    9.  #if UNITY_IOS
    10.  
    11.  public void OnReceivedMemoryWarning(string msg){
    12.  
    13.  Debug.Log("Memory warning received from iOS");
    14.  Resources.UnloadUnusedAssets();
    15.  
    16.  }
    17.  
    18.  #endif
    19. }
    20.  
    21.  

    In my scene I have a GameObject named "MemoryHandlerIOS". According to the manual the first parameter in UnitySendMessage should be name of the game object.

    printf_console doesn't output the message in the mm file and the Debug.Log from the class isn't called either. However I'm not entirely sure if that's due to the fact I have Debug Scripting off ( can't compile if I turn that on ).

    The mm file is build into the Xcode project.

    Anything I'm doing wrong?
     
  2. SidarVasco

    SidarVasco

    Joined:
    Feb 9, 2015
    Posts:
    163
    Bump...
     
  3. SidarVasco

    SidarVasco

    Joined:
    Feb 9, 2015
    Posts:
    163
    Bump...
     
  4. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    Remove line 7 from your .mm and make sure your GameObject and unity C# class names are both MemoryHandlerIOS. If that doesn't work, try changing your derived class to MonoBehaviour instead of the Singleton.
     
  5. SidarVasco

    SidarVasco

    Joined:
    Feb 9, 2015
    Posts:
    163
    Jim,

    Originally line 7 wasn't there either, but that didn't work.

    Singleton is derived from monoheviour. If I change the unity AppController.mm in xcode directly all calls are made fine.
    It just seems like my custom mm controller doesn't have any effect.

    Any other ideas?
     
  6. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    What is the full name of your mm file in Assets/Plugins/iOS?
     
  7. SidarVasco

    SidarVasco

    Joined:
    Feb 9, 2015
    Posts:
    163
    MemoryAppController.mm
     
  8. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    I don't see any other issues... You tried changing your C# class name to match the GameObject name right?

    Try adding this above the printf_console line.
    Code (csharp):
    1. [super applicationDidReceiveMemoryWarning:application];
     
  9. SidarVasco

    SidarVasco

    Joined:
    Feb 9, 2015
    Posts:
    163
    yes


    Ill try your suggestion and let you know.
     
  10. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    Did you ever get this working? I'm having the same issue, where it seems the methods in the subclass of the app controller are never called.
     
    Deleted User likes this.
  11. SidarVasco

    SidarVasco

    Joined:
    Feb 9, 2015
    Posts:
    163
    Hey @Benzino07 I pretty much dropped it. But I do remember a few of those debug lines popping up but not sure how or what I did. I moved on from that project. Sorry.
     
  12. SimteractiveDev

    SimteractiveDev

    Joined:
    Jul 9, 2014
    Posts:
    97
    No worries, thanks anyway :)
     
  13. yotam1

    yotam1

    Joined:
    Nov 21, 2016
    Posts:
    1