A game about forced loneliness, made by TACStudios
1using System.Linq;
2#if UNITY_EDITOR
3using UnityEngine;
4using UnityEditor;
5using UnityEngine.InputSystem;
6using UnityEditor.PackageManager;
7using UnityEditor.PackageManager.Requests;
8
9internal class DownloadableSample : ScriptableObject
10{
11 public string url;
12 public string[] packageDeps;
13}
14
15[CustomEditor(typeof(DownloadableSample))]
16internal class DownloadableSampleInspector : Editor
17{
18 private ListRequest list;
19 private AddRequest add;
20
21 public void OnEnable()
22 {
23 list = Client.List();
24 }
25
26 bool HasPackage(string id)
27 {
28 if (id.Contains('@'))
29 return list.Result.Any(x => x.packageId == id);
30 else
31 return list.Result.Any(x => x.packageId.Split('@')[0] == id);
32 }
33
34 public override void OnInspectorGUI()
35 {
36 GUILayout.Label("Downloadable Sample", EditorStyles.boldLabel);
37
38 var sample = (DownloadableSample)target;
39 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);
40 if (GUILayout.Button("Download Sample"))
41 {
42 var url = sample.url.Replace("%VERSION%", InputSystem.version.ToString());
43 Application.OpenURL(url);
44 }
45
46 GUILayout.Space(10);
47
48 GUILayout.Label("Package Dependencies", EditorStyles.boldLabel);
49
50 // We are adding a new package, wait for the operation to finish and then relist.
51 if (add != null)
52 {
53 if (add.IsCompleted)
54 {
55 add = null;
56 list = Client.List();
57 }
58 }
59
60 if (add != null || !list.IsCompleted)
61 // Keep refreshing while we are waiting for Packman to resolve our request.
62 Repaint();
63 else
64 {
65 if (!sample.packageDeps.All(x => HasPackage(x)))
66 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);
67 }
68
69
70 foreach (var req in sample.packageDeps)
71 {
72 Rect rect = EditorGUILayout.GetControlRect(true, 20);
73
74 GUI.Label(rect, new GUIContent(req), EditorStyles.label);
75 rect.width -= 160;
76 rect.x += 160;
77 if (add != null || !list.IsCompleted)
78 {
79 using (new EditorGUI.DisabledScope(true))
80 {
81 GUI.Label(rect, "checking…", EditorStyles.label);
82 }
83 }
84 else if (HasPackage(req))
85 {
86 GUI.Label(rect, $"OK \u2713", EditorStyles.boldLabel);
87 }
88 else
89 {
90 GUI.Label(rect, "Missing \u2717", EditorStyles.label);
91 rect.x += rect.width - 80;
92 rect.width = 80;
93 if (GUI.Button(rect, "Install"))
94 add = Client.Add(req);
95 }
96 }
97 }
98}
99
100#endif