CrossPlatformInputInitialize.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. namespace UnityStandardAssets.CrossPlatformInput.Inspector
  5. {
  6. [InitializeOnLoad]
  7. public class CrossPlatformInitialize
  8. {
  9. // Custom compiler defines:
  10. //
  11. // CROSS_PLATFORM_INPUT : denotes that cross platform input package exists, so that other packages can use their CrossPlatformInput functions.
  12. // EDITOR_MOBILE_INPUT : denotes that mobile input should be used in editor, if a mobile build target is selected. (i.e. using Unity Remote app).
  13. // MOBILE_INPUT : denotes that mobile input should be used right now!
  14. static CrossPlatformInitialize()
  15. {
  16. var defines = GetDefinesList(buildTargetGroups[0]);
  17. if (!defines.Contains("CROSS_PLATFORM_INPUT"))
  18. {
  19. SetEnabled("CROSS_PLATFORM_INPUT", true, false);
  20. SetEnabled("MOBILE_INPUT", true, true);
  21. }
  22. }
  23. [MenuItem("Mobile Input/Enable")]
  24. private static void Enable()
  25. {
  26. SetEnabled("MOBILE_INPUT", true, true);
  27. switch (EditorUserBuildSettings.activeBuildTarget)
  28. {
  29. case BuildTarget.Android:
  30. case BuildTarget.iOS:
  31. case BuildTarget.WSAPlayer:
  32. EditorUtility.DisplayDialog("Mobile Input",
  33. "You have enabled Mobile Input. You'll need to use the Unity Remote app on a connected device to control your game in the Editor.",
  34. "OK");
  35. break;
  36. default:
  37. EditorUtility.DisplayDialog("Mobile Input",
  38. "You have enabled Mobile Input, but you have a non-mobile build target selected in your build settings. The mobile control rigs won't be active or visible on-screen until you switch the build target to a mobile platform.",
  39. "OK");
  40. break;
  41. }
  42. }
  43. [MenuItem("Mobile Input/Enable", true)]
  44. private static bool EnableValidate()
  45. {
  46. var defines = GetDefinesList(mobileBuildTargetGroups[0]);
  47. return !defines.Contains("MOBILE_INPUT");
  48. }
  49. [MenuItem("Mobile Input/Disable")]
  50. private static void Disable()
  51. {
  52. SetEnabled("MOBILE_INPUT", false, true);
  53. switch (EditorUserBuildSettings.activeBuildTarget)
  54. {
  55. case BuildTarget.Android:
  56. case BuildTarget.iOS:
  57. EditorUtility.DisplayDialog("Mobile Input",
  58. "You have disabled Mobile Input. Mobile control rigs won't be visible, and the Cross Platform Input functions will always return standalone controls.",
  59. "OK");
  60. break;
  61. }
  62. }
  63. [MenuItem("Mobile Input/Disable", true)]
  64. private static bool DisableValidate()
  65. {
  66. var defines = GetDefinesList(mobileBuildTargetGroups[0]);
  67. return defines.Contains("MOBILE_INPUT");
  68. }
  69. private static BuildTargetGroup[] buildTargetGroups = new BuildTargetGroup[]
  70. {
  71. BuildTargetGroup.Standalone,
  72. BuildTargetGroup.Android,
  73. BuildTargetGroup.iOS
  74. };
  75. private static BuildTargetGroup[] mobileBuildTargetGroups = new BuildTargetGroup[]
  76. {
  77. BuildTargetGroup.Android,
  78. BuildTargetGroup.iOS,
  79. BuildTargetGroup.WSA
  80. };
  81. private static void SetEnabled(string defineName, bool enable, bool mobile)
  82. {
  83. //Debug.Log("setting "+defineName+" to "+enable);
  84. foreach (var group in mobile ? mobileBuildTargetGroups : buildTargetGroups)
  85. {
  86. var defines = GetDefinesList(group);
  87. if (enable)
  88. {
  89. if (defines.Contains(defineName))
  90. {
  91. return;
  92. }
  93. defines.Add(defineName);
  94. }
  95. else
  96. {
  97. if (!defines.Contains(defineName))
  98. {
  99. return;
  100. }
  101. while (defines.Contains(defineName))
  102. {
  103. defines.Remove(defineName);
  104. }
  105. }
  106. string definesString = string.Join(";", defines.ToArray());
  107. PlayerSettings.SetScriptingDefineSymbolsForGroup(group, definesString);
  108. }
  109. }
  110. private static List<string> GetDefinesList(BuildTargetGroup group)
  111. {
  112. return new List<string>(PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';'));
  113. }
  114. }
  115. }