Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity AppController subclassing.

Discussion in 'iOS and tvOS' started by techmage, Jul 23, 2013.

  1. VuforiaMatrix

    VuforiaMatrix

    Joined:
    Dec 23, 2014
    Posts:
    1
    Hi all,

    I am trying to integrate a Unity/Vuforia project into a iOS existing app.
    I followed this excellent tutorial:
    http://www.the-nerd.be/2014/09/08/sandbox-unity-app-in-existing-ios-app/
    And everything works good except I have this error:
    "Could not find a UIView with CAEAGLLayer layer class that responds to selector renderFrameQCAR"
    Can someone help me?

    Thanks.
     
  2. hebaios

    hebaios

    Joined:
    Jan 5, 2015
    Posts:
    2
    Hi all

    I face same error to include Unity/Vuforia project in Xcode native.
    "Could not find a UIView with CAEAGLLayer layer class that responds to selector renderFrameQCAR"
    I work on Unity 4.6, Xcode 6, Vuforia 3.0.9
    Do you VuforiaMatrix found solution ? or any one else ?

    Thank you,
     
    arizigler likes this.
  3. owi

    owi

    Joined:
    Aug 7, 2014
    Posts:
    1
    Hey there,

    I'm currently working with a custom AppController to access custom ViewControllers to wrap a Unity app within a native iOS app.

    For Unity 5.0.0b18 I had to adapt the custom AppController.mm:
    It seems one has to adapt the custom Unity AppController for builds from Unity 5. Due to the fact that the method "createViewHierarchyImpl" is deprecated, one has to use "willStartWithViewController" in "YourCustomAppController.mm".
    Based on the tutorial by TheNerd from his website I used the following code within "YourCustomAppController.mm":

    Code (Objective-C++):
    1. #import <UIKit/UIKit.h>
    2. #import "UnityAppController.h"
    3. #import "UI/UnityView.h"
    4. #import "UI/UnityViewControllerBase.h"
    5.  
    6. #import "StartViewController.h"
    7.  
    8. @interface JNAppController : UnityAppController
    9.  
    10. @property (nonatomic, strong) UINavigationController *navController;
    11.  
    12. - (void)willStartWithViewController;
    13.  
    14. @end
    15.  
    16. @implementation JNAppController
    17. - (void)willStartWithViewController:(UIViewController*)controller {
    18.  
    19.     _rootController    = [[UnityDefaultViewController alloc] init];
    20.     _rootView        = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    21.     _rootController.view = _rootView;
    22.  
    23.     StartViewController *startVC = [[StartViewController alloc] initWithNibName:nil bundle:nil];
    24.  
    25.     self.navController = [[UINavigationController alloc] initWithRootViewController:startVC];
    26.     [_rootView addSubview:self.navController.view];
    27. }
    28.  
    29. @end
    30.  
    31. IMPL_APP_CONTROLLER_SUBCLASS(JNAppController)
    EDIT:
    In the "UnityViewControllerBase.h" I had to adapt the last lines from
    Code (Objective-C++):
    1. // this is helper to add proper rotation handling methods depending on ios version
    2. extern "C" void AddViewControllerRotationHandling(Class class_, IMP willRotateToInterfaceOrientation, IMP didRotateFromInterfaceOrientation, IMP viewWillTransitionToSize);
    3. extern "C" void AddViewControllerDefaultRotationHandling(Class class_);
    to
    Code (Objective-C++):
    1. // this is helper to add proper rotation handling methods depending on ios version
    2. #ifdef __cplusplus
    3. extern "C" {
    4. #endif
    5.     void AddViewControllerRotationHandling(Class class_, IMP willRotateToInterfaceOrientation, IMP didRotateFromInterfaceOrientation, IMP viewWillTransitionToSize);
    6. #ifdef __cplusplus
    7. }
    8. #endif
    9.  
    10. #ifdef __cplusplus
    11. extern "C" {
    12. #endif
    13.     void AddViewControllerDefaultRotationHandling(Class class_);
    14. #ifdef __cplusplus
    15. }
    16. #endif

    That's it.

    Best,

    owi
     
    Last edited: Jan 27, 2015
    Dover8 likes this.
  4. lmbarns

    lmbarns

    Joined:
    Jul 14, 2011
    Posts:
    1,628
    Has anyone had any luck subclassing like this with il2cpp 64 bit in 4.6.3??

    I have a project that subclassed a unity project built with 4.5.3, now I'm trying to update the Unity project with 4.6.3 and swap it in but a lot has changed and I'm struggling to figure it out.

    Also, depending on the Unity version I have installed, I get different sets of errors. Unity 4.6.2 gives completely different errors from 4.6.3, but if I take only the unity project into xcode and compile, it runs on device correctly, when I bring into my other project it explodes.

    Is subclassing with il2cpp a lot different from the instructions for doing it with pre 64bit builds? I see in Unity5 it's different yet again, are those the same changes that are affecting the 4.6.3 version? Should I be looking at the Unity 5 subclassing instructions instead or is 4.6.3 different and still using original 4.x stuff?
     
  5. alexatino

    alexatino

    Joined:
    Dec 11, 2014
    Posts:
    6
  6. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,623
    >>http://www.makethegame.net/unity/add-unity3d-to-native-ios-app-with-unity-5-and-vuforia-4-x/
    you have
    which is opposite of true - on 5.0 all of unity is ARC (both trampoline and lib)

    well if you would check trampoline we have this code:
    so if you set to -1 simply - no need to add it at all
     
    alexatino likes this.
  7. thenerd

    thenerd

    Joined:
    Aug 6, 2014
    Posts:
    6
    I managed to use Unity 4.6.3, Vuforia 4 and Xcode 6.1 to play nicely together, but I have an annoying issue.
    This way I can use the UnityView within a regular native iOS application.

    I'm using a 3th party library in my iOS project to unzip files (https://github.com/soffes/ssziparchive). This library uses
    When I add this project inside my Xcode project, I get an error in one of the files generated by Unity (Libraries/libil2cpp/include/support/zlib/zConf.h)

    The error happens on line 338 at the following code (in bold)

    Code (CSharp):
    1. #ifdef SMALL_MEDIUM
    2.    /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
    3. #  define Bytef Byte FAR
    4. #else
    5.    [B]typedef Byte  FAR Bytef;[/B]
    6. #endif
    The compiler says 'Unknown type name 'il2cpp_z_Byte'

    I guess this has something to do with the fact SSZipArchive also uses the zlib library? Because if I remove this library the error goes away.
    For now, i just changed the code Byte to int to silence the compiler and it works. But I don't like it :)

    Any idea @Alexy what we could do?
     
  8. alexatino

    alexatino

    Joined:
    Dec 11, 2014
    Posts:
    6
  9. tomhenrik

    tomhenrik

    Joined:
    Apr 16, 2015
    Posts:
    1
    I'm working on a project using Unity 4.6.3, Vuforia 4 and Xcode 6.1 and managed to add this to a native iOS app. I then added a Storyboard and a new view which shows up after splash screen. To get to the unity view part you have to click a button. This all works fine. Problem is that I want to lock the orientation of the iOS View and leave the UnityView with autorotate. From my understanding is that in ios8 the rootview controller rules the orientation part. From various different forums they all came to the conclusion that in the view controller you have to implement the shouldAutorotate and supportedInterfaceOrientations to control the orientation for that view.

    In my VuforiaNativeRendererController I have the createViewHierarchyImpl method where I create a root controller and rootview basically as has been described in several posts in this thread. But for some reason no matter how much I rotated the phone the orientation methods in my viewcontroller was never called. I discovered that is b/c the rootviewcontroller was always UnityDefaultViewController. After much digging I found another method in UnityAppController called createRootViewController which I then implemented and returned my own viewcontroller. Now my orientation callback methods got called when rotating the phone. Problem is now this also locks my UnityView scene which I want to be able to rotate.
    So question is, can I somehow change the rootviewcontroller ? Or can this be solved in another way that I don’t see ?
    ng Unity 4.6.3, Vuforia 4 and Xcode 6.1 and managed
     
  10. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,623
    you should check how UnityDefaultViewController is implemented and or inherit from it (calling super when needed) or copy appropriate code to your controller
     
  11. mellinsa

    mellinsa

    Joined:
    Apr 16, 2015
    Posts:
    7
    Well, it almost takes half of the memory on a physical iPhone, surprised! Do you check check the memory status via instrument?
     
    Last edited: Apr 22, 2015
  12. izzusan

    izzusan

    Joined:
    Nov 15, 2012
    Posts:
    3
    hi,
    owi, i need your help :)

    I need to integrate the unity 5 project with a custom app controller.

    i follow the tutorial by TheNerd from his website and with unity 4.6..2 work fine!

    but if i tried with unity 5 and with your fix doesn't work.... the custom app controller don't start and after 5 sec the app crash...

    now the ios plugin in not in the root but is in plugins/ios/TNAppController.mm and don't start HelloViewController.m

    Have you a idea? your project work fine?

    thanks a lot
     
  13. Dover8

    Dover8

    Joined:
    Aug 20, 2010
    Posts:
    94
    I have a similar situation. Pre Vuforia 4 I was using this code in my createViewHierarchy:

    Code (CSharp):
    1. -(void)createViewHierarchyImpl
    2. {
    3.     UIStoryboard *sb = [UIStoryboard storyboardWithName:@"AR" bundle:nil];
    4.     PPARStartViewController * helloVC = (PPARStartViewController *)[sb instantiateViewControllerWithIdentifier:@"StartController"];
    5.    
    6.     self.navController = [[UINavigationController alloc] initWithRootViewController:helloVC];
    7.     self.navController.navigationBarHidden = YES;
    8.    
    9.     _rootController = self.navController;
    10.     _rootView = self.navController.view;
    11.    
    12. }
    But now if I try that I get an exception:
    Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<PPARStartViewController: 0x17dc3070> should have parent view controller:<UnityDefaultViewController: 0x1c083200> but actual parent is:<UINavigationController: 0x17dc3820>’

    Thing is that I WANT it's parent to be the UINavigationController. If I go back to the code from the Nerd tutorial mentioned several times here already then it works, but like TomHenrik says I no longer have control of rotations in my viewController hierarchy. Previously I had extended UINavigationController to pass down rotations as it was always the rootController, but now it's the UnityDefaultViewController that intercepts rotation calls and I can't pass them on.

    Tom, what did you put into the createRootViewController method?
     
  14. VProductions

    VProductions

    Joined:
    Apr 22, 2015
    Posts:
    4
    Hi,

    Under unity4.5.3f + vuforia 3.0.9 it worked out.
    But under unity 4.6.6p1 + vuforia 4.2.3 it's not working.
    (To make my app 64bit compatible, I need to stick to this combination of the newest versions.)
    Unityview has been loaded, but Camera isn't working, only displaying a black screen.

    I used the following method
    Code (CSharp):
    1. - (void)createViewHierarchyImpl
    2. {
    3.     _rootController    = [[UnityDefaultViewController alloc] init];
    4.     _rootView        = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    5.     _rootController.view = _rootView;
    6.    
    7.     UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboardIPad" bundle: nil];
    8.  
    9.      self.navController = [mainStoryboard instantiateInitialViewController];
    10.      [_rootView addSubview:self.navController.view];
    11. }
    12.  
    and I got this error :
    VideoBackgroundConfig with screen size of zero received, skipping config step.

    Anyone can help me?
     
    Last edited: Jun 17, 2015
  15. Dover8

    Dover8

    Joined:
    Aug 20, 2010
    Posts:
    94
    You are not initialising the self.navController Change to:
    Code (CSharp):
    1. self.navController = [[UINavigationController alloc] initWithRootViewController:[mainStoryboard instantiateInitialViewController]];
     
  16. VProductions

    VProductions

    Joined:
    Apr 22, 2015
    Posts:
    4
    Hi, thanks Dover8, I know what you are trying to say, but my Storyboard already has navigation controller as the entry point and also has a viewController as a rootViewController of that nav controller, which means navController is allocated and initialized when I call [mainStoryboard instantiateInitialViewController].

    I wanted to focus more on the error: "VideoBackgroundConfig with screen size of zero received, skipping config step", but still have no clue..
     
  17. Dover8

    Dover8

    Joined:
    Aug 20, 2010
    Posts:
    94
    I've managed to solve this issue, so if anyone else needs a solution here it is:
    • In the appController subclass, also implement the createAutoRotatingUnityViewController and pass back a new UnityDefaultViewController from it.
    • Code (CSharp):
      1. -(UIViewController*)createAutorotatingUnityViewController
      2. {
      3.     return [[UnityDefaultViewController alloc] init];
      4. }
    • In UnityAppController, disable the calls to createRootViewController in checkOrientationRequest and onForcedOrientation
    • change onForcedOrientation to this:
    • Code (CSharp):
      1.  
      2. -(void)onForcedOrientation:(ScreenOrientation)orient
      3. {
      4.     [_unityViewwillRotateTo:orient];
      5.     OrientView(_rootController, _rootView, orient);
      6.     [_rootViewlayoutSubviews];
      7.     [_unityViewdidRotate];
      8. }
      9.  
    This has restored my view hierarchy and not I have control over the rotations again. Yay! Though I understand someone at Unity may shout at me for butchering their code. As a further note, the checkOrientationRequest method is being called every frame. Surely this isn't the best practise?
     
  18. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,623
    >>Though I understand someone at Unity may shout at me for butchering their code
    quite the opposite - we worked hard to have ALL ui code opened in trampoline for you to tweak. While i'm obviously out of the loop on why you want to do this "butchering", that is pretty workable solution (though beware of ios8+ and fixed orientation - but you should test yourself maybe you wont hit issues)
    >>the checkOrientationRequest method is being called every frame. Surely this isn't the best practise?
    i would be interested in hearing the other option
     
  19. Dover8

    Dover8

    Joined:
    Aug 20, 2010
    Posts:
    94
    OK, so I did miss one thing. While the Unity view itself was rotating correctly, that view also happened to contain a Vuforia AR camera which was NOT rotating correctly. I needed to pass down the willRotateToInterfaceOrientation messages to the CustomViewController that had the UnityView as a subview, and get it to call the
    Code (objC):
    1. willRotateTo:ConvertToUnityScreenOrientation(toInterfaceOrientation, 0)
    method. Now, finally, everything is rotating as expected. More testing is required, of course.
     
  20. thenerd

    thenerd

    Joined:
    Aug 6, 2014
    Posts:
    6
  21. madhur

    madhur

    Joined:
    May 16, 2012
    Posts:
    86
    Hi,
    I am upgrading my native project to use Unity 5 and Xcode 6.4 and the basics seems to be working fine with the help of different posts and tutorials. But there's one issue I am facing. My app is running only in landscape. When I go to the UI withe Unity view the orientation seems to be coming in the Portrait always (So there's black border coming). Other UIs in the storyboard are coming in landscape. But the Unity view is not coming like that. Please see the attached image.

    @thenerd - Thanks for your video. When watching that I noticed that in that also that red box is coming like this.(at 27.23 in the video) but after that your unity views are coming in full screen.

    Please let me know what do I have to change to make the orientation work properly.

    Thanks orientation.png
     
  22. aventurella

    aventurella

    Joined:
    Sep 10, 2015
    Posts:
    5
    Here is a take on this, which works, for Swift 2.1, Xcode 7.1, Unity 5.2.2f1

    https://github.com/blitzagency/ios-unity5

    This can be full screen or non-full screen. Additionally you can use this in storyboards or not. It's based on the work by Frederik Jacques from this video http://www.the-nerd.be/2015/08/20/a-better-way-to-integrate-unity3d-within-a-native-ios-application/ with modifications for Swift, and I captured all of his configuration adjustments and framework linking in a Unity.xcconfig file (you can find it in the repo along with the obj-c bridging header.
     
    jack_loverde likes this.
  23. Drewlon

    Drewlon

    Joined:
    Dec 21, 2015
    Posts:
    2
    Does anyone have any advice on how to accomplish this with a Unity project that was build using the Cardboard sdk? I have gone through the amazing tutorial provided by Frederik, and have gotten it to work when its a non VR Unity project. Specifically, I am getting Uncaught exception: NSInvalidArgumentException: -[CardboardAppController unityController]: unrecognized selector sent to instance 0x15f6c33b0.
    Xcode sees two AppControllers, the "CardboardAppController.mm" in the iOS/Plugins/ folder, and the "UnityAppController.mm" in the imported "Classes" folder... Any advice on how to merge or bridge the two?
     
  24. Paulius-Liekis

    Paulius-Liekis

    Joined:
    Aug 31, 2009
    Posts:
    672
    Has anyone figured out a way to kill Unity view and then restart it again?
     
  25. ruzhuxiaogu

    ruzhuxiaogu

    Joined:
    Jul 18, 2016
    Posts:
    1
    Would you like give me a whole demo? I will appreciate it ,my email is m18354276092@163.com,thanks!
     
  26. arcontech

    arcontech

    Joined:
    Sep 3, 2016
    Posts:
    1
    Hi everyone, I have a little problem... :D
    I have managed to integrate Unity 5.4.0, Vuforia 6.0, and Xcode, it is all working fine, except.... the actions on the view controllers. For example, if my initial ViewController has a label and a button, viewdidload() is called and the code in there works fine, but in the same ViewController, if I assign an @Action to the button, this one doesnt gets called. The button gets pressed, I can see the animation, but the action never goes through. What can be happening?

    Here is my UnityAppController Subclass:

    @interface MyAppController : UnityAppController



    - (void)willStartWithViewController;

    - (void)createViewHierarchyImpl;


    @End


    @implementation MyAppController

    - (void)willStartWithViewController:(UIViewController*)controller {



    UIStoryboard *storyBoard = [UIStoryboardstoryboardWithName:mad:"Main"bundle:nil];

    UIViewController *mainVC = [storyBoard instantiateInitialViewController];



    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    self.window.rootViewController = mainVC;



    _rootController = self.window.rootViewController;

    _rootView = _rootController.view;



    _rootController = [[UnityDefaultViewControlleralloc] init];

    _rootView = [[UIViewalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    _rootController.view = _rootView;

    }






    - (void)createViewHierarchyImpl;

    {UIStoryboard *mainStoryboard = [UIStoryboardstoryboardWithName:mad:"Main"bundle: nil];

    UIViewController *mainVC = [mainStoryboard instantiateInitialViewController];



    _rootController = mainVC;

    _rootView = mainVC.view;

    }


    @End

    and Here is my ViewController:

    import UIKit


    class ViewController: UIViewController {





    overridefunc viewDidLoad() {

    super.viewDidLoad()



    //self.view.backgroundColor = UIColor.redColor();

    // Do any additional setup after loading the view.

    }



    @IBActionfunc button() {

    self.view.backgroundColor = UIColor.redColor()

    }

    Please, any help will be appreciated.

    Thanks!!
     
  27. jack_loverde

    jack_loverde

    Joined:
    Apr 16, 2014
    Posts:
    6
  28. minimalmally

    minimalmally

    Joined:
    Oct 21, 2016
    Posts:
    12
    I was wondering if you guys tried loading unity VC into an imessage VC which is available on iOS10. I'm having a hard time because it doesn't have appdelgate file. Any help or suggestions is greatly appreciated.
     
  29. eppz

    eppz

    Joined:
    Aug 2, 2014
    Posts:
    172
    Last edited: Jan 31, 2018
  30. zero_null

    zero_null

    Joined:
    Mar 11, 2014
    Posts:
    159
    how to build when I don't have UnityAppController.h
    I mean, I am trying to build myplugin.a file from a clean new xcode project, that I can use in my game.
     
  31. unity_IsXwPrbyxlHwuQ

    unity_IsXwPrbyxlHwuQ

    Joined:
    Sep 14, 2017
    Posts:
    2
    Hi

    I want to integrate unity and iOS, but I have this error to moment compile application, when I try to invoke from AppDelegate the function

    currentUnityController!.application(application, didFinishLaunchingWithOptions: launchOptions)
    It generates the error:

    was compiled with optimization - stepping may behave oddly; variables may not be available.

    I followed this guide but do not get his solution

    https://www.agnosticdev.com/blog-entry/swift/integrating-unity-and-vuforia-ios-swift-project?page=1https://github.com/blitzagency/ios-unity5

    This my error:
     

    Attached Files:

  32. tanalavijayios

    tanalavijayios

    Joined:
    Dec 21, 2017
    Posts:
    3
    Hi, Everyone. I integrated unity in iOS it is working fine but what i want is before tapping the unity is running in the background. But i dont want like that when I tap on the button only unity should start. any solution please tell me.
    mail : tanalavijayios@gmail.com
     
  33. robotunity

    robotunity

    Joined:
    Mar 20, 2014
    Posts:
    6

    These sample files are no longer online. Do they exist anywhere?
     
  34. dustin_red

    dustin_red

    Joined:
    Feb 7, 2018
    Posts:
    46
  35. dustin_red

    dustin_red

    Joined:
    Feb 7, 2018
    Posts:
    46
    I'm trying to just overlay a native UI button on top of the UnityView and getting some errors.

    "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UnityPortraitOnlyViewController showRecordButton]: unrecognized selector sent to instance"

    I'm a long time Unity/C# dev, but still wrapping my brain around native iOS. I think this means the showRecordButton method could not be found when called in the "MyViewController.mm" ToggleRecordButton(int show). I'm not sure if its not properly exposed in the header or I'm just not calling it correctly.

    Any help?

    From Unity:
    Code (CSharp):
    1.  
    2.  
    3.         [DllImport("__Internal")]
    4.         private static extern void ToggleRecordButton(int show);
    5.  
    6.         public static void CallToggleRecordButton(bool show) {
    7.             ToggleRecordButton((show) ? 1 : 0);
    8.         }
    9.  
    10.  
    In my "Plugins/iOS/MyAppController.mm":
    Code (CSharp):
    1. #import <UIKit/UIKit.h>
    2. #import "UnityAppController.h"
    3. #import "UI/UnityView.h"
    4. #import "MyViewController.h"
    5.  
    6. @interface MyAppController : UnityAppController
    7. {
    8. }
    9. - (void)willStartWithViewController;
    10. @end
    11. @implementation MyAppController
    12. - (void)willStartWithViewController:(UIViewController*)controller {
    13.  
    14. // Make your view controller the root view controller
    15.     _rootController = [[MyViewController alloc] init];
    16.  
    17.     // Setup Unity View
    18.     _unityView.contentScaleFactor = UnityScreenScaleFactor([UIScreen mainScreen]);
    19.     _unityView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    20.     _unityView.frame = _rootController.view.bounds;
    21.  
    22.     // Set the root view to be your custom view controller's root view.
    23.     _rootView = _rootController.view;
    24.  
    25.     // Add the unity view to the back of your custom view controller
    26.     [_rootController.view addSubview:_unityView];
    27.     [_rootController.view sendSubviewToBack:_unityView];
    28.  
    29. }
    30. @end
    31.  
    32. IMPL_APP_CONTROLLER_SUBCLASS(MyAppController)
    33.  
    In my "Plugins/iOS/MyViewController.h":
    Code (CSharp):
    1. #import <UIKit/UIKit.h>
    2. #import "UI/UnityViewControllerBase.h"
    3.  
    4. @interface MyViewController : UnityDefaultViewController
    5.  
    6. -(void)showRecordButton;
    7. -(void)hideRecordButton;
    8.  
    9. @end
    10.  
    11.  
    In my "Plugins/iOS/MyViewController.mm":
    Code (CSharp):
    1. #import "MyViewController.h"
    2.  
    3. @implementation MyViewController
    4. {
    5.     UIButton* _recordButton;
    6. }
    7. - (void)showRecordButton
    8. {
    9.     [self showRecordButtonView];
    10. }
    11.  
    12. - (void)showRecordButtonView
    13. {
    14.     if(_recordButton == nil)
    15.     {
    16.         _recordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    17.         [_recordButton setFrame:CGRectMake(0, 0, 100, 100)];
    18.         //[_recordButton setBackgroundImage:[UIImage imageNamed:@"sig.png"] forState:UIControlStateNormal];
    19.         [_recordButton setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 50.0, 0.0)];
    20.         [_recordButton setTitleEdgeInsets:UIEdgeInsetsMake(75.0, 0.0, 0.0, 0.0)];
    21.         [_recordButton setTitle:@"record" forState:UIControlStateNormal];
    22.         [_recordButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    23.     }
    24.  
    25.     [self.view addSubview:_recordButton];
    26. }
    27.  
    28. - (void)hideRecordButton
    29. {
    30.     if (_recordButton != nil) {
    31.         [_recordButton removeFromSuperview];
    32.     }
    33. }
    34.  
    35. - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    36. {
    37.     [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    38.     _recordButton.frame = self.view.bounds;
    39. }
    40. @end
    41.  
    42. extern "C" void ToggleRecordButton(int show)
    43. {
    44.     MyViewController* controller = (MyViewController*)UnityGetGLViewController();
    45.     if (show == 1) {
    46.         [controller showRecordButton];
    47.     } else {
    48.         [controller hideRecordButton];
    49.     }
    50. }
    51.  
     
    Last edited: Mar 13, 2018
  36. dustin_red

    dustin_red

    Joined:
    Feb 7, 2018
    Posts:
    46
    I was able to get the method calls working by making the custom view controller a singleton. But I don't see the button added to the view when its called. :(

    Add to the "Plugins/iOS/MyViewController.h":
    Code (CSharp):
    1. +(instancetype) singleton;
    And add to the "Plugins/iOS/MyViewController.mm":
    Code (CSharp):
    1. +(instancetype) singleton
    2. {
    3.     static id _sharedInstance = nil;
    4.     static dispatch_once_t oncePredicate;
    5.     dispatch_once(&oncePredicate, ^
    6.                   {
    7.                       _sharedInstance = [self new];
    8.                   });
    9.     return _sharedInstance;
    10. }
    And change the "Plugins/iOS/MyViewController.mm" ToggleRecordButton method to reference the singleton:
    Code (CSharp):
    1. extern "C" void ToggleRecordButton(int show)
    2. {
    3.     if (show == 1) {
    4.         [[MyViewController singleton] showRecordButton];
    5.     } else {
    6.         [[MyViewController singleton] hideRecordButton];
    7.     }
    8. }
     
  37. divya_thakkar

    divya_thakkar

    Joined:
    Oct 27, 2018
    Posts:
    1
    I can succesfully integrate unity in native ios project. Now, I can open unity in viewdidLoad but I want that, When I click on button then it should be open. Please help. Thanks in advance.
    This is the code:
    What I am trying to call unity scene on button click.

    Code (CSharp):
    1. - (IBAction)btnClicked:(UIButton *)sender {
    2.      NSLog(@"Button action called..");
    3.     UnityAppController *appController = (UnityAppController*) [[UIApplication sharedApplication] delegate];
    4.  
    5.  
    6.     [self.view addSubview:appController.unityView];
    7.     [self.view sendSubviewToBack:appController.unityView];
    8.  
    9. }
    Please help If I'm doing anything wrong.