OpenCVForUnityIOSBuildPostprocessor.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #if (UNITY_5 || UNITY_5_3_OR_NEWER) && UNITY_IOS
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEditor.Callbacks;
  5. using UnityEditor.iOS.Xcode;
  6. using System.Diagnostics;
  7. #if UNITY_2017_2_OR_NEWER
  8. using UnityEditor.iOS.Xcode.Extensions;
  9. #endif
  10. using System;
  11. using System.Collections;
  12. using System.IO;
  13. namespace OpenCVForUnity
  14. {
  15. public class OpenCVForUnityIOSBuildPostprocessor : MonoBehaviour
  16. {
  17. [PostProcessBuild]
  18. public static void OnPostprocessBuild (BuildTarget buildTarget, string path)
  19. {
  20. if (buildTarget == BuildTarget.iOS) {
  21. string[] guids = UnityEditor.AssetDatabase.FindAssets ("OpenCVForUnityIOSBuildPostprocessor");
  22. if (guids.Length == 0) {
  23. UnityEngine.Debug.LogWarning ("SetPluginImportSettings Faild : OpenCVForUnityIOSBuildPostprocessor.cs is missing.");
  24. return;
  25. }
  26. string opencvForUnityFolderPath = AssetDatabase.GUIDToAssetPath (guids [0]).Substring ("Assets/".Length);
  27. opencvForUnityFolderPath = opencvForUnityFolderPath.Substring (0, opencvForUnityFolderPath.LastIndexOf ("Editor/OpenCVForUnityIOSBuildPostprocessor.cs"));
  28. if (PlayerSettings.iOS.sdkVersion == iOSSdkVersion.DeviceSDK) {
  29. #if UNITY_2018_3_OR_NEWER
  30. RemoveSimulatorArchitectures (path + "/Frameworks/", "opencv2.framework/opencv2");
  31. #else
  32. RemoveSimulatorArchitectures (path + "/Frameworks/"+opencvForUnityFolderPath+"Plugins/iOS/", "opencv2.framework/opencv2");
  33. #endif
  34. RemoveSimulatorArchitectures (path + "/Libraries/"+opencvForUnityFolderPath+"Plugins/iOS/", "libopencvforunity.a");
  35. }
  36. #if UNITY_5_0 || UNITY_5_1 || UNITY5_2
  37. string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
  38. #else
  39. string projPath = PBXProject.GetPBXProjectPath (path);
  40. #endif
  41. PBXProject proj = new PBXProject ();
  42. proj.ReadFromString (System.IO.File.ReadAllText (projPath));
  43. #if UNITY_5_0 || UNITY_5_1 || UNITY5_2
  44. string target = proj.TargetGuidByName ("Unity-iPhone");
  45. #else
  46. string target = proj.TargetGuidByName (PBXProject.GetUnityTargetName ());
  47. #endif
  48. #if UNITY_2018_1_OR_NEWER
  49. #elif UNITY_2017_2_OR_NEWER
  50. string frameworkPath = "Frameworks/"+opencvForUnityFolderPath+"Plugins/iOS/opencv2.framework";
  51. string fileGuid = proj.FindFileGuidByProjectPath(frameworkPath);
  52. proj.AddFileToBuild(target, fileGuid);
  53. proj.AddFileToEmbedFrameworks(target, fileGuid);
  54. foreach (var configName in proj.BuildConfigNames()) {
  55. var configGuid = proj.BuildConfigByName(target, configName);
  56. proj.SetBuildPropertyForConfig(configGuid, "LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks");
  57. }
  58. #else
  59. UnityEngine.Debug.LogError ("If the version of Unity is less than 2017.2, you have to set opencv2.framework to Embedded Binaries manually.");
  60. #endif
  61. File.WriteAllText (projPath, proj.WriteToString ());
  62. #if UNITY_5_5_OR_NEWER
  63. if ((int)Convert.ToDecimal (PlayerSettings.iOS.targetOSVersionString) < 8) {
  64. #else
  65. if ((int)PlayerSettings.iOS.targetOSVersion < (int)iOSTargetOSVersion.iOS_8_0) {
  66. #endif
  67. UnityEngine.Debug.LogError ("Please set Target minimum iOS Version to 8.0 or higher.");
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// Removes the simulator architectures.
  73. /// </summary>
  74. /// <param name="WorkingDirectory">Working directory.</param>
  75. /// <param name="filePath">File path.</param>
  76. private static void RemoveSimulatorArchitectures (string WorkingDirectory, string filePath)
  77. {
  78. Process process = new Process ();
  79. process.StartInfo.FileName = "/bin/bash";
  80. process.StartInfo.WorkingDirectory = WorkingDirectory;
  81. process.StartInfo.Arguments = "-c \" ";
  82. process.StartInfo.Arguments += "lipo -remove i386 " + filePath + " -o " + filePath + ";";
  83. process.StartInfo.Arguments += "lipo -remove x86_64 " + filePath + " -o " + filePath + ";";
  84. process.StartInfo.Arguments += "lipo -info " + filePath + ";";
  85. process.StartInfo.Arguments += " \"";
  86. process.StartInfo.UseShellExecute = false;
  87. process.StartInfo.RedirectStandardOutput = true;
  88. process.StartInfo.RedirectStandardError = true;
  89. process.Start ();
  90. string output = process.StandardOutput.ReadToEnd ();
  91. string error = process.StandardError.ReadToEnd ();
  92. process.WaitForExit ();
  93. process.Close ();
  94. if (string.IsNullOrEmpty (error)) {
  95. UnityEngine.Debug.Log ("success : " + output);
  96. } else {
  97. UnityEngine.Debug.LogWarning ("error : " + error);
  98. }
  99. }
  100. }
  101. }
  102. #endif