#if UNITY_5 || UNITY_5_3_OR_NEWER
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System;
namespace OpenCVForUnity
{
class OpenCVForUnityMenuItem : MonoBehaviour
{
///
/// Open OpenCV for Unity API Reference.
///
[MenuItem ("Tools/OpenCV for Unity/Open OpenCV for Unity API Reference", false, 12)]
public static void OpenOpenCVForUnityAPIReference ()
{
Application.OpenURL ("http://enoxsoftware.github.io/OpenCVForUnity/3.0.0/doc/html/index.html");
}
///
/// Open OpenCV C++ API Reference.
///
[MenuItem ("Tools/OpenCV for Unity/Open OpenCV C++ API Reference", false, 13)]
public static void OpenOpenCVAPIReference ()
{
Application.OpenURL ("http://docs.opencv.org/3.3.0/index.html");
}
///
/// Sets the plugin import settings.
///
[MenuItem ("Tools/OpenCV for Unity/Set Plugin Import Settings", false, 1)]
public static void SetPluginImportSettings ()
{
string[] guids = UnityEditor.AssetDatabase.FindAssets ("OpenCVForUnityMenuItem");
if (guids.Length == 0) {
Debug.LogWarning ("SetPluginImportSettings Faild : OpenCVForUnityMenuItem.cs is missing.");
return;
}
string opencvForUnityFolderPath = AssetDatabase.GUIDToAssetPath (guids [0]).Substring (0, AssetDatabase.GUIDToAssetPath (guids [0]).LastIndexOf ("Editor/OpenCVForUnityMenuItem.cs"));
string pluginsFolderPath = opencvForUnityFolderPath + "Plugins";
// Debug.Log ("pluginsFolderPath " + pluginsFolderPath);
string extraFolderPath = opencvForUnityFolderPath + "Extra";
// Debug.Log ("extraFolderPath " + extraFolderPath);
//Disable Extra folder
SetPlugins (GetPluginFilePaths (extraFolderPath + "/exclude_contrib/Android/libs/armeabi-v7a"), null, null);
SetPlugins (GetPluginFilePaths (extraFolderPath + "/exclude_contrib/Android/libs/x86"), null, null);
SetPlugins (GetPluginFilePaths (extraFolderPath + "/exclude_contrib/Android/libs/arm64-v8a"), null, null);
SetPlugins (new string[] { extraFolderPath + "/exclude_contrib/iOS/opencv2.framework" }, null, null);
SetPlugins (GetPluginFilePaths (extraFolderPath + "/exclude_contrib/iOS"), null, null);
//Android
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/Android/libs/armeabi-v7a"), null,
new Dictionary> () { {BuildTarget.Android,new Dictionary () { {
"CPU",
"ARMv7"
}
}
}
});
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/Android/libs/x86"), null,
new Dictionary> () { {BuildTarget.Android,new Dictionary () { {
"CPU",
"x86"
}
}
}
});
#if UNITY_2018_1_OR_NEWER
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/Android/libs/arm64-v8a"), null,
new Dictionary> () { {BuildTarget.Android,new Dictionary () { {
"CPU",
"ARM64"
}
}
}
});
#else
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/Android/libs/arm64-v8a"), null, null);
#endif
//iOS
SetPlugins (new string[] { pluginsFolderPath + "/iOS/opencv2.framework" }, null,
new Dictionary> () { {
BuildTarget.iOS,
new Dictionary () { {
"AddToEmbeddedBinaries",
"true"
}
}
}
});
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/iOS"), null,
new Dictionary> () { {
BuildTarget.iOS,
null
}
});
//OSX
SetPlugins (new string[] { pluginsFolderPath + "/macOS/opencvforunity.bundle" }, new Dictionary () { {
"CPU",
"AnyCPU"
}, {
"OS",
"OSX"
}
},
new Dictionary> () {
#if UNITY_2017_3_OR_NEWER
{
BuildTarget.StandaloneOSX,new Dictionary () { {
"CPU",
"AnyCPU"
}
}
}
#else
{
BuildTarget.StandaloneOSXIntel,new Dictionary () { {
"CPU",
"x86"
}
}
}, {
BuildTarget.StandaloneOSXIntel64,new Dictionary () { {
"CPU",
"x86_64"
}
}
}, {
BuildTarget.StandaloneOSXUniversal,new Dictionary () { {
"CPU",
"AnyCPU"
}
}
}
#endif
});
//Windows
SetPlugins (new string[] { pluginsFolderPath + "/Windows/x86/opencvforunity.dll" }, new Dictionary () { {
"CPU",
"x86"
}, {
"OS",
"Windows"
}
},
new Dictionary> () { {BuildTarget.StandaloneWindows,new Dictionary () { {
"CPU",
"x86"
}
}
}
});
SetPlugins (new string[] { pluginsFolderPath + "/Windows/x86_64/opencvforunity.dll" }, new Dictionary () { {
"CPU",
"x86_64"
}, {
"OS",
"Windows"
}
},
new Dictionary> () { {BuildTarget.StandaloneWindows64,new Dictionary () { {
"CPU",
"x86_64"
}
}
}
});
//Linux
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/Linux/x86"), new Dictionary () { {
"CPU",
"x86"
}, {
"OS",
"Linux"
}
},
new Dictionary> () { {BuildTarget.StandaloneLinux,new Dictionary () { {
"CPU",
"x86"
}
}
},
});
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/Linux/x86_64"), new Dictionary () { {
"CPU",
"x86_64"
}, {
"OS",
"Linux"
}
},
new Dictionary> () { {BuildTarget.StandaloneLinux64,new Dictionary () { {
"CPU",
"x86_64"
}
}
},
});
//UWP
#if UNITY_5_0 || UNITY_5_1
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/WSA/UWP/ARM"), null, null);
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/WSA/UWP/x64"), null, null);
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/WSA/UWP/x86"), null, null);
#else
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/WSA/UWP/ARM"), null,
new Dictionary> () { {BuildTarget.WSAPlayer,new Dictionary () { {
"SDK",
"UWP"
}, {
"CPU",
"ARM"
}
}
}
});
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/WSA/UWP/x64"), null,
new Dictionary> () { {BuildTarget.WSAPlayer,new Dictionary () { {
"SDK",
"UWP"
}, {
"CPU",
"x64"
}
}
}
});
SetPlugins (GetPluginFilePaths (pluginsFolderPath + "/WSA/UWP/x86"), null,
new Dictionary> () { {BuildTarget.WSAPlayer,new Dictionary () { {
"SDK",
"UWP"
}, {
"CPU",
"x86"
}
}
}
});
#endif
//WebGL
#if UNITY_2018_2_OR_NEWER
SetPlugins (new string[] { pluginsFolderPath + "/WebGL/2018.2/opencvforunity.bc" }, null, new Dictionary> () { {
BuildTarget.WebGL,
null
}
});
SetPlugins (new string[] { pluginsFolderPath + "/WebGL/5.6/opencvforunity.bc" }, null, null);
#elif UNITY_5_6_OR_NEWER
SetPlugins (new string[] { pluginsFolderPath + "/WebGL/2018.2/opencvforunity.bc" }, null, null);
SetPlugins (new string[] { pluginsFolderPath + "/WebGL/5.6/opencvforunity.bc" }, null, new Dictionary> () { {
BuildTarget.WebGL,
null
}
});
#else
SetPlugins (new string[] { pluginsFolderPath + "/WebGL/2018.2/opencvforunity.bc" }, null, null);
SetPlugins (new string[] { pluginsFolderPath + "/WebGL/5.6/opencvforunity.bc" }, null, null);
#endif
}
///
/// Gets the plugin file paths.
///
/// The plugin file paths.
/// Folder path.
static string[] GetPluginFilePaths (string folderPath)
{
Regex reg = new Regex (".meta$|.DS_Store$|.zip");
try {
return Directory.GetFiles (folderPath).Where (f => !reg.IsMatch (f)).ToArray ();
} catch (Exception ex) {
Debug.LogWarning ("SetPluginImportSettings Faild :" + ex);
return null;
}
}
///
/// Sets the plugins.
///
/// Files.
/// Editor settings.
/// Settings.
public static void SetPlugins (string[] files, Dictionary editorSettings, Dictionary> settings)
{
if (files == null)
return;
foreach (string item in files) {
PluginImporter pluginImporter = PluginImporter.GetAtPath (item) as PluginImporter;
if (pluginImporter != null) {
pluginImporter.SetCompatibleWithAnyPlatform (false);
pluginImporter.SetCompatibleWithEditor (false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.Android, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.iOS, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneWindows, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneWindows64, false);
#if UNITY_2017_3_OR_NEWER
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSX, false);
#else
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSXIntel, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSXIntel64, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSXUniversal, false);
#endif
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneLinux, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneLinux64, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneLinuxUniversal, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.WSAPlayer, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.WebGL, false);
if (editorSettings != null) {
pluginImporter.SetCompatibleWithEditor (true);
foreach (KeyValuePair pair in editorSettings) {
if (pluginImporter.GetEditorData (pair.Key) != pair.Value) {
pluginImporter.SetEditorData (pair.Key, pair.Value);
}
}
}
if (settings != null) {
foreach (KeyValuePair> settingPair in settings) {
pluginImporter.SetCompatibleWithPlatform (settingPair.Key, true);
if (settingPair.Value != null) {
foreach (KeyValuePair pair in settingPair.Value) {
if (pluginImporter.GetPlatformData (settingPair.Key, pair.Key) != pair.Value) {
pluginImporter.SetPlatformData (settingPair.Key, pair.Key, pair.Value);
}
}
}
}
} else {
pluginImporter.SetCompatibleWithPlatform (BuildTarget.Android, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.iOS, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneWindows, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneWindows64, false);
#if UNITY_2017_3_OR_NEWER
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSX, false);
#else
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSXIntel, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSXIntel64, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneOSXUniversal, false);
#endif
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneLinux, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneLinux64, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.StandaloneLinuxUniversal, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.WSAPlayer, false);
pluginImporter.SetCompatibleWithPlatform (BuildTarget.WebGL, false);
}
pluginImporter.SaveAndReimport ();
Debug.Log ("SetPluginImportSettings Success :" + item);
} else {
Debug.LogWarning ("SetPluginImportSettings Faild :" + item);
}
}
}
}
}
#endif