集成 Unity (2017.1+) 场景到 iOS (Swift 4.0, Xcode 9+) 项目的示例代码。
This repo contains a demo Xcode 9.1 iOS project (Swift 4.0) that builds a single view app with an embeded Unity scene (Unity 2017.2.0f3) with two-way communication between Unity and Swift. It contains automation script that could be easily re-used in existing Unity and Swift projects.
This would not be possible without tutorial by BLITZ and video by the-nerd Frederik Jacques. Several updates are applied to fit latest Xcode and Unity releases.
_main) undefinedI like to automate things when I find myself doing the same thing several times, and potentially over and over again. If you are (like I am) tired of these tedious steps every time you export a new Unity build, you will love this automation. Once integrated, Swift project gets updated automatically everytime a new Unity build is exported.
First open Unity project, open Demo scene, open Build Settings and switch to iOS platform, then hit Build. You
can put output anywhere but better put it to some temporary folder such as /tmp.
Once build succeeded, open Xcode project, select target device (Unity does not support x86_64) and hit
Build and then run. You might need to change bundle identifier if Xcode has problem creating provisioning profile.
Copy this post build script to your Unity project's Assets folder. Put anywhere you like but since this is an
editor script, Unity requires it to be under a folder named Editor.
Open the script and follow the instructions in the comments. You need to tell it the path to your Swift project and the name of your awesome product.
///
/// Path to the root directory of Xcode project.
/// This should point to the directory of '${XcodeProjectName}.xcodeproj'.
/// It is recommended to use relative path here.
/// Current directory is the root directory of this Unity project, i.e. the directory of 'Assets' folder.
/// Sample value: "../xcode"
///
private const string XcodeProjectRoot = ;
///
/// Name of the Xcode project.
/// This script looks for '${XcodeProjectName} + ".xcodeproj"' under '${XcodeProjectRoot}'.
/// Sample value: "DemoApp"
///
private const string XcodeProjectName = ;You're done with Unity part!
Xcode side requires a bit more one time effort.
Copy entire Unity folder to the root of your Swift project and add it to Xcode source tree. Make sure all the
.mm files are added to correct build target.
Add following lines to the .gitignore file in the same directory of your .xcodeproj entry. If you don't have one
there, create one or merge it to your higher level git ignore file with proper updates to the paths.
# This file is generated by Unity post build action
DemoApp/Unity/Exports.xcconfig
# Files under Libraries will be copied from Unity build during Xcode build
DemoApp/Unity/Libraries/
# Files under Classes will be copied from Unity build during Xcode build
DemoApp/Unity/Classes/Select Unity as project configuration file. If you already have your own, merge Unity/Unity.xcconfig to your own.
If you don't have Objective-C bridging header file, you can skip this. Otherwise, merge Unity/Bridging-Header.h
to your own bridging header and comment out following lines in Unity/Unity.xcconfig:
SWIFT_OBJC_BRIDGING_HEADER = $(PRODUCT_NAME)/Unity/Bridging-Header.h;
SWIFT_PRECOMPILE_BRIDGING_HEADER = YES;DemoApp settings. Note the order. If your project name has space(s) in it, surround the $PRODUCT_NAME with double quote.echo "Syncing code from $UNITY_IOS_EXPORT_PATH..."
rsync -rc --exclude-from="$PRODUCT_NAME"/Unity/rsync_exclude --delete $UNITY_IOS_EXPORT_PATH/Classes/ "$PRODUCT_NAME"/Unity/Classes/
rsync -rc --exclude-from="$PRODUCT_NAME"/Unity/rsync_exclude --delete $UNITY_IOS_EXPORT_PATH/Libraries/ "$PRODUCT_NAME"/Unity/Libraries/echo "Syncing data from $UNITY_IOS_EXPORT_PATH..."
rm -rf "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Data"
cp -Rf "$UNITY_IOS_EXPORT_PATH/Data" "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Data"Update AppDelegate.swift to initialize Unity during application start. Follow the sample AppDelegate code.
Once Unity is loaded, a UnityReady notification will be triggered. Subscribe to that notification, grab Unity
view or view controller and present them as you like. Follow the sample ViewController code.
// Get Unity View
UnityGetGLView()
// Get Unity View Controller
UnityGetGLViewController()Now you should be good to go. Give it a try!
Note: Building the Unity project for the first time will modify Xcode project file a lot. It is safe to commit the change into source control as it only performs necessary updates going forward.
暂无开放 Issues,或尚未同步最近议题。