DownloadableSample.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Linq;
  2. #if UNITY_EDITOR
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEngine.InputSystem;
  6. using UnityEditor.PackageManager;
  7. using UnityEditor.PackageManager.Requests;
  8. internal class DownloadableSample : ScriptableObject
  9. {
  10. public string url;
  11. public string[] packageDeps;
  12. }
  13. [CustomEditor(typeof(DownloadableSample))]
  14. internal class DownloadableSampleInspector : Editor
  15. {
  16. private ListRequest list;
  17. private AddRequest add;
  18. public void OnEnable()
  19. {
  20. list = Client.List();
  21. }
  22. bool HasPackage(string id)
  23. {
  24. if (id.Contains('@'))
  25. return list.Result.Any(x => x.packageId == id);
  26. else
  27. return list.Result.Any(x => x.packageId.Split('@')[0] == id);
  28. }
  29. public override void OnInspectorGUI()
  30. {
  31. GUILayout.Label("Downloadable Sample", EditorStyles.boldLabel);
  32. var sample = (DownloadableSample)target;
  33. EditorGUILayout.HelpBox($"The {target.name} sample is stored outside of the input system package, because it contains custom project settings and is too big to be distributed as part of the sample. Instead, you can download it as a .unitypackage file which you can import into the project. Click the button below to download this sample", MessageType.Info);
  34. if (GUILayout.Button("Download Sample"))
  35. {
  36. var url = sample.url.Replace("%VERSION%", InputSystem.version.ToString());
  37. Application.OpenURL(url);
  38. }
  39. GUILayout.Space(10);
  40. GUILayout.Label("Package Dependencies", EditorStyles.boldLabel);
  41. // We are adding a new package, wait for the operation to finish and then relist.
  42. if (add != null)
  43. {
  44. if (add.IsCompleted)
  45. {
  46. add = null;
  47. list = Client.List();
  48. }
  49. }
  50. if (add != null || !list.IsCompleted)
  51. // Keep refreshing while we are waiting for Packman to resolve our request.
  52. Repaint();
  53. else
  54. {
  55. if (!sample.packageDeps.All(x => HasPackage(x)))
  56. EditorGUILayout.HelpBox($"The {target.name} sample requires the following packages to be installed in your Project. Please install all the required packages before downloading the sample!", MessageType.Warning);
  57. }
  58. foreach (var req in sample.packageDeps)
  59. {
  60. Rect rect = EditorGUILayout.GetControlRect(true, 20);
  61. GUI.Label(rect, new GUIContent(req), EditorStyles.label);
  62. rect.width -= 160;
  63. rect.x += 160;
  64. if (add != null || !list.IsCompleted)
  65. {
  66. using (new EditorGUI.DisabledScope(true))
  67. {
  68. GUI.Label(rect, "checking…", EditorStyles.label);
  69. }
  70. }
  71. else if (HasPackage(req))
  72. {
  73. GUI.Label(rect, $"OK \u2713", EditorStyles.boldLabel);
  74. }
  75. else
  76. {
  77. GUI.Label(rect, "Missing \u2717", EditorStyles.label);
  78. rect.x += rect.width - 80;
  79. rect.width = 80;
  80. if (GUI.Button(rect, "Install"))
  81. add = Client.Add(req);
  82. }
  83. }
  84. }
  85. }
  86. #endif