触摸一个UIButton并在UIView或UIViewController上显示Unity [英] Touch a UIButton and show Unity on UIView or UIViewController

查看:142
本文介绍了触摸一个UIButton并在UIView或UIViewController上显示Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Unity论坛上读了几个答案,包括这里的内容,仅展示了如何创建子类并展示了我的第一个UIViewController.

I read several answers on the Unity forum, including here and only show how to create a subclass and present my first UIViewController.

- (void)createViewHierarchyImpl
{
    homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];

    //_rootView = _unityView;
    _rootView = homeViewController.view;

    //_rootController = [[UnityDefaultViewController alloc] init];
    _rootController = homeViewController;
}

到目前为止,很好.
问题在于,没有新示例生成XCode的Unity插件示例来显示带有Unity的UIViewController.

So far, so good.
The problem is that there are no examples for the new version of the Unity plugin that generates XCode, to show a UIViewController with Unity.

-(void)goToUnity:(id)sender
{
    NSLog(@"touch the button is OK");

    UIViewController *app = [[UIViewController alloc] initWithNibName:@"UnityView" bundle:nil];

    [self.navigationController pushViewController:app animated:YES];
}

因此,什么都没有发生. 有什么方法可以在默认"模式的Xcode中调用Unity?

Thus, nothing happens. Is there any way to call the Unity in "default" mode Xcode?

谢谢.

推荐答案

读了很多遍,多次测试并犯了错误,我终于做到了!触摸一个UIButton并打开或执行您想像的操作.

我将在这里与那些需要更完整代码的人分享.
入门:
您需要创建自己的主文件.h和.m才能自动压倒" Unity:

mainAppController.h

Read an read and read many times, test and erros many times, i finally do! Touch a UIButton and open or do whatever you imagine with it.

I will share here for those who need a more complete code.
Getting Started:
You need to create your own main file .h and .m to be "overruled" the Unity automatically:

mainAppController.h

#import "UnityAppController.h"
#import "UI/UnityView.h"
#import "iPhone_View.h"

@interface mainAppController : UnityAppController
{
    UnityAppController  *_unityAppController;
}

- (void)createViewHierarchyImpl;

@end

您必须导入此H标头以控制Unity内容.

You have to import this H headers to controll Unity stuffs.

mainAppController.m

#import "mainAppController.h"

@implementation mainAppController

-(void)createViewHierarchyImpl
{
    if(_unityAppController == nil)
    {
        _unityAppController = [[UnityAppController alloc] init];

        NSLog(@"Unity Controller was set NICE!!!!");
    }
    UIStoryboard *storyBoard;

    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
    {
        storyBoard = [UIStoryboard storyboardWithName:@"iPadMain" bundle:nil];
    }
    else
    {
        storyBoard = [UIStoryboard storyboardWithName:@"iPhoneMain" bundle:nil];
    }

    UIViewController *mainVC = [storyBoard instantiateInitialViewController];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.rootViewController = mainVC;

    _rootController = [self.window rootViewController];
    //_rootController.wantsFullScreenLayout = TRUE;
    _rootController.edgesForExtendedLayout = TRUE;

    _rootView       = _rootController.view;

    [self.window makeKeyAndVisible];
}

@end

IMPL_APP_CONTROLLER_SUBCLASS(mainAppController) //THIS IS VERY IMPORTANT!!!

最后一行是Unity,它将读取"并使用.

This last line is the Unity which will "read" and use.

所以,看,我使用了情节提要,因此我可以从视觉上更好地控制导航.但是您可以使用任何您想要的东西.

So, see, I used the storyboard, so I have more control over the navigation visually. But you can use whatever you want.

在其他任何UIViewController中,我将调用Unity,如下所示:

In another any UIViewController, I'll call the Unity, as follows:

ShowARViewController.h

#import <UIKit/UIKit.h>
#import "UnityAppController.h"
#import "UI/UnityView.h"
#import "iPhone_View.h"

@interface ShowARViewController : UIViewController
{
    UnityAppController *unityController;
    IBOutlet UIView *viewToUnity; // This is linked on Storyboard 
}
@end

ShowARViewController.m

#import "ShowARViewController.h"

@interface ShowARViewController ()

@end

@implementation ShowARViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    unityController = (UnityAppController*)[[UIApplication sharedApplication] delegate];
    [viewToUnity addSubview:unityController.unityView];
}
// more methods and codes...
@end

看,因此,我可以对UIView中添加的Unity进行更多控制,并且可以执行我想要的操作,例如,在此视图上方添加一个按钮.

See, so I can have more control of Unity that I added in UIView and I can do what I want, for example, add a button above this view.

我希望我真的有所帮助. 如果您有任何疑问或建议来改进此代码,请随时给我写信.

I hope I have helped really. If you have any questions or suggestions to make this code better, feel free to write me.

致谢

这篇关于触摸一个UIButton并在UIView或UIViewController上显示Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆