CrossPlatformInputInitialize.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.PSM:
  32. case BuildTarget.Tizen:
  33. case BuildTarget.WSAPlayer:
  34. EditorUtility.DisplayDialog("Mobile Input",
  35. "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.",
  36. "OK");
  37. break;
  38. default:
  39. EditorUtility.DisplayDialog("Mobile Input",
  40. "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.",
  41. "OK");
  42. break;
  43. }
  44. }
  45. [MenuItem("Mobile Input/Enable", true)]
  46. private static bool EnableValidate()
  47. {
  48. var defines = GetDefinesList(mobileBuildTargetGroups[0]);
  49. return !defines.Contains("MOBILE_INPUT");
  50. }
  51. [MenuItem("Mobile Input/Disable")]
  52. private static void Disable()
  53. {
  54. SetEnabled("MOBILE_INPUT", false, true);
  55. switch (EditorUserBuildSettings.activeBuildTarget)
  56. {
  57. case BuildTarget.Android:
  58. case BuildTarget.iOS:
  59. EditorUtility.DisplayDialog("Mobile Input",
  60. "You have disabled Mobile Input. Mobile control rigs won't be visible, and the Cross Platform Input functions will always return standalone controls.",
  61. "OK");
  62. break;
  63. }
  64. }
  65. [MenuItem("Mobile Input/Disable", true)]
  66. private static bool DisableValidate()
  67. {
  68. var defines = GetDefinesList(mobileBuildTargetGroups[0]);
  69. return defines.Contains("MOBILE_INPUT");
  70. }
  71. private static BuildTargetGroup[] buildTargetGroups = new BuildTargetGroup[]
  72. {
  73. BuildTargetGroup.Standalone,
  74. BuildTargetGroup.Android,
  75. BuildTargetGroup.iOS
  76. };
  77. private static BuildTargetGroup[] mobileBuildTargetGroups = new BuildTargetGroup[]
  78. {
  79. BuildTargetGroup.Android,
  80. BuildTargetGroup.iOS,
  81. BuildTargetGroup.PSM,
  82. BuildTargetGroup.SamsungTV,
  83. BuildTargetGroup.Tizen,
  84. BuildTargetGroup.WSA
  85. };
  86. private static void SetEnabled(string defineName, bool enable, bool mobile)
  87. {
  88. //Debug.Log("setting "+defineName+" to "+enable);
  89. foreach (var group in mobile ? mobileBuildTargetGroups : buildTargetGroups)
  90. {
  91. var defines = GetDefinesList(group);
  92. if (enable)
  93. {
  94. if (defines.Contains(defineName))
  95. {
  96. return;
  97. }
  98. defines.Add(defineName);
  99. }
  100. else
  101. {
  102. if (!defines.Contains(defineName))
  103. {
  104. return;
  105. }
  106. while (defines.Contains(defineName))
  107. {
  108. defines.Remove(defineName);
  109. }
  110. }
  111. string definesString = string.Join(";", defines.ToArray());
  112. PlayerSettings.SetScriptingDefineSymbolsForGroup(group, definesString);
  113. }
  114. }
  115. private static List<string> GetDefinesList(BuildTargetGroup group)
  116. {
  117. return new List<string>(PlayerSettings.GetScriptingDefineSymbolsForGroup(group).Split(';'));
  118. }
  119. }
  120. }