如何在ViewController Xcode中嵌入Unity项目(objective c / swift)

时间:2017-11-03 17:47:07

标签: ios unity3d unity5

我如何在xcode 9.0中将我的Unity项目(2017.2.0f3)插入我的viewcontroller?

this guide中,实际上已过时,我收到以下错误:

clang: error: unknown argument: '-weak-LSystem'

在构建阶段 - 编译源代码中,我只获得了3个文件(ViewControllermainAppDelegate),但在教程90+编译源中

2 个答案:

答案 0 :(得分:4)

我在此处添加了指南视频:https://www.youtube.com/watch?v=2PW7_CfIwY0

我上传了一个4K 10GB视频文件..希望youtube在您看到此答案时完成处理。

应该很容易遵循(我希望..我不擅长视频)..但需要一些时间。如果您不是为Simulator构建,请删除:-DTARGET_IPHONE_SIMULATOR=1 ..相应地修改其他标志。

我在视频中使用的Debug.xcconfig如下:

UNITY_RUNTIME_VERSION = 2017.2.0f3;
UNITY_SCRIPTING_BACKEND = il2cpp;
UNITY_IOS_EXPORT_PATH = ../Unity;
GCC_PREFIX_HEADER = $(UNITY_IOS_EXPORT_PATH)/Classes/Prefix.pch;

OTHER_LDFLAGS = -weak-lSystem -weak_framework CoreMotion -weak_framework GameKit -weak_framework iAd -framework CoreGraphics -framework AVFoundation -framework CoreVideo -framework CoreMedia -framework CoreText -framework SystemConfiguration -framework CoreLocation -framework MediaPlayer -framework MediaToolbox -framework CFNetwork -framework AudioToolbox -framework OpenAL -framework Metal -framework QuartzCore -framework Metal -framework OpenGLES -framework UIKit -framework Foundation -liconv.2 -liPhone-lib;

HEADER_SEARCH_PATHS = "$(UNITY_IOS_EXPORT_PATH)/Classes" "$(UNITY_IOS_EXPORT_PATH)/Classes/Native" "$(UNITY_IOS_EXPORT_PATH)/Libraries/bdwgc/include" "$(UNITY_IOS_EXPORT_PATH)/Libraries/libil2cpp/include";
LIBRARY_SEARCH_PATHS = "$(UNITY_IOS_EXPORT_PATH)" "$(UNITY_IOS_EXPORT_PATH)/Libraries" "$(UNITY_IOS_EXPORT_PATH)/Libraries/libil2cpp/include";

ENABLE_BITCODE = YES;

OTHER_CFLAGS = -DINIT_SCRIPTING_BACKEND=1 -DTARGET_IPHONE_SIMULATOR=1 -DRUNTIME_IL2CPP=1 -fno-strict-overflow;
LD_GENERATE_MAP_FILE = YES;
CLANG_CXX_LANGUAGE_STANDARD = c++11;
CLANG_CXX_LIBRARY = libc++;
CLANG_ENABLE_MODULES = NO;
CLANG_WARN_BOOL_CONVERSION = NO;
CLANG_WARN_CONSTANT_CONVERSION = NO;
CLANG_WARN_INT_CONVERSION = NO;
CLANG_WARN_OBJC_ROOT_CLASS = YES;
CLANG_WARN_UNREACHABLE_CODE = NO;
CLANG_WARN__DUPLICATE_METHOD_MATCH = NO;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES;
CLANG_WARN_EMPTY_BODY = NO;
CLANG_WARN_ENUM_CONVERSION = NO;
GCC_C_LANGUAGE_STANDARD = c99;
GCC_ENABLE_OBJC_EXCEPTIONS = NO;
GCC_ENABLE_CPP_RTTI = NO;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_THUMB_SUPPORT = NO;
GCC_USE_INDIRECT_FUNCTION_CALLS = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION[arch=*64] = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = NO;
GCC_WARN_UNINITIALIZED_AUTOS = NO;
GCC_WARN_UNUSED_FUNCTION = NO;

视频中Launcher.mm的代码是(因此您无需全部输入):

@interface Launcher()
@property (nonatomic, assign) bool isRunning;
@end

@implementation Launcher

+ (int)initializeUnity:(int)argc argv:(char**)argv appDelegateName:(NSString *)appDelegateName {
    return UNITY_INIT(argc, argv, appDelegateName.UTF8String);
}

+ (UnityAppController *)controller {
    static UnityAppController *instance = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[UnityAppController alloc] init];
    });

    return instance;
}

- (void)start {
    if (!self.isRunning) {
        self.isRunning = true;
        [self applicationDidBecomeActive:UIApplication.sharedApplication];
    }
}

- (void)stop {
    if (self.isRunning) {
        [self applicationWillResignActive:UIApplication.sharedApplication];
        self.isRunning = false;
    }
}

- (UIView *)view {
    return UnityGetGLView();
}


- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
    return [[Launcher controller] application:application supportedInterfaceOrientationsForWindow:window];
}

- (void)application:(UIApplication*)application didReceiveLocalNotification:(UILocalNotification*)notification
{
    [[Launcher controller] application:application didReceiveLocalNotification:notification];
}


- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
    #if UNITY_USES_REMOTE_NOTIFICATIONS
    [[Launcher controller] application:application didReceiveRemoteNotification:userInfo];
    #endif
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    #if UNITY_USES_REMOTE_NOTIFICATIONS
    [[Launcher controller] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
    #endif
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler
{
    #if UNITY_USES_REMOTE_NOTIFICATIONS
    [[[Launcher controller] application:application didReceiveRemoteNotification:userInfo] fetchCompletionHandler:handler];
    #endif
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{
    #if UNITY_USES_REMOTE_NOTIFICATIONS
    [[Launcher controller] application:application didFailToRegisterForRemoteNotificationsWithError:error];
    #endif
}

- (BOOL)application:(UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation
{
    return [[Launcher controller] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
}

- (BOOL)application:(UIApplication*)application willFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    [[Launcher controller] application:application willFinishLaunchingWithOptions:launchOptions];
    return YES;
}

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    [[Launcher controller] application:application didFinishLaunchingWithOptions:launchOptions];

    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication*)application
{
    if (self.isRunning) {
        [[Launcher controller] applicationDidEnterBackground:application];
    }
}

- (void)applicationWillEnterForeground:(UIApplication*)application
{
    if (self.isRunning) {
        [[Launcher controller] applicationWillEnterForeground:application];
    }
}

- (void)applicationDidBecomeActive:(UIApplication*)application
{
    if (self.isRunning) {
        [[Launcher controller] applicationDidBecomeActive:application];
    }
}

- (void)applicationWillResignActive:(UIApplication*)application
{
    if (self.isRunning) {
        [[Launcher controller] applicationWillResignActive:application];
    }
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication*)application
{
    [[Launcher controller] applicationDidReceiveMemoryWarning:application];
}

- (void)applicationWillTerminate:(UIApplication*)application
{
    [[Launcher controller] applicationWillTerminate:application];
}
@end

答案 1 :(得分:1)

这是最新的指南,它将Unity View Controller(Unity 2017.2.0f3)集成到Xcode 9(和9.1)Swift项目中。如果您使用的是Obj-C,它会有类似的概念。它还有一个演示项目,您可以在应用于自己的项目之前查看一切是如何工作的。

https://github.com/jiulongw/swift-unity