A game about forced loneliness, made by TACStudios
1using System;
2using System.Collections.Generic;
3using Unity.Multiplayer.Center.Analytics;
4using Unity.Multiplayer.Center.Questionnaire;
5using Unity.Multiplayer.Center.Recommendations;
6using Unity.Multiplayer.Center.Window.UI;
7using UnityEditor;
8using UnityEngine.UIElements;
9
10namespace Unity.Multiplayer.Center.Window
11{
12 class RecommendationViewBottomBar : VisualElement
13 {
14 readonly Label m_PackageCount;
15 readonly Button m_InstallPackageButton;
16 readonly Label m_InfoLabel;
17
18 IMultiplayerCenterAnalytics m_Analytics;
19
20 MultiplayerCenterWindow m_Window = EditorWindow.GetWindow<MultiplayerCenterWindow>();
21 List<string> m_PackagesToInstallIds = new ();
22 List<string> m_PackagesToInstallNames = new ();
23 RecommendationViewData m_RecommendationViewData;
24 SolutionsToRecommendedPackageViewData m_SolutionToPackageData;
25
26 public RecommendationViewBottomBar(IMultiplayerCenterAnalytics analytics)
27 {
28 m_Analytics = analytics;
29 name = "bottom-bar";
30 m_PackageCount = new Label {name = "package-count"};
31 m_InfoLabel = new Label();
32
33 // Setup Install Button
34 m_InstallPackageButton = new Button(OnInstallButtonClicked) {text = "Install Packages"};
35 m_InstallPackageButton.AddToClassList(StyleClasses.NextStepButton);
36
37 // Put the button in a container
38 var installPackageContainer = new VisualElement() {name = "install-package-container"};
39 installPackageContainer.Add(m_InstallPackageButton);
40
41 Add(m_PackageCount);
42 Add(m_InfoLabel);
43 Add(installPackageContainer);
44 }
45
46 void OnInstallButtonClicked()
47 {
48 if (!PackageManagement.IsAnyMultiplayerPackageInstalled() || WarnDialogForPackageInstallation())
49 {
50 SendInstallationAnalyticsEvent();
51 InstallSelectedPackagesAndExtension();
52 }
53 }
54
55 void SendInstallationAnalyticsEvent()
56 {
57 var answerObject = UserChoicesObject.instance;
58 m_Analytics.SendInstallationEvent(answerObject.UserAnswers, answerObject.Preset,
59 AnalyticsUtils.GetPackagesWithAnalyticsFormat(m_RecommendationViewData, m_SolutionToPackageData));
60 }
61
62 bool WarnDialogForPackageInstallation()
63 {
64 var warningMessage =
65 "Ensure compatibility with your current multiplayer packages before installing or upgrading the following:\n" +
66 string.Join("\n", m_PackagesToInstallNames);
67 return EditorUtility.DisplayDialog("Install Packages", warningMessage, "OK", "Cancel");
68 }
69
70 void InstallSelectedPackagesAndExtension()
71 {
72 SetInfoTextForInstallation(isInstalling:true);
73 m_Window.DisableUiForInstallation();
74 PackageManagement.InstallPackages(m_PackagesToInstallIds, onAllInstalled: OnInstallationFinished);
75 }
76
77 void OnInstallationFinished(bool success)
78 {
79 SetInfoTextForInstallation(isInstalling:false);
80 m_Window.RequestShowGettingStartedTabAfterDomainReload();
81 m_Window.ReenableUiAfterInstallation();
82 }
83
84 public void UpdatePackagesToInstall(RecommendationViewData data, SolutionsToRecommendedPackageViewData packageViewData)
85 {
86 m_RecommendationViewData = data;
87 m_SolutionToPackageData = packageViewData;
88 var packages = RecommendationUtils.PackagesToInstall(data, packageViewData);
89 RecommendationUtils.GetPackagesWithAdditionalPackages(packages, out m_PackagesToInstallIds, out m_PackagesToInstallNames, out var toolTip);
90 m_PackageCount.tooltip = toolTip;
91
92 // Note: quickstart is counted in the list of packages to install, but not the names
93 m_PackageCount.text = $"Packages to install: {m_PackagesToInstallNames.Count}";
94 // if the list is empty, disable the button
95 m_InstallPackageButton.SetEnabled(m_PackagesToInstallNames.Count > 0);
96 }
97
98 internal void SetInfoTextForInstallation(bool isInstalling)
99 {
100 SetInfoLabelTextAndVisibility("Downloading packages, please wait ...", isInstalling);
101 }
102
103 internal void SetInfoTextForCheckingPackages(bool isChecking)
104 {
105 SetInfoLabelTextAndVisibility("Querying packages information ...", isChecking);
106
107 // Handle the case of reopening the window during the installation.
108 // When reopening the window, the packages are being checked. Once that check is done, we still want to
109 // display the installation package text if there is an ongoing installation.
110 if(!isChecking && !PackageManagement.IsInstallationFinished())
111 SetInfoTextForInstallation(isInstalling:true);
112 }
113
114 void SetInfoLabelTextAndVisibility(string text, bool isVisible)
115 {
116 m_InfoLabel.text = text;
117 m_InfoLabel.visible = isVisible;
118 }
119 }
120}