Skip to content

Commit

Permalink
Merge pull request #6 from handsomecode/enh/config_file
Browse files Browse the repository at this point in the history
Update configuration file and refresh mechanism
  • Loading branch information
enmiller authored Nov 1, 2017
2 parents 41d9b85 + d28f551 commit b1a7efa
Show file tree
Hide file tree
Showing 16 changed files with 533 additions and 734 deletions.
32 changes: 8 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,30 @@ make
```

## Usage
### To generate a new project
Currently, Unity needs to be closed for this process to begin.
### To generate new iOS and Unity projects
_Currently, Unity needs to be closed for this process to begin._

1. Create a top-level folder and navigate to it. This folder will contain all information about the Xcode and Unity projects. (_Note: By default, the name of this folder will be the name of the Xcode and Unity projects._)
```
mkdir ExampleProject
cd ExampleProject
```

2. Run the following to generate the `ubconfig.json` file where you can specify project names and paths
2. Run the following to generate the `ubconfig.json` file where you can specify project information
```
UnityBuildKit init
UnityBuildKit config
```

3. After filling out the config file information, run
```
$ UnityBuildKit generate
```

4. Once generation is complete, open the Xcode project. Using the Build Scheme drop down menu, select Edit Scheme to edit your project's scheme (the scheme should be the same name as your project). Select the Run action then the Options tab. In the middle of the window, you will see `Metal API Validation` with a set of options in a drop down menu. Change the current option to `Disabled`, as seen below:
![](Assets/metal_validation_scheme.png)
_(Note: if you do not see the option for Metal API Validation, verify your run destination is a physical device of set to Generic iOS Device)_

#### Notes
- The generation script sets up the Unity project to build for the Device SDK. These means that, if building for a simulator, there is a high probability that you will encounter build and linker errors in Xcode. Change the run destination to a physical device and the errors should go away.

- If building for a physical device, do not forget to set up your code signing.

### To refresh the Xcode project
After making updates to your Unity project, you'll need to refresh the files linked with the Xcode project. Currently, Unity needs to be closed for this process to begin.
- The generation script sets up the Unity project to build for the Device SDK. These means that, if building for a simulator, there is a high probability that you will encounter build and linker errors in Xcode. Change the run destination to a physical device and the errors should go away. You can change this in Unity using the Build Settings once generation is completed.

1. Navigate to the top-level folder
```
cd ExampleProject
```

2. Run
```
UnityBuildKit refresh
```
### Refreshing the projects
The iOS project is automatically updated and refreshed every time the Unity project is built. You shouldn't need to do anything! 😀

## Known Unity Version Compatibility
- 2107.1.f1
Expand All @@ -86,7 +70,7 @@ This tool is built using:

and the wonderful dependencies they bring with them.

Inspiration for building `UnityBuildKit` came after running into several problems while trying to [manually do this process](https://the-nerd.be/2015/11/13/integrate-unity-5-in-a-native-ios-app-with-xcode-7/) and reading over a [github issue](https://github.com/blitzagency/ios-unity5/issues/52) trying to resolve those problems.
Inspiration for building `UnityBuildKit` came after running into several problems while trying to [manually do this process](https://the-nerd.be/2015/11/13/integrate-unity-5-in-a-native-ios-app-with-xcode-7/) and reading over a [github issue](https://github.com/blitzagency/ios-unity5/issues/52) trying to resolve those problems (big thanks to [jiulongw](https://github.com/jiulongw/swift-unity)).

## License

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// config.swift
// configFile.swift
//
// Copyright (c) 2017 Handsome
//
Expand Down Expand Up @@ -28,11 +28,20 @@ extension File {
class func configFile() -> Data? {
let file = """
{
"project_name": "",
"bundle_id": "<com.example.project_name>",
"unity_path": "<path_to_Unity>",
"unity_version": "",
"unity_scene_name": ""
"ios": {
"projectName": "ExampleProject",
"bundleId": "com.example.ExampleProject",
"projectPath": "" // Defaults to iOS/
},
"unity": {
"projectName": "ExampleProject",
"applicationPath": "", // e.g /Applications/Unity/Unity.app/Contents/MacOS/Unity
"version": "", // e.g. 2017.1.f1
"projectPath": "", // Defaults to Unity/
"sceneNames": [
"ExampleScene"
]
}
}
""".data(using: .utf8)
return file
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// UnityEditorScript.swift
// UnityEditorBuildScript.swift
//
// Copyright (c) 2017 Handsome
//
Expand Down
163 changes: 163 additions & 0 deletions Sources/UBKit/Files/Unity/UnityProjectScript.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//
// UnityProjectScript.swift
// UnityBuildKitPackageDescription
//
// Copyright (c) 2017 Handsome
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Foundation

extension File {

class func unityProjectScriptFile(projectName: String, iOSProjectPath: String) -> Data? {
let file = """
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
public class XcodeRefresher {
private const string iOSProjectRoot = \"\(iOSProjectPath)\";
private const string iOSProjectName = \"\(projectName)\";
private const string ClassesProjectPath = "Vendor/UBK/Classes";
private const string LibrariesProjectPath = "Vendor/UBK/Libraries";
private const string PbxFilePath = iOSProjectName + ".xcodeproj/project.pbxproj";
public static void Refresh() {
var pathToBuiltProject = GetArg ("-buildPath");
UpdateUnityProjectFiles (pathToBuiltProject);
}
#if UNITY_IOS
[PostProcessBuild]
public static void OnPostBuild(BuildTarget target, string pathToBuiltProject) {
UpdateUnityProjectFiles(pathToBuiltProject);
}
#endif
private static void UpdateUnityProjectFiles(string pathToBuiltProject) {
var pbx = new PBXProject();
var pbxPath = Path.Combine(iOSProjectRoot, PbxFilePath);
pbx.ReadFromFile(pbxPath);
ProcessUnityDirectory(
pbx,
Path.Combine(pathToBuiltProject, "Classes"),
Path.Combine(iOSProjectRoot, ClassesProjectPath),
ClassesProjectPath);
ProcessUnityDirectory(
pbx,
Path.Combine(pathToBuiltProject, "Libraries"),
Path.Combine(iOSProjectRoot, LibrariesProjectPath),
LibrariesProjectPath);
pbx.WriteToFile(pbxPath);
}
/// <summary>
/// Update pbx project file by adding src files and removing extra files that
/// exists in dest but not in src any more.
///
/// This method only updates the pbx project file. It does not copy or delete
/// files in Swift Xcode project. The Swift Xcode project will do copy and delete
/// during build, and it should copy files if contents are different, regardless
/// of the file time.
/// </summary>
/// <param name="pbx">The pbx project.</param>
/// <param name="src">The directory where Unity project is built.</param>
/// <param name="dest">The directory of the Swift Xcode project where the
/// Unity project is embedded into.</param>
/// <param name="projectPathPrefix">The prefix of project path in Swift Xcode
/// project for Unity code files. E.g. "DempApp/Unity/Classes" for all files
/// under Classes folder from Unity iOS build output.</param>
private static void ProcessUnityDirectory(PBXProject pbx, string src, string dest, string projectPathPrefix) {
var targetGuid = pbx.TargetGuidByName(iOSProjectName);
string[] newFiles, extraFiles;
CompareDirectories(src, dest, out newFiles, out extraFiles);
foreach (var f in newFiles) {
var projPath = Path.Combine(projectPathPrefix, f);
if (!pbx.ContainsFileByProjectPath(projPath)) {
var guid = pbx.AddFile(Path.Combine(src, f), projPath, PBXSourceTree.Absolute);
pbx.AddFileToBuild(targetGuid, guid);
Debug.LogFormat("Added file to pbx: '{0}'", projPath);
}
}
foreach (var f in extraFiles) {
var projPath = Path.Combine(projectPathPrefix, f);
if (pbx.ContainsFileByProjectPath(projPath)) {
var guid = pbx.FindFileGuidByProjectPath(projPath);
pbx.RemoveFile(guid);
Debug.LogFormat("Removed file from pbx: '{0}'", projPath);
}
}
}
/// <summary>
/// Compares the directories. Returns files that exists in src and
/// extra files that exists in dest but not in src any more.
/// </summary>
private static void CompareDirectories(string src, string dest, out string[] srcFiles, out string[] extraFiles) {
srcFiles = GetFilesRelativePath(src);
var destFiles = GetFilesRelativePath(dest);
var extraFilesSet = new HashSet<string>(destFiles);
extraFilesSet.ExceptWith(srcFiles);
extraFiles = extraFilesSet.ToArray();
}
private static string[] GetFilesRelativePath(string directory) {
var results = new List<string>();
if (Directory.Exists(directory)) {
foreach (var path in Directory.GetFiles(directory, "*", SearchOption.AllDirectories)) {
var relative = path.Substring(directory.Length).TrimStart('/');
results.Add(relative);
}
}
return results.ToArray();
}
private static string GetArg(string name) {
var args = System.Environment.GetCommandLineArgs();
for (int i = 0; i < args.Length; i++) {
if (args [i] == name && args.Length > i + 1) {
return args [i + 1];
}
}
return null;
}
}
""".data(using: .utf8)
return file
}
}
5 changes: 4 additions & 1 deletion Sources/UBKit/Files/Xcode/SpecFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ extension File {
sources:
- \(projectName)
- Vendor
postbuildScripts:
- script: rm -rf "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Data"\\ncp -Rf "$UNITY_IOS_EXPORT_PATH/Data" "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Data"
name: UnityBuildKit Postbuild
settings:
PRODUCT_BUNDLE_IDENTIFIER: \(bundleIdentifier)
IOS_DEPLOYMENT_TARGET: 11.0
Expand All @@ -58,7 +61,7 @@ extension File {
OTHER_CFLAGS: $(inherited) -DINIT_SCRIPTING_BACKEND=1 -fno-strict-overflow -DRUNTIME_IL2CPP=1
CLANG_CXX_LANGUAGE_STANDARD: c++11
CLANG_CXX_LIBRARY: libc++
SWIFT_OBJC_BRIDGING_HEADER: UnityBridge.h
SWIFT_OBJC_BRIDGING_HEADER: UnityBuildKit/UnityBridge/UnityBridge.h
CLANG_ENABLE_MODULES: NO
CLANG_WARN_BOOL_CONVERSION: NO
CLANG_WARN_CONSTANT_CONVERSION: NO
Expand Down
Loading

0 comments on commit b1a7efa

Please sign in to comment.