ZEDDefineHandler.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. using UnityEditor.PackageManager;
  7. using UnityEditor.PackageManager.Requests;
  8. /// <summary>
  9. /// Manages the various compiler defines that the ZED Unity plugin uses to enable and disable features that are dependent on specific packages.
  10. /// This includes the SteamVR and Oculus plugins (for controller interaction) and OpenCV for Unity (for ArUco detection).
  11. /// </summary>
  12. [InitializeOnLoad]
  13. public class ZEDDefineHandler : AssetPostprocessor
  14. {
  15. static ZEDDefineHandler()
  16. {
  17. }
  18. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
  19. {
  20. #region VR Plugins
  21. if (CheckPackageExists("OVRManager"))
  22. {
  23. ActivateDefine("Oculus", "ZED_OCULUS");
  24. }
  25. else
  26. {
  27. DeactivateDefine("Oculus", "ZED_OCULUS");
  28. }
  29. if (CheckPackageExists("SteamVR_Camera")) //"OpenVR" and "SteamVR" exist in script names in the Oculus plugin.
  30. {
  31. ActivateDefine("SteamVR", "ZED_STEAM_VR");
  32. }
  33. else
  34. {
  35. DeactivateDefine("SteamVR", "ZED_STEAM_VR");
  36. }
  37. if (CheckPackageExists("SteamVR_Input_Sources"))
  38. {
  39. ActivateDefine("SteamVR_2_0_Input", "ZED_SVR_2_0_INPUT");
  40. }
  41. else
  42. {
  43. DeactivateDefine("SteamVR_2_0_Input", "ZED_SVR_2_0_INPUT");
  44. }
  45. #endregion
  46. #region OpenCV
  47. string opencvfilename = "opencvforunity.dll";
  48. opencvfilename = "opencvforunity";
  49. //if(EditorPrefs.GetBool("ZEDOpenCV") == false && CheckPackageExists(opencvfilename))
  50. if (CheckPackageExists(opencvfilename))
  51. {
  52. ActivateDefine("ZEDOpenCV", "ZED_OPENCV_FOR_UNITY");
  53. }
  54. else
  55. {
  56. DeactivateDefine("ZEDOpenCV", "ZED_OPENCV_FOR_UNITY");
  57. }
  58. #endregion
  59. }
  60. /// <summary>
  61. /// Finds if a folder in the project exists with the specified name.
  62. /// Used to check if a plugin has been imported, as the relevant plugins are placed
  63. /// in a folder named after the package. Example: "Assets/Oculus".
  64. /// </summary>
  65. /// <param name="name">Package name.</param>
  66. /// <returns></returns>
  67. public static bool CheckPackageExists(string name)
  68. {
  69. string[] packages = AssetDatabase.FindAssets(name);
  70. return packages.Length != 0;
  71. }
  72. /// <summary>
  73. /// Activates a define tag in the project. Used to enable compiling sections of scripts with that tag enabled.
  74. /// For instance, parts of this script under a #if ZED_STEAM_VR statement will be ignored by the compiler unless ZED_STEAM_VR is enabled.
  75. /// </summary>
  76. public static void ActivateDefine(string packageName, string defineName)
  77. {
  78. EditorPrefs.SetBool(packageName, true);
  79. string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
  80. if (defines.Length != 0)
  81. {
  82. if (!defines.Contains(defineName))
  83. {
  84. defines += ";" + defineName;
  85. }
  86. }
  87. else
  88. {
  89. if (!defines.Contains(defineName))
  90. {
  91. defines += defineName;
  92. }
  93. }
  94. PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, defines);
  95. // FOR OCULUS QUEST
  96. string defines_android = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
  97. if (defines_android.Length != 0)
  98. {
  99. if (!defines_android.Contains(defineName))
  100. {
  101. defines_android += ";" + defineName;
  102. }
  103. }
  104. else
  105. {
  106. if (!defines_android.Contains(defineName))
  107. {
  108. defines_android += defineName;
  109. }
  110. }
  111. PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, defines_android);
  112. }
  113. /// <summary>
  114. /// Removes a define tag from the project.
  115. /// Called whenever a package is checked for but not found.
  116. /// Removing the define tags will prevent compilation of code marked with that tag, like #if ZED_OCULUS.
  117. /// </summary>
  118. public static void DeactivateDefine(string packagename, string defineName)
  119. {
  120. EditorPrefs.SetBool(packagename, false);
  121. string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
  122. if (defines.Length != 0)
  123. {
  124. if (defineName != null && defines.Contains(defineName))
  125. {
  126. defines = defines.Remove(defines.IndexOf(defineName), defineName.Length);
  127. if (defines.LastIndexOf(";") == defines.Length - 1 && defines.Length != 0)
  128. {
  129. defines.Remove(defines.LastIndexOf(";"), 1);
  130. }
  131. }
  132. }
  133. PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, defines);
  134. }
  135. }
  136. #endif