OpenCVForUnityMenuItem.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. #if UNITY_2021_2_OR_NEWER
  10. using UnityEditor.Build;
  11. #endif
  12. namespace OpenCVForUnity.Editor
  13. {
  14. public class OpenCVForUnityMenuItem : MonoBehaviour
  15. {
  16. static int pluginFileIndex = 0;
  17. static int pluginFileCount = 0;
  18. static int GetPluginFileCount(string folderPath)
  19. {
  20. int pluginFileCount = 0;
  21. Regex reg = new Regex(".meta$|.DS_Store$|.zip$|.bundle|.framework");
  22. try
  23. {
  24. pluginFileCount += Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories).Where(f => !reg.IsMatch(f)).Count();
  25. }
  26. catch (Exception)
  27. {
  28. //Debug.LogWarning("SetPluginImportSettings Faild :" + ex);
  29. return 0;
  30. }
  31. pluginFileCount += Directory.GetDirectories(folderPath, "*.bundle", SearchOption.AllDirectories).Count();
  32. pluginFileCount += Directory.GetDirectories(folderPath, "*.framework", SearchOption.AllDirectories).Count();
  33. return pluginFileCount;
  34. }
  35. static string[] GetPluginFilePaths(string folderPath)
  36. {
  37. Regex reg = new Regex(".meta$|.DS_Store$|.zip");
  38. try
  39. {
  40. return Directory.GetFiles(folderPath).Where(f => !reg.IsMatch(f)).Select(n => n.Replace("\\", "/")).ToArray();
  41. }
  42. catch (Exception)
  43. {
  44. //Debug.LogWarning("SetPluginImportSettings Faild :" + ex);
  45. return null;
  46. }
  47. }
  48. public static void SetPluginImportSettings()
  49. {
  50. try
  51. {
  52. string[] guids = UnityEditor.AssetDatabase.FindAssets("OpenCVForUnityMenuItem");
  53. if (guids.Length == 0)
  54. {
  55. Debug.LogWarning("SetPluginImportSettings Faild : OpenCVForUnityMenuItem.cs is missing.");
  56. return;
  57. }
  58. string opencvForUnityFolderPath = AssetDatabase.GUIDToAssetPath(guids[0]).Substring(0, AssetDatabase.GUIDToAssetPath(guids[0]).LastIndexOf("/Editor/OpenCVForUnityMenuItem.cs"));
  59. string pluginsFolderPath = opencvForUnityFolderPath + "/Plugins";
  60. //Debug.Log("pluginsFolderPath " + pluginsFolderPath);
  61. string extraFolderPath = opencvForUnityFolderPath + "/Extra";
  62. //Debug.Log("extraFolderPath " + extraFolderPath);
  63. pluginFileIndex = 0;
  64. pluginFileCount = GetPluginFileCount(pluginsFolderPath) + GetPluginFileCount(extraFolderPath);
  65. //Android
  66. SetAndroidPluginImportSettings(pluginsFolderPath, extraFolderPath, true);
  67. //iOS
  68. SetIOSPluginImportSettings(pluginsFolderPath, extraFolderPath, true);
  69. //OSX
  70. SetOSXPluginImportSettings(pluginsFolderPath, extraFolderPath, true);
  71. //Windows
  72. SetWindowsPluginImportSettings(pluginsFolderPath, extraFolderPath, true);
  73. //Linux
  74. SetLinuxPluginImportSettings(pluginsFolderPath, extraFolderPath, true);
  75. //UWP
  76. SetUWPPluginImportSettings(pluginsFolderPath, extraFolderPath, true);
  77. //WebGL
  78. SetWebGLPluginImportSettings(pluginsFolderPath, extraFolderPath, true);
  79. }
  80. catch (Exception e)
  81. {
  82. Debug.Log("SetPluginImportSettings() " + e);
  83. }
  84. finally
  85. {
  86. EditorUtility.ClearProgressBar();
  87. }
  88. //Debug.Log("pluginFilesIndex " + pluginFileIndex);
  89. //Debug.Log("pluginFilesCount " + pluginFileCount);
  90. }
  91. public static void SetAndroidPluginImportSettings(string pluginsFolderPath, string extraFolderPath, bool displayProgressBar = false)
  92. {
  93. SetPlugins(GetPluginFilePaths(extraFolderPath + "/exclude_contrib/Android/libs/armeabi-v7a"), null, null, displayProgressBar);
  94. SetPlugins(GetPluginFilePaths(extraFolderPath + "/exclude_contrib/Android/libs/arm64-v8a"), null, null, displayProgressBar);
  95. SetPlugins(GetPluginFilePaths(extraFolderPath + "/exclude_contrib/Android/libs/x86"), null, null, displayProgressBar);
  96. SetPlugins(GetPluginFilePaths(extraFolderPath + "/exclude_contrib/Android/libs/x86_64"), null, null, displayProgressBar);
  97. SetPlugins(GetPluginFilePaths(extraFolderPath + "/dll_version/Android/libs/armeabi-v7a"), null, null, displayProgressBar);
  98. SetPlugins(GetPluginFilePaths(extraFolderPath + "/dll_version/Android/libs/arm64-v8a"), null, null, displayProgressBar);
  99. SetPlugins(GetPluginFilePaths(extraFolderPath + "/dll_version/Android/libs/x86"), null, null, displayProgressBar);
  100. SetPlugins(GetPluginFilePaths(extraFolderPath + "/dll_version/Android/libs/x86_64"), null, null, displayProgressBar);
  101. SetPlugins(GetPluginFilePaths(extraFolderPath + "/dll_version/Android/exclude_contrib/libs/armeabi-v7a"), null, null, displayProgressBar);
  102. SetPlugins(GetPluginFilePaths(extraFolderPath + "/dll_version/Android/exclude_contrib/libs/arm64-v8a"), null, null, displayProgressBar);
  103. SetPlugins(GetPluginFilePaths(extraFolderPath + "/dll_version/Android/exclude_contrib/libs/x86"), null, null, displayProgressBar);
  104. SetPlugins(GetPluginFilePaths(extraFolderPath + "/dll_version/Android/exclude_contrib/libs/x86_64"), null, null, displayProgressBar);
  105. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/Android/libs/armeabi-v7a"), null,
  106. new Dictionary<BuildTarget, Dictionary<string, string>>() { {BuildTarget.Android,new Dictionary<string, string> () { {
  107. "CPU",
  108. "ARMv7"
  109. }
  110. }
  111. }
  112. }, displayProgressBar);
  113. #if UNITY_2018_1_OR_NEWER
  114. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/Android/libs/arm64-v8a"), null,
  115. new Dictionary<BuildTarget, Dictionary<string, string>>() { {BuildTarget.Android,new Dictionary<string, string> () { {
  116. "CPU",
  117. "ARM64"
  118. }
  119. }
  120. }
  121. }, displayProgressBar);
  122. #else
  123. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/Android/libs/arm64-v8a"), null, null, displayProgressBar);
  124. #endif
  125. #if UNITY_2021_2_OR_NEWER
  126. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/Android/libs/x86"), null,
  127. new Dictionary<BuildTarget, Dictionary<string, string>>() { {BuildTarget.Android,new Dictionary<string, string> () { {
  128. "CPU",
  129. "x86"
  130. }
  131. }
  132. }
  133. }, displayProgressBar);
  134. #else
  135. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/Android/libs/x86"), null, null, displayProgressBar);
  136. #endif
  137. #if UNITY_2021_2_OR_NEWER
  138. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/Android/libs/x86_64"), null,
  139. new Dictionary<BuildTarget, Dictionary<string, string>>() { {BuildTarget.Android,new Dictionary<string, string> () { {
  140. "CPU",
  141. "x86_64"
  142. }
  143. }
  144. }
  145. }, displayProgressBar);
  146. #else
  147. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/Android/libs/x86_64"), null, null, displayProgressBar);
  148. #endif
  149. }
  150. public static void SetIOSPluginImportSettings(string pluginsFolderPath, string extraFolderPath, bool displayProgressBar = false)
  151. {
  152. SetPlugins(new string[] { extraFolderPath + "/exclude_contrib/iOS/opencv2.framework" }, null, null, displayProgressBar);
  153. SetPlugins(GetPluginFilePaths(extraFolderPath + "/exclude_contrib/iOS"), null, null, displayProgressBar);
  154. SetPlugins(new string[] { pluginsFolderPath + "/iOS/opencv2.framework" }, null,
  155. new Dictionary<BuildTarget, Dictionary<string, string>>() { {
  156. BuildTarget.iOS,
  157. new Dictionary<string, string> () { {
  158. "AddToEmbeddedBinaries",
  159. "true"
  160. }
  161. }
  162. }
  163. }, displayProgressBar);
  164. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/iOS"), null,
  165. new Dictionary<BuildTarget, Dictionary<string, string>>() { {
  166. BuildTarget.iOS,
  167. null
  168. }
  169. }, displayProgressBar);
  170. }
  171. public static void SetOSXPluginImportSettings(string pluginsFolderPath, string extraFolderPath, bool displayProgressBar = false)
  172. {
  173. SetPlugins(new string[] { pluginsFolderPath + "/macOS/opencvforunity.bundle" }, new Dictionary<string, string>() { {
  174. "CPU",
  175. "AnyCPU"
  176. }, {
  177. "OS",
  178. "OSX"
  179. }
  180. },
  181. new Dictionary<BuildTarget, Dictionary<string, string>>() {
  182. #if UNITY_2017_3_OR_NEWER
  183. {
  184. BuildTarget.StandaloneOSX,new Dictionary<string, string> () { {
  185. "CPU",
  186. "AnyCPU"
  187. }
  188. }
  189. }
  190. #else
  191. {
  192. BuildTarget.StandaloneOSXIntel,new Dictionary<string, string> () { {
  193. "CPU",
  194. "x86"
  195. }
  196. }
  197. }, {
  198. BuildTarget.StandaloneOSXIntel64,new Dictionary<string, string> () { {
  199. "CPU",
  200. "x86_64"
  201. }
  202. }
  203. }, {
  204. BuildTarget.StandaloneOSXUniversal,new Dictionary<string, string> () { {
  205. "CPU",
  206. "AnyCPU"
  207. }
  208. }
  209. }
  210. #endif
  211. }, displayProgressBar);
  212. }
  213. public static void SetWindowsPluginImportSettings(string pluginsFolderPath, string extraFolderPath, bool displayProgressBar = false)
  214. {
  215. SetPlugins(new string[] { extraFolderPath + "/dll_version/Windows/x86/opencvforunity.dll" }, null, null, displayProgressBar);
  216. SetPlugins(new string[] { extraFolderPath + "/dll_version/Windows/x86_64/opencvforunity.dll" }, null, null, displayProgressBar);
  217. SetPlugins(new string[] { extraFolderPath + "/dll_version/Windows/ARM64/opencvforunity.dll" }, null, null, displayProgressBar);
  218. SetPlugins(new string[] { pluginsFolderPath + "/Windows/x86/opencvforunity.dll" }, new Dictionary<string, string>() { {
  219. "CPU",
  220. "x86"
  221. }, {
  222. "OS",
  223. "Windows"
  224. }
  225. },
  226. new Dictionary<BuildTarget, Dictionary<string, string>>() { {BuildTarget.StandaloneWindows,new Dictionary<string, string> () { {
  227. "CPU",
  228. "x86"
  229. }
  230. }
  231. }
  232. }, displayProgressBar);
  233. SetPlugins(new string[] { pluginsFolderPath + "/Windows/x86_64/opencvforunity.dll" }, new Dictionary<string, string>() { {
  234. "CPU",
  235. "x86_64"
  236. }, {
  237. "OS",
  238. "Windows"
  239. }
  240. },
  241. new Dictionary<BuildTarget, Dictionary<string, string>>() { {BuildTarget.StandaloneWindows64,new Dictionary<string, string> () { {
  242. "CPU",
  243. "x86_64"
  244. }
  245. }
  246. }
  247. }, displayProgressBar);
  248. #if UNITY_2023_1_OR_NEWER
  249. SetPlugins(new string[] { pluginsFolderPath + "/Windows/ARM64/opencvforunity.dll" }, new Dictionary<string, string>() { {
  250. "CPU",
  251. "ARM64"
  252. }, {
  253. "OS",
  254. "Windows"
  255. }
  256. },
  257. new Dictionary<BuildTarget, Dictionary<string, string>>() { {BuildTarget.StandaloneWindows64,new Dictionary<string, string> () { {
  258. "CPU",
  259. "ARM64"
  260. }
  261. }
  262. }
  263. }, displayProgressBar);
  264. #endif
  265. }
  266. public static void SetLinuxPluginImportSettings(string pluginsFolderPath, string extraFolderPath, bool displayProgressBar = false)
  267. {
  268. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/Linux/x86_64"), new Dictionary<string, string>() { {
  269. "CPU",
  270. "x86_64"
  271. }, {
  272. "OS",
  273. "Linux"
  274. }
  275. },
  276. new Dictionary<BuildTarget, Dictionary<string, string>>() { {BuildTarget.StandaloneLinux64,new Dictionary<string, string> () { {
  277. "CPU",
  278. "x86_64"
  279. }
  280. }
  281. },
  282. }, displayProgressBar);
  283. }
  284. public static void SetUWPPluginImportSettings(string pluginsFolderPath, string extraFolderPath, bool displayProgressBar = false)
  285. {
  286. #if UNITY_5_0 || UNITY_5_1
  287. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/WSA/UWP/ARM"), null, null, displayProgressBar);
  288. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/WSA/UWP/ARM64"), null, null, displayProgressBar);
  289. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/WSA/UWP/x64"), null, null, displayProgressBar);
  290. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/WSA/UWP/x86"), null, null, displayProgressBar);
  291. #else
  292. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/WSA/UWP/ARM"), null,
  293. new Dictionary<BuildTarget, Dictionary<string, string>>() { {BuildTarget.WSAPlayer,new Dictionary<string, string> () { {
  294. "SDK",
  295. "UWP"
  296. }, {
  297. "CPU",
  298. "ARM"
  299. }
  300. }
  301. }
  302. }, displayProgressBar);
  303. #if UNITY_2019_1_OR_NEWER
  304. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/WSA/UWP/ARM64"), null,
  305. new Dictionary<BuildTarget, Dictionary<string, string>>() { {BuildTarget.WSAPlayer,new Dictionary<string, string> () { {
  306. "SDK",
  307. "UWP"
  308. }, {
  309. "CPU",
  310. "ARM64"
  311. }
  312. }
  313. }
  314. }, displayProgressBar);
  315. #else
  316. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/WSA/UWP/ARM64"), null, null, displayProgressBar);
  317. #endif
  318. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/WSA/UWP/x64"), null,
  319. new Dictionary<BuildTarget, Dictionary<string, string>>() { {BuildTarget.WSAPlayer,new Dictionary<string, string> () { {
  320. "SDK",
  321. "UWP"
  322. }, {
  323. "CPU",
  324. "x64"
  325. }
  326. }
  327. }
  328. }, displayProgressBar);
  329. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/WSA/UWP/x86"), null,
  330. new Dictionary<BuildTarget, Dictionary<string, string>>() { {BuildTarget.WSAPlayer,new Dictionary<string, string> () { {
  331. "SDK",
  332. "UWP"
  333. }, {
  334. "CPU",
  335. "x86"
  336. }
  337. }
  338. }
  339. }, displayProgressBar);
  340. #endif
  341. }
  342. public static void SetWebGLPluginImportSettings(string pluginsFolderPath, string extraFolderPath, bool displayProgressBar = false)
  343. {
  344. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/WebGL/2021.2"), null, null, displayProgressBar);
  345. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/WebGL/2022.2"), null, null, displayProgressBar);
  346. SetPlugins(GetPluginFilePaths(pluginsFolderPath + "/WebGL/2023.2"), null, null, displayProgressBar);
  347. string version = "2021.2";
  348. #if UNITY_2023_2_OR_NEWER
  349. version = "2023.2";
  350. #elif UNITY_2022_2_OR_NEWER
  351. version = "2022.2";
  352. #elif UNITY_2021_2_OR_NEWER
  353. version = "2021.2";
  354. #endif
  355. #if UNITY_2021_2_OR_NEWER
  356. bool threads = ValidateUseWebGLThreadsSupport();
  357. bool simd = ValidateUseWebGLSIMDSupport();
  358. if(threads && simd)
  359. {
  360. SetPlugins(new string[] { pluginsFolderPath + "/WebGL/" + version + "/opencvforunity_threads_simd.bc" }, null, new Dictionary<BuildTarget, Dictionary<string, string>>() { {
  361. BuildTarget.WebGL,
  362. null
  363. }
  364. }, displayProgressBar);
  365. }else if (threads)
  366. {
  367. SetPlugins(new string[] { pluginsFolderPath + "/WebGL/" + version + "/opencvforunity_threads.bc" }, null, new Dictionary<BuildTarget, Dictionary<string, string>>() { {
  368. BuildTarget.WebGL,
  369. null
  370. }
  371. }, displayProgressBar);
  372. }
  373. else
  374. {
  375. SetPlugins(new string[] { pluginsFolderPath + "/WebGL/" + version + "/opencvforunity.bc" }, null, new Dictionary<BuildTarget, Dictionary<string, string>>() { {
  376. BuildTarget.WebGL,
  377. null
  378. }
  379. }, displayProgressBar);
  380. }
  381. #else
  382. Debug.LogError("The WebGL platform build of OpenCVForUnity is not supported in versions of Unity under " + version + ".");
  383. #endif
  384. if (PlayerSettings.WebGL.exceptionSupport == WebGLExceptionSupport.None)
  385. PlayerSettings.WebGL.exceptionSupport = WebGLExceptionSupport.ExplicitlyThrownExceptionsOnly;
  386. }
  387. private static void SetPlugins(string[] files, Dictionary<string, string> editorSettings, Dictionary<BuildTarget, Dictionary<string, string>> settings, bool displayProgressBar)
  388. {
  389. if (files == null)
  390. return;
  391. foreach (string item in files)
  392. {
  393. PluginImporter pluginImporter = PluginImporter.GetAtPath(item) as PluginImporter;
  394. if (pluginImporter != null)
  395. {
  396. //if(displayProgressBar) EditorUtility.DisplayProgressBar("Set Plugin Import Settings", item, 1);
  397. if (displayProgressBar)
  398. {
  399. EditorUtility.DisplayProgressBar("Set Plugin Import Settings", string.Format("{0} / {1} " + item, pluginFileIndex + 1, pluginFileCount), (float)pluginFileIndex / pluginFileCount);
  400. pluginFileIndex++;
  401. }
  402. pluginImporter.SetCompatibleWithAnyPlatform(false);
  403. pluginImporter.SetCompatibleWithEditor(false);
  404. pluginImporter.SetCompatibleWithPlatform(BuildTarget.Android, false);
  405. pluginImporter.SetCompatibleWithPlatform(BuildTarget.iOS, false);
  406. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, false);
  407. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows64, false);
  408. #if UNITY_2017_3_OR_NEWER
  409. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneOSX, false);
  410. #else
  411. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel, false);
  412. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel64, false);
  413. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXUniversal, false);
  414. #endif
  415. #if UNITY_2019_2_OR_NEWER
  416. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneLinux64, false);
  417. #else
  418. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneLinux, false);
  419. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneLinux64, false);
  420. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneLinuxUniversal, false);
  421. #endif
  422. pluginImporter.SetCompatibleWithPlatform(BuildTarget.WSAPlayer, false);
  423. pluginImporter.SetCompatibleWithPlatform(BuildTarget.WebGL, false);
  424. if (editorSettings != null)
  425. {
  426. pluginImporter.SetCompatibleWithEditor(true);
  427. foreach (KeyValuePair<string, string> pair in editorSettings)
  428. {
  429. if (pluginImporter.GetEditorData(pair.Key) != pair.Value)
  430. {
  431. pluginImporter.SetEditorData(pair.Key, pair.Value);
  432. }
  433. }
  434. }
  435. if (settings != null)
  436. {
  437. foreach (KeyValuePair<BuildTarget, Dictionary<string, string>> settingPair in settings)
  438. {
  439. pluginImporter.SetCompatibleWithPlatform(settingPair.Key, true);
  440. if (settingPair.Value != null)
  441. {
  442. foreach (KeyValuePair<string, string> pair in settingPair.Value)
  443. {
  444. if (pluginImporter.GetPlatformData(settingPair.Key, pair.Key) != pair.Value)
  445. {
  446. pluginImporter.SetPlatformData(settingPair.Key, pair.Key, pair.Value);
  447. }
  448. }
  449. }
  450. }
  451. #if UNITY_2019_1_OR_NEWER
  452. pluginImporter.isPreloaded = true;
  453. #endif
  454. }
  455. else
  456. {
  457. pluginImporter.SetCompatibleWithPlatform(BuildTarget.Android, false);
  458. pluginImporter.SetCompatibleWithPlatform(BuildTarget.iOS, false);
  459. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows, false);
  460. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneWindows64, false);
  461. #if UNITY_2017_3_OR_NEWER
  462. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneOSX, false);
  463. #else
  464. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel, false);
  465. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXIntel64, false);
  466. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneOSXUniversal, false);
  467. #endif
  468. #if UNITY_2019_2_OR_NEWER
  469. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneLinux64, false);
  470. #else
  471. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneLinux, false);
  472. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneLinux64, false);
  473. pluginImporter.SetCompatibleWithPlatform(BuildTarget.StandaloneLinuxUniversal, false);
  474. #endif
  475. pluginImporter.SetCompatibleWithPlatform(BuildTarget.WSAPlayer, false);
  476. pluginImporter.SetCompatibleWithPlatform(BuildTarget.WebGL, false);
  477. #if UNITY_2019_1_OR_NEWER
  478. pluginImporter.isPreloaded = false;
  479. #endif
  480. }
  481. pluginImporter.SaveAndReimport();
  482. if (displayProgressBar) Debug.Log("SetPluginImportSettings Success :" + item);
  483. }
  484. else
  485. {
  486. //Debug.LogWarning("SetPluginImportSettings Faild :" + item);
  487. }
  488. }
  489. }
  490. #if UNITY_2018_1_OR_NEWER
  491. static readonly string SYMBOL_OPENCV_USE_UNSAFE_CODE = "OPENCV_USE_UNSAFE_CODE";
  492. //[MenuItem("Tools/OpenCV for Unity/Use Unsafe Code", validate = true, priority = 12)]
  493. public static bool ValidateUseUnsafeCode()
  494. {
  495. //Menu.SetChecked("Tools/OpenCV for Unity/Use Unsafe Code", EditorUserBuildSettings.activeScriptCompilationDefines.Contains(SYMBOL_OPENCV_USE_UNSAFE_CODE));
  496. //return true;
  497. return EditorUserBuildSettings.activeScriptCompilationDefines.Contains(SYMBOL_OPENCV_USE_UNSAFE_CODE);
  498. }
  499. //[MenuItem("Tools/OpenCV for Unity/Use Unsafe Code", validate = false, priority = 12)]
  500. public static void UseUnsafeCode(bool enable)
  501. {
  502. //if (Menu.GetChecked("Tools/OpenCV for Unity/Use Unsafe Code"))
  503. //{
  504. if (!enable) {
  505. if (EditorUtility.DisplayDialog("Disable Unsafe Code",
  506. "Do you want to disable Unsafe Code in OpenCV for Unity?", "Yes", "Cancel"))
  507. {
  508. #if UNITY_2021_2_OR_NEWER
  509. Symbol.Remove(NamedBuildTarget.Standalone, Symbol.GetCurrentSymbols(NamedBuildTarget.Standalone), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  510. Symbol.Remove(NamedBuildTarget.Android, Symbol.GetCurrentSymbols(NamedBuildTarget.Android), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  511. Symbol.Remove(NamedBuildTarget.iOS, Symbol.GetCurrentSymbols(NamedBuildTarget.iOS), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  512. Symbol.Remove(NamedBuildTarget.WebGL, Symbol.GetCurrentSymbols(NamedBuildTarget.WebGL), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  513. Symbol.Remove(NamedBuildTarget.WindowsStoreApps, Symbol.GetCurrentSymbols(NamedBuildTarget.WindowsStoreApps ), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  514. #else
  515. Symbol.Remove(BuildTargetGroup.Standalone, Symbol.GetCurrentSymbols(BuildTargetGroup.Standalone), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  516. Symbol.Remove(BuildTargetGroup.Android, Symbol.GetCurrentSymbols(BuildTargetGroup.Android), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  517. Symbol.Remove(BuildTargetGroup.iOS, Symbol.GetCurrentSymbols(BuildTargetGroup.iOS), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  518. Symbol.Remove(BuildTargetGroup.WebGL, Symbol.GetCurrentSymbols(BuildTargetGroup.WebGL), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  519. Symbol.Remove(BuildTargetGroup.WSA, Symbol.GetCurrentSymbols(BuildTargetGroup.WSA), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  520. #endif
  521. Debug.Log("\"" + SYMBOL_OPENCV_USE_UNSAFE_CODE + "\" has been removed from Scripting Define Symbols.");
  522. EditorUtility.DisplayDialog("success!!",
  523. "\"" + SYMBOL_OPENCV_USE_UNSAFE_CODE + "\" has been removed from Scripting Define Symbols.", "OK");
  524. }
  525. }
  526. else
  527. {
  528. if (EditorUtility.DisplayDialog("Enable Unsafe Code",
  529. "Do you want to enable Unsafe Code in OpenCV for Unity?", "Yes", "Cancel"))
  530. {
  531. #if UNITY_2021_2_OR_NEWER
  532. Symbol.Add(NamedBuildTarget.Standalone, Symbol.GetCurrentSymbols(NamedBuildTarget.Standalone), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  533. Symbol.Add(NamedBuildTarget.Android, Symbol.GetCurrentSymbols(NamedBuildTarget.Android), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  534. Symbol.Add(NamedBuildTarget.iOS, Symbol.GetCurrentSymbols(NamedBuildTarget.iOS), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  535. Symbol.Add(NamedBuildTarget.WebGL, Symbol.GetCurrentSymbols(NamedBuildTarget.WebGL), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  536. Symbol.Add(NamedBuildTarget.WindowsStoreApps, Symbol.GetCurrentSymbols(NamedBuildTarget.WindowsStoreApps), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  537. #else
  538. Symbol.Add(BuildTargetGroup.Standalone, Symbol.GetCurrentSymbols(BuildTargetGroup.Standalone), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  539. Symbol.Add(BuildTargetGroup.Android, Symbol.GetCurrentSymbols(BuildTargetGroup.Android), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  540. Symbol.Add(BuildTargetGroup.iOS, Symbol.GetCurrentSymbols(BuildTargetGroup.iOS), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  541. Symbol.Add(BuildTargetGroup.WebGL, Symbol.GetCurrentSymbols(BuildTargetGroup.WebGL), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  542. Symbol.Add(BuildTargetGroup.WSA, Symbol.GetCurrentSymbols(BuildTargetGroup.WSA), SYMBOL_OPENCV_USE_UNSAFE_CODE);
  543. #endif
  544. Debug.Log("\"" + SYMBOL_OPENCV_USE_UNSAFE_CODE + "\" has been added to Scripting Define Symbols.");
  545. EditorUtility.DisplayDialog("success!!",
  546. "\"" + SYMBOL_OPENCV_USE_UNSAFE_CODE + "\" has been added to Scripting Define Symbols.", "OK");
  547. }
  548. }
  549. }
  550. static class Symbol
  551. {
  552. #if UNITY_2021_2_OR_NEWER
  553. public static IEnumerable<string> GetCurrentSymbols(NamedBuildTarget nameBuildTarget)
  554. {
  555. return PlayerSettings.GetScriptingDefineSymbols(nameBuildTarget).Split(';');
  556. }
  557. private static void SaveSymbol(NamedBuildTarget nameBuildTarget, IEnumerable<string> currentSymbols)
  558. {
  559. var symbols = String.Join(";", currentSymbols.ToArray());
  560. PlayerSettings.SetScriptingDefineSymbols(nameBuildTarget, symbols);
  561. }
  562. public static void Add(NamedBuildTarget nameBuildTarget, IEnumerable<string> currentSymbols, params string[] symbols)
  563. {
  564. currentSymbols = currentSymbols.Except(symbols);
  565. currentSymbols = currentSymbols.Concat(symbols).Distinct();
  566. SaveSymbol(nameBuildTarget, currentSymbols);
  567. }
  568. public static void Remove(NamedBuildTarget nameBuildTarget, IEnumerable<string> currentSymbols, params string[] symbols)
  569. {
  570. currentSymbols = currentSymbols.Except(symbols);
  571. SaveSymbol(nameBuildTarget, currentSymbols);
  572. }
  573. public static void Set(NamedBuildTarget nameBuildTarget, IEnumerable<string> currentSymbols, params string[] symbols)
  574. {
  575. SaveSymbol(nameBuildTarget, symbols);
  576. }
  577. #else
  578. public static IEnumerable<string> GetCurrentSymbols(BuildTargetGroup buildTargetGroup)
  579. {
  580. return PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup).Split(';');
  581. }
  582. private static void SaveSymbol(BuildTargetGroup buildTargetGroup, IEnumerable<string> currentSymbols)
  583. {
  584. var symbols = String.Join(";", currentSymbols.ToArray());
  585. PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, symbols);
  586. }
  587. public static void Add(BuildTargetGroup buildTargetGroup, IEnumerable<string> currentSymbols, params string[] symbols)
  588. {
  589. currentSymbols = currentSymbols.Except(symbols);
  590. currentSymbols = currentSymbols.Concat(symbols).Distinct();
  591. SaveSymbol(buildTargetGroup, currentSymbols);
  592. }
  593. public static void Remove(BuildTargetGroup buildTargetGroup, IEnumerable<string> currentSymbols, params string[] symbols)
  594. {
  595. currentSymbols = currentSymbols.Except(symbols);
  596. SaveSymbol(buildTargetGroup, currentSymbols);
  597. }
  598. public static void Set(BuildTargetGroup buildTargetGroup, IEnumerable<string> currentSymbols, params string[] symbols)
  599. {
  600. SaveSymbol(buildTargetGroup, symbols);
  601. }
  602. #endif
  603. }
  604. #endif
  605. public static bool ValidateUseWebGLThreadsSupport()
  606. {
  607. return PlayerSettings.WebGL.threadsSupport;
  608. }
  609. public static void UseWebGLThreadsSupport(bool enable)
  610. {
  611. PlayerSettings.WebGL.threadsSupport = enable;
  612. }
  613. public static bool ValidateUseWebGLSIMDSupport()
  614. {
  615. return PlayerSettings.WebGL.emscriptenArgs.Contains("-msimd128");
  616. }
  617. public static void UseWebGLSIMDSupport(bool enable)
  618. {
  619. if (enable)
  620. {
  621. PlayerSettings.WebGL.emscriptenArgs += "-msimd128";
  622. }
  623. else
  624. {
  625. PlayerSettings.WebGL.emscriptenArgs = PlayerSettings.WebGL.emscriptenArgs.Replace("-msimd128","");
  626. }
  627. }
  628. //[MenuItem("Tools/OpenCV for Unity/Import Extra Package", false, 24)]
  629. public static void ImportExtraPackage()
  630. {
  631. AssetDatabase.ImportPackage("Assets/OpenCVForUnity/Extra.unitypackage", true);
  632. }
  633. [MenuItem("Tools/OpenCV for Unity/Open OpenCV for Unity API Reference", false, 35)]
  634. public static void OpenOpenCVForUnityAPIReference()
  635. {
  636. Application.OpenURL("http://enoxsoftware.github.io/OpenCVForUnity/3.0.0/doc/html/index.html");
  637. }
  638. [MenuItem("Tools/OpenCV for Unity/Open OpenCV C++ API Reference", false, 36)]
  639. public static void OpenOpenCVAPIReference()
  640. {
  641. Application.OpenURL("http://docs.opencv.org/4.8.0/index.html");
  642. }
  643. }
  644. }
  645. #endif