CollabPlugin.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.Reflection;
  3. using UnityEditor;
  4. namespace Unity.PlasticSCM.Editor
  5. {
  6. internal static class CollabPlugin
  7. {
  8. internal static bool IsEnabled()
  9. {
  10. return IsCollabInstanceEnabled();
  11. }
  12. internal static void Disable()
  13. {
  14. DisableCollabInstance();
  15. DisableCollabInProjectSettings();
  16. }
  17. static void DisableCollabInstance()
  18. {
  19. object collabInstance = GetCollabInstance();
  20. if (collabInstance == null)
  21. return;
  22. // Invokes Collab.instance.SetCollabEnabledForCurrentProject(false)
  23. SetCollabEnabledForCurrentProject(collabInstance, false);
  24. }
  25. static void DisableCollabInProjectSettings()
  26. {
  27. // Invokes PlayerSettings.SetCloudServiceEnabled("Collab", false)
  28. SetCloudServiceEnabled("Collab", false);
  29. AssetDatabase.SaveAssets();
  30. }
  31. static bool IsCollabInstanceEnabled()
  32. {
  33. object collabInstance = GetCollabInstance();
  34. if (collabInstance == null)
  35. return false;
  36. // Invokes Collab.instance.IsCollabEnabledForCurrentProject()
  37. return IsCollabEnabledForCurrentProject(collabInstance);
  38. }
  39. static void SetCollabEnabledForCurrentProject(object collabInstance, bool enable)
  40. {
  41. MethodInfo InternalSetCollabEnabledForCurrentProject =
  42. CollabType.GetMethod("SetCollabEnabledForCurrentProject");
  43. if (InternalSetCollabEnabledForCurrentProject == null)
  44. return;
  45. InternalSetCollabEnabledForCurrentProject.
  46. Invoke(collabInstance, new object[] { enable });
  47. }
  48. static void SetCloudServiceEnabled(string setting, bool enable)
  49. {
  50. MethodInfo InternalSetCloudServiceEnabled = PlayerSettingsType.GetMethod(
  51. "SetCloudServiceEnabled",
  52. BindingFlags.NonPublic | BindingFlags.Static);
  53. if (InternalSetCloudServiceEnabled == null)
  54. return;
  55. InternalSetCloudServiceEnabled.
  56. Invoke(null, new object[] { setting, enable });
  57. }
  58. static object GetCollabInstance()
  59. {
  60. if (CollabType == null)
  61. return null;
  62. PropertyInfo InternalInstance =
  63. CollabType.GetProperty("instance");
  64. if (InternalInstance == null)
  65. return null;
  66. return InternalInstance.GetValue(null, null);
  67. }
  68. static bool IsCollabEnabledForCurrentProject(object collabInstance)
  69. {
  70. MethodInfo InternalIsCollabEnabledForCurrentProject =
  71. CollabType.GetMethod("IsCollabEnabledForCurrentProject");
  72. if (InternalIsCollabEnabledForCurrentProject == null)
  73. return false;
  74. return (bool)InternalIsCollabEnabledForCurrentProject.
  75. Invoke(collabInstance, null);
  76. }
  77. static readonly Type CollabType =
  78. typeof(UnityEditor.Editor).Assembly.
  79. GetType("UnityEditor.Collaboration.Collab");
  80. static readonly Type PlayerSettingsType =
  81. typeof(UnityEditor.PlayerSettings);
  82. }
  83. }