11using UnityEngine.Assertions;
14using UnityEditor.Callbacks;
15using UnityEditor.iOS.Xcode;
19public static class UnityCoreHapticsPostProcessor
22 const string MODULE_MAP_FILENAME =
"module.modulemap";
25 private static bool assetStoreBuild =
false;
26 private static string pluginRelativePathInXCode;
30 public static void OnPostProcessBuild(BuildTarget buildTarget,
string buildPath)
32 if (buildTarget == BuildTarget.iOS)
34 var pbxProjectPath = PBXProject.GetPBXProjectPath(buildPath);
35 var proj =
new PBXProject();
36 proj.ReadFromFile(pbxProjectPath);
39 var guids = AssetDatabase.FindAssets(
"t:Script UnityCoreHaptics");
40 if (guids.Length == 0)
42 Debug.LogError(
"UnityCoreHaptics script not found");
45 string scriptPath = AssetDatabase.GUIDToAssetPath(guids[0]);
46 Debug.Log(
"UnityCoreHaptics Path:" + scriptPath);
49 string assetsFolder =
"Assets/";
52 if (scriptPath.StartsWith(assetsFolder))
54 assetStoreBuild =
true;
58 assetStoreBuild =
false;
61 string targetGUID = proj.GetUnityFrameworkTargetGuid();
65 var pluginRelativePathInUnity = GetPluginPathRelativeToAssets();
68 string pluginRelativePathInXCode =
"";
71 pluginRelativePathInXCode = Path.Combine(
"Libraries", pluginRelativePathInUnity);
75 pluginRelativePathInXCode = Path.Combine(
"Libraries",
"com.interhaptics.core_sdk", pluginRelativePathInUnity);
78 proj.AddFrameworkToProject(targetGUID,
"CoreHaptics.framework",
false);
79 proj.SetBuildProperty(targetGUID,
"ENABLE_BITCODE",
"NO");
81 proj.AddBuildProperty(targetGUID,
"CLANG_ENABLE_MODULES",
"YES");
82 proj.AddBuildProperty(targetGUID,
"SWIFT_INCLUDE_PATHS", pluginRelativePathInXCode);
83 proj.AddBuildProperty(targetGUID,
"LD_RUNPATH_SEARCH_PATHS",
"@executable_path/Frameworks");
85 WriteModuleToFramework(proj, targetGUID, pbxProjectPath, pluginRelativePathInUnity, pluginRelativePathInXCode);
91 private static string _normalizePath(
string path)
93 return Path.GetFullPath(
new Uri(path).LocalPath)
94 .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
98 private static string _getRelativePath(
string basePath,
string fullPath) {
100 if (fullPath ==
null || fullPath ==
"") {
104 if (_normalizePath(fullPath) == _normalizePath(basePath)) {
108 var dirPath = Path.GetDirectoryName(fullPath);
109 return Path.Combine(_getRelativePath(basePath, dirPath), Path.GetFileName(fullPath));
112 private static string GetPluginPathRelativeToAssets() {
116 files = System.IO.Directory.GetFiles(UnityEngine.Application.dataPath,
"UnityCoreHapticsProxy.cs", SearchOption.AllDirectories);
120 files = System.IO.Directory.GetFiles(Path.GetFullPath(
"Packages/com.interhaptics.core_sdk"),
"UnityCoreHapticsProxy.cs", SearchOption.AllDirectories);
122 if (files.Length != 1) {
123 throw new Exception(
"[UnityCoreHapticsPostProcessor] Error: there should exactly be one file named UnityCoreHapticsProxy.cs");
127 return Path.GetDirectoryName(_getRelativePath(UnityEngine.Application.dataPath, files[0]));
131 return Path.GetDirectoryName(_getRelativePath(Path.GetFullPath(
"Packages/com.interhaptics.core_sdk"), files[0]));
135 private static void WriteModuleToFramework(
138 string pbxProjectPath,
139 string pluginRelativePathInUnity,
140 string pluginRelativePathInXCode
144 string moduleMapDestRelativePath = Path.Combine(pluginRelativePathInXCode, MODULE_MAP_FILENAME);
145 Debug.Log(
"[UnityCoreHapticsPostProcessor] Adding properties to XCode framework. Module path : " + moduleMapDestRelativePath);
146 string file_guid = proj.AddFile(moduleMapDestRelativePath, moduleMapDestRelativePath, PBXSourceTree.Source);
147 proj.AddFileToBuild(targetGUID, file_guid);
148 proj.WriteToFile(pbxProjectPath);
154 sourcePath = Path.Combine(UnityEngine.Application.dataPath, pluginRelativePathInUnity, MODULE_MAP_FILENAME);
158 sourcePath = Path.Combine(Path.GetFullPath(
"Packages/com.interhaptics.core_sdk"), pluginRelativePathInUnity, MODULE_MAP_FILENAME);
160 string destPath = Path.Combine(Path.GetDirectoryName(pbxProjectPath),
"..", moduleMapDestRelativePath);
161 if (!Directory.Exists(Path.GetDirectoryName(destPath)))
163 Debug.Log(
"[UnityCoreHapticsPostProcessor] Creating directory " + destPath);
164 Directory.CreateDirectory(Path.GetDirectoryName(destPath));
166 Debug.Log(
"[UnityCoreHapticsPostProcessor] Copy module file to project : " + sourcePath +
" -> " + destPath);
167 File.Copy(sourcePath, destPath);