OpenCVForUnityMenuItem.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #if UNITY_5 || UNITY_5_3_OR_NEWER
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using System.Collections.Generic;
  8. using System;
  9. namespace OpenCVForUnity
  10. {
  11. class OpenCVForUnityMenuItem : MonoBehaviour
  12. {
  13. /// <summary>
  14. /// Open OpenCV for Unity API Reference.
  15. /// </summary>
  16. [MenuItem ("Tools/OpenCV for Unity/Open OpenCV for Unity API Reference", false, 12)]
  17. public static void OpenOpenCVForUnityAPIReference ()
  18. {
  19. Application.OpenURL ("http://enoxsoftware.github.io/OpenCVForUnity/3.0.0/doc/html/index.html");
  20. }
  21. /// <summary>
  22. /// Open OpenCV C++ API Reference.
  23. /// </summary>
  24. [MenuItem ("Tools/OpenCV for Unity/Open OpenCV C++ API Reference", false, 13)]
  25. public static void OpenOpenCVAPIReference ()
  26. {
  27. Application.OpenURL ("http://docs.opencv.org/3.3.0/index.html");
  28. }
  29. /// <summary>
  30. /// Sets the plugin import settings.
  31. /// </summary>
  32. [MenuItem ("Tools/OpenCV for Unity/Set Plugin Import Settings", false, 1)]
  33. public static void SetPluginImportSettings ()
  34. {
  35. string[] guids = UnityEditor.AssetDatabase.FindAssets ("OpenCVForUnityMenuItem");
  36. if (guids.Length == 0) {
  37. Debug.LogWarning ("SetPluginImportSettings Faild : OpenCVForUnityMenuItem.cs is missing.");
  38. return;
  39. }
  40. string opencvForUnityFolderPath = AssetDatabase.GUIDToAssetPath (guids [0]).Substring (0, AssetDatabase.GUIDToAssetPath (guids [0]).LastIndexOf ("Editor/OpenCVForUnityMenuItem.cs"));
  41. string pluginsFolderPath = opencvForUnityFolderPath + "Plugins";
  42. // Debug.Log ("pluginsFolderPath " + pluginsFolderPath);
  43. string extraFolderPath = opencvForUnityFolderPath + "Extra";
  44. // Debug.Log ("extraFolderPath " + extraFolderPath);
  45. //Disable Extra folder
  46. SetPlugins (GetPluginFilePaths (extraFolderPath + "/exclude_contrib/Android/libs/armeabi-v7a"), null, null);
  47. SetPlugins (GetPluginFilePaths (extraFolderPath + "/exclude_contrib/Android/libs/x86"), null, null);
  48. SetPlugins (GetPluginFilePaths (extraFolderPath + "/exclude_contrib/Android/libs/arm64-v8a"), null, null);
  49. SetPlugins (new string[] { extraFolderPath + "/exclude_contrib/iOS/opencv2.framework" }, null, null);
  50. SetPlugins (GetPluginFilePaths (extraFolderPath + "/exclude_contrib/iOS"), null, null);
  51. //Android
  52. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/Android/libs/armeabi-v7a"), null,
  53. new Dictionary<BuildTarget, Dictionary<string, string>> () { {BuildTarget.Android,new Dictionary<string, string> () { {
  54. "CPU",
  55. "ARMv7"
  56. }
  57. }
  58. }
  59. });
  60. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/Android/libs/x86"), null,
  61. new Dictionary<BuildTarget, Dictionary<string, string>> () { {BuildTarget.Android,new Dictionary<string, string> () { {
  62. "CPU",
  63. "x86"
  64. }
  65. }
  66. }
  67. });
  68. #if UNITY_2018_1_OR_NEWER
  69. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/Android/libs/arm64-v8a"), null,
  70. new Dictionary<BuildTarget, Dictionary<string, string>> () { {BuildTarget.Android,new Dictionary<string, string> () { {
  71. "CPU",
  72. "ARM64"
  73. }
  74. }
  75. }
  76. });
  77. #else
  78. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/Android/libs/arm64-v8a"), null, null);
  79. #endif
  80. //iOS
  81. SetPlugins (new string[] { pluginsFolderPath + "/iOS/opencv2.framework" }, null,
  82. new Dictionary<BuildTarget, Dictionary<string, string>> () { {
  83. BuildTarget.iOS,
  84. new Dictionary<string, string> () { {
  85. "AddToEmbeddedBinaries",
  86. "true"
  87. }
  88. }
  89. }
  90. });
  91. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/iOS"), null,
  92. new Dictionary<BuildTarget, Dictionary<string, string>> () { {
  93. BuildTarget.iOS,
  94. null
  95. }
  96. });
  97. //OSX
  98. SetPlugins (new string[] { pluginsFolderPath + "/macOS/opencvforunity.bundle" }, new Dictionary<string, string> () { {
  99. "CPU",
  100. "AnyCPU"
  101. }, {
  102. "OS",
  103. "OSX"
  104. }
  105. },
  106. new Dictionary<BuildTarget, Dictionary<string, string>> () {
  107. #if UNITY_2017_3_OR_NEWER
  108. {
  109. BuildTarget.StandaloneOSX,new Dictionary<string, string> () { {
  110. "CPU",
  111. "AnyCPU"
  112. }
  113. }
  114. }
  115. #else
  116. {
  117. BuildTarget.StandaloneOSXIntel,new Dictionary<string, string> () { {
  118. "CPU",
  119. "x86"
  120. }
  121. }
  122. }, {
  123. BuildTarget.StandaloneOSXIntel64,new Dictionary<string, string> () { {
  124. "CPU",
  125. "x86_64"
  126. }
  127. }
  128. }, {
  129. BuildTarget.StandaloneOSXUniversal,new Dictionary<string, string> () { {
  130. "CPU",
  131. "AnyCPU"
  132. }
  133. }
  134. }
  135. #endif
  136. });
  137. //Windows
  138. SetPlugins (new string[] { pluginsFolderPath + "/Windows/x86/opencvforunity.dll" }, new Dictionary<string, string> () { {
  139. "CPU",
  140. "x86"
  141. }, {
  142. "OS",
  143. "Windows"
  144. }
  145. },
  146. new Dictionary<BuildTarget, Dictionary<string, string>> () { {BuildTarget.StandaloneWindows,new Dictionary<string, string> () { {
  147. "CPU",
  148. "x86"
  149. }
  150. }
  151. }
  152. });
  153. SetPlugins (new string[] { pluginsFolderPath + "/Windows/x86_64/opencvforunity.dll" }, new Dictionary<string, string> () { {
  154. "CPU",
  155. "x86_64"
  156. }, {
  157. "OS",
  158. "Windows"
  159. }
  160. },
  161. new Dictionary<BuildTarget, Dictionary<string, string>> () { {BuildTarget.StandaloneWindows64,new Dictionary<string, string> () { {
  162. "CPU",
  163. "x86_64"
  164. }
  165. }
  166. }
  167. });
  168. //Linux
  169. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/Linux/x86"), new Dictionary<string, string> () { {
  170. "CPU",
  171. "x86"
  172. }, {
  173. "OS",
  174. "Linux"
  175. }
  176. },
  177. new Dictionary<BuildTarget, Dictionary<string, string>> () { {BuildTarget.StandaloneLinux,new Dictionary<string, string> () { {
  178. "CPU",
  179. "x86"
  180. }
  181. }
  182. },
  183. });
  184. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/Linux/x86_64"), new Dictionary<string, string> () { {
  185. "CPU",
  186. "x86_64"
  187. }, {
  188. "OS",
  189. "Linux"
  190. }
  191. },
  192. new Dictionary<BuildTarget, Dictionary<string, string>> () { {BuildTarget.StandaloneLinux64,new Dictionary<string, string> () { {
  193. "CPU",
  194. "x86_64"
  195. }
  196. }
  197. },
  198. });
  199. //UWP
  200. #if UNITY_5_0 || UNITY_5_1
  201. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/WSA/UWP/ARM"), null, null);
  202. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/WSA/UWP/x64"), null, null);
  203. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/WSA/UWP/x86"), null, null);
  204. #else
  205. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/WSA/UWP/ARM"), null,
  206. new Dictionary<BuildTarget, Dictionary<string, string>> () { {BuildTarget.WSAPlayer,new Dictionary<string, string> () { {
  207. "SDK",
  208. "UWP"
  209. }, {
  210. "CPU",
  211. "ARM"
  212. }
  213. }
  214. }
  215. });
  216. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/WSA/UWP/x64"), null,
  217. new Dictionary<BuildTarget, Dictionary<string, string>> () { {BuildTarget.WSAPlayer,new Dictionary<string, string> () { {
  218. "SDK",
  219. "UWP"
  220. }, {
  221. "CPU",
  222. "x64"
  223. }
  224. }
  225. }
  226. });
  227. SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/WSA/UWP/x86"), null,
  228. new Dictionary<BuildTarget, Dictionary<string, string>> () { {BuildTarget.WSAPlayer,new Dictionary<string, string> () { {
  229. "SDK",
  230. "UWP"
  231. }, {
  232. "CPU",
  233. "x86"
  234. }
  235. }
  236. }
  237. });
  238. #endif
  239. //WebGL
  240. #if UNITY_2018_2_OR_NEWER
  241. SetPlugins (new string[] { pluginsFolderPath + "/WebGL/2018.2/opencvforunity.bc" }, null, new Dictionary<BuildTarget, Dictionary<string, string>> () { {
  242. BuildTarget.WebGL,
  243. null
  244. }
  245. });
  246. SetPlugins (new string[] { pluginsFolderPath + "/WebGL/5.6/opencvforunity.bc" }, null, null);
  247. #elif UNITY_5_6_OR_NEWER
  248. SetPlugins (new string[] { pluginsFolderPath + "/WebGL/2018.2/opencvforunity.bc" }, null, null);
  249. SetPlugins (new string[] { pluginsFolderPath + "/WebGL/5.6/opencvforunity.bc" }, null, new Dictionary<BuildTarget, Dictionary<string, string>> () { {
  250. BuildTarget.WebGL,
  251. null
  252. }
  253. });
  254. #else
  255. SetPlugins (new string[] { pluginsFolderPath + "/WebGL/2018.2/opencvforunity.bc" }, null, null);
  256. SetPlugins (new string[] { pluginsFolderPath + "/WebGL/5.6/opencvforunity.bc" }, null, null);
  257. #endif
  258. }
  259. /// <summary>
  260. /// Gets the plugin file paths.
  261. /// </summary>
  262. /// <returns>The plugin file paths.</returns>
  263. /// <param name="folderPath">Folder path.</param>
  264. static string[] GetPluginFilePaths (string folderPath)
  265. {
  266. Regex reg = new Regex (".meta$|.DS_Store$|.zip");
  267. try {
  268. return Directory.GetFiles (folderPath).Where (f => !reg.IsMatch (f)).ToArray ();
  269. } catch (Exception ex) {
  270. Debug.LogWarning ("SetPluginImportSettings Faild :" + ex);
  271. return null;
  272. }
  273. }
  274. /// <summary>
  275. /// Sets the plugins.
  276. /// </summary>
  277. /// <param name="files">Files.</param>
  278. /// <param name="editorSettings">Editor settings.</param>
  279. /// <param name="settings">Settings.</param>
  280. public static void SetPlugins (string[] files, Dictionary<string, string> editorSettings, Dictionary<BuildTarget, Dictionary<string, string>> settings)
  281. {
  282. if (files == null)
  283. return;
  284. foreach (string item in files) {
  285. PluginImporter pluginImporter = PluginImporter.GetAtPath (item) as PluginImporter;
  286. if (pluginImporter != null) {
  287. pluginImporter.SetCompatibleWithAnyPlatform (false);
  288. pluginImporter.SetCompatibleWithEditor (false);
  289. pluginImporter.SetCompatibleWithPlatform (BuildTarget.Android, false);
  290. pluginImporter.SetCompatibleWithPlatform (BuildTarget.iOS, false);
  291. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneWindows, false);
  292. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneWindows64, false);
  293. #if UNITY_2017_3_OR_NEWER
  294. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSX, false);
  295. #else
  296. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSXIntel, false);
  297. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSXIntel64, false);
  298. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSXUniversal, false);
  299. #endif
  300. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneLinux, false);
  301. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneLinux64, false);
  302. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneLinuxUniversal, false);
  303. pluginImporter.SetCompatibleWithPlatform (BuildTarget.WSAPlayer, false);
  304. pluginImporter.SetCompatibleWithPlatform (BuildTarget.WebGL, false);
  305. if (editorSettings != null) {
  306. pluginImporter.SetCompatibleWithEditor (true);
  307. foreach (KeyValuePair<string, string> pair in editorSettings) {
  308. if (pluginImporter.GetEditorData (pair.Key) != pair.Value) {
  309. pluginImporter.SetEditorData (pair.Key, pair.Value);
  310. }
  311. }
  312. }
  313. if (settings != null) {
  314. foreach (KeyValuePair<BuildTarget, Dictionary<string, string>> settingPair in settings) {
  315. pluginImporter.SetCompatibleWithPlatform (settingPair.Key, true);
  316. if (settingPair.Value != null) {
  317. foreach (KeyValuePair<string, string> pair in settingPair.Value) {
  318. if (pluginImporter.GetPlatformData (settingPair.Key, pair.Key) != pair.Value) {
  319. pluginImporter.SetPlatformData (settingPair.Key, pair.Key, pair.Value);
  320. }
  321. }
  322. }
  323. }
  324. } else {
  325. pluginImporter.SetCompatibleWithPlatform (BuildTarget.Android, false);
  326. pluginImporter.SetCompatibleWithPlatform (BuildTarget.iOS, false);
  327. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneWindows, false);
  328. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneWindows64, false);
  329. #if UNITY_2017_3_OR_NEWER
  330. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSX, false);
  331. #else
  332. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSXIntel, false);
  333. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSXIntel64, false);
  334. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSXUniversal, false);
  335. #endif
  336. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneLinux, false);
  337. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneLinux64, false);
  338. pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneLinuxUniversal, false);
  339. pluginImporter.SetCompatibleWithPlatform (BuildTarget.WSAPlayer, false);
  340. pluginImporter.SetCompatibleWithPlatform (BuildTarget.WebGL, false);
  341. }
  342. pluginImporter.SaveAndReimport ();
  343. Debug.Log ("SetPluginImportSettings Success :" + item);
  344. } else {
  345. Debug.LogWarning ("SetPluginImportSettings Faild :" + item);
  346. }
  347. }
  348. }
  349. }
  350. }
  351. #endif