forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdsProjectSettings.cs
More file actions
395 lines (341 loc) · 16.7 KB
/
Copy pathAdsProjectSettings.cs
File metadata and controls
395 lines (341 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.Advertisements;
using UnityEngine.Networking;
namespace UnityEditor.Connect
{
internal class AdsProjectSettings : ServicesProjectSettings
{
const string k_ServiceName = "Ads";
// Actual states for the Ads state machine
const string k_StateNameDisabled = "DisabledState";
const string k_StateNameEnabled = "EnabledState";
const string k_AdsCommonUxmlPath = "UXML/ServicesWindow/AdsProjectSettings.uxml";
const string k_AdsDisabledUxmlPath = "UXML/ServicesWindow/AdsProjectSettingsDisabled.uxml";
const string k_AdsEnabledUxmlPath = "UXML/ServicesWindow/AdsProjectSettingsEnabled.uxml";
// Elements of the UXML
const string k_ServiceToggleClassName = "service-toggle";
const string k_ServiceNameProperty = "serviceName";
const string k_ServiceScrollContainerClassName = "scroll-container";
const string k_AdsPermissionMessage = "You do not have sufficient permissions to enable / disable Ads service.";
// Asset store GUIDs
const string k_AdvertisingAndroidAdsDllGuid = "cad99f482ce25421196533fe02e6a13e";
const string k_AdvertisingIosAdsDllGuid = "d6f3e2ade30154a80a137e0079f66a08";
const string k_AdvertisingEditorDllGuid = "56921141d53fd4a5888445107b1b1286";
const string k_AdsAssetStorePackageInstalledWarning = "The Asset Store Package is installed.\n Usage of Package Manager is recommended.";
const string k_AdsPackageName = "Ads Package";
const string k_GettingStartedLink = "GettingStarted";
const string k_LearnMoreLink = "LearnMore";
const string k_GoToDashboardLinkName = "GoToDashboard";
const string k_ToggleTestModeName = "ToggleTestMode";
const string k_AppleGameIdName = "AppleGameId";
const string k_AndroidGameIdName = "AndroidGameId";
VisualElement m_GoToDashboard;
Toggle m_MainServiceToggle;
bool m_EventHandlerInitialized;
EnabledState m_EnabledState;
DisabledState m_DisabledState;
internal enum AdsEvent
{
Enabling,
Disabling,
}
[SettingsProvider]
public static SettingsProvider CreateServicesProvider()
{
return new AdsProjectSettings(AdsService.instance.projectSettingsPath, SettingsScope.Project);
}
SimpleStateMachine<AdsEvent> m_StateMachine;
public AdsProjectSettings(string path, SettingsScope scopes, IEnumerable<string> keywords = null)
: base(path, scopes, k_ServiceName, keywords)
{
m_StateMachine = new SimpleStateMachine<AdsEvent>();
m_StateMachine.AddEvent(AdsEvent.Enabling);
m_StateMachine.AddEvent(AdsEvent.Disabling);
m_EnabledState = new EnabledState(m_StateMachine, this);
m_DisabledState = new DisabledState(m_StateMachine, this);
m_StateMachine.AddState(m_EnabledState);
m_StateMachine.AddState(m_DisabledState);
}
void OnDestroy()
{
UnregisterEvent();
}
protected override Notification.Topic[] notificationTopicsToSubscribe => new[]
{
Notification.Topic.AdsService,
Notification.Topic.ProjectBind,
Notification.Topic.CoppaCompliance
};
protected override SingleService serviceInstance => AdsService.instance;
protected override string serviceUssClassName => "ads";
void SetupServiceToggle(SingleService singleService)
{
m_MainServiceToggle.SetProperty(k_ServiceNameProperty, singleService.name);
m_MainServiceToggle.SetEnabled(false);
UpdateServiceToggleAndDashboardLink(singleService.IsServiceEnabled());
if (singleService.displayToggle)
{
m_MainServiceToggle.RegisterValueChangedCallback(evt =>
{
if (currentUserPermission != UserRole.Owner && currentUserPermission != UserRole.Manager)
{
UpdateServiceToggleAndDashboardLink(evt.previousValue);
return;
}
singleService.EnableService(evt.newValue);
});
}
else
{
m_MainServiceToggle.style.display = DisplayStyle.None;
}
}
void UpdateServiceToggleAndDashboardLink(bool isEnabled)
{
if (m_GoToDashboard != null)
{
m_GoToDashboard.style.display = (isEnabled) ? DisplayStyle.Flex : DisplayStyle.None;
}
if (m_MainServiceToggle != null)
{
m_MainServiceToggle.SetValueWithoutNotify(isEnabled);
SetupServiceToggleLabel(m_MainServiceToggle, isEnabled);
}
}
void SetUpGameId()
{
var unavailableGameId = L10n.Tr("N/A");
// Getting the textfield for updates with the actual GameId values...
var appleGameId = AdvertisementSettings.GetGameId(RuntimePlatform.IPhonePlayer);
var androidGameId = AdvertisementSettings.GetGameId(RuntimePlatform.Android);
var scrollContainer = rootVisualElement.Q(className: k_ServiceScrollContainerClassName);
scrollContainer.Q<TextField>(name: k_AppleGameIdName)?.SetValueWithoutNotify(appleGameId ?? unavailableGameId);
scrollContainer.Q<TextField>(name: k_AndroidGameIdName)?.SetValueWithoutNotify(androidGameId ?? unavailableGameId);
}
protected override void ToggleRestrictedVisualElementsAvailability(bool enable)
{
var serviceToggleContainer = rootVisualElement.Q(className: k_ServiceToggleContainerClassName);
var unityToggle = serviceToggleContainer?.Q(className: k_UnityToggleClassName);
if (unityToggle != null)
{
unityToggle.SetEnabled(enable);
if (!enable)
{
var notifications = NotificationManager.instance.GetNotificationsForTopics(Notification.Topic.AdsService);
if (notifications.Any(notification => notification.rawMessage == k_AdsPermissionMessage))
{
return;
}
NotificationManager.instance.Publish(
Notification.Topic.AdsService,
Notification.Severity.Warning,
k_AdsPermissionMessage);
}
}
}
protected override void ActivateAction(string searchContext)
{
// Must reset properties every time this is activated
var mainTemplate = EditorGUIUtility.Load(k_AdsCommonUxmlPath) as VisualTreeAsset;
var newVisual = mainTemplate.CloneTree().contentContainer;
ServicesUtils.TranslateStringsInTree(newVisual);
rootVisualElement.Add(newVisual);
// Make sure to activate the state machine to the current state...
if (AdsService.instance.IsServiceEnabled())
{
m_StateMachine.Initialize(m_EnabledState);
}
else
{
m_StateMachine.Initialize(m_DisabledState);
}
// Register the events for enabling / disabling the service only once.
RegisterEvent();
// Moved the Go to dashboard link to the header title section.
m_GoToDashboard = rootVisualElement.Q(k_GoToDashboardLinkName);
if (m_GoToDashboard != null)
{
var clickable = new Clickable(() =>
{
OpenDashboardForOrgKeyAndProjectGuid(ServicesConfiguration.instance.adsDashboardUrl);
});
m_GoToDashboard.AddManipulator(clickable);
}
m_MainServiceToggle = rootVisualElement.Q<Toggle>(className: k_ServiceToggleClassName);
SetupServiceToggle(AdsService.instance);
}
protected override void DeactivateAction()
{
// Make sure to reset the state machine
m_StateMachine.ClearCurrentState();
UnregisterEvent();
}
void RegisterEvent()
{
if (!m_EventHandlerInitialized)
{
AdsService.instance.serviceAfterEnableEvent += ServiceIsEnablingEvent;
AdsService.instance.serviceAfterDisableEvent += ServiceIsDisablingEvent;
AdsService.instance.gameIdsUpdatedEvent += GameIdsUpdatedEvent;
m_EventHandlerInitialized = true;
}
}
void UnregisterEvent()
{
if (m_EventHandlerInitialized)
{
AdsService.instance.serviceAfterEnableEvent -= ServiceIsEnablingEvent;
AdsService.instance.serviceAfterDisableEvent -= ServiceIsDisablingEvent;
AdsService.instance.gameIdsUpdatedEvent -= GameIdsUpdatedEvent;
m_EventHandlerInitialized = false;
}
}
void ServiceIsEnablingEvent(object sender, EventArgs args)
{
if (settingsWindow.GetCurrentProvider() == this)
{
m_MainServiceToggle.SetValueWithoutNotify(true);
m_StateMachine.ProcessEvent(AdsEvent.Enabling);
}
}
void ServiceIsDisablingEvent(object sender, EventArgs args)
{
if (settingsWindow.GetCurrentProvider() == this)
{
m_MainServiceToggle.SetValueWithoutNotify(false);
m_StateMachine.ProcessEvent(AdsEvent.Disabling);
}
}
void GameIdsUpdatedEvent()
{
SetUpGameId();
}
class BaseState : GenericBaseState<AdsProjectSettings, AdsEvent>
{
public BaseState(string name, SimpleStateMachine<AdsEvent> simpleStateMachine, AdsProjectSettings provider)
: base(name, simpleStateMachine, provider)
{
}
}
// Disabled state of the service
sealed class DisabledState : BaseState
{
public DisabledState(SimpleStateMachine<AdsEvent> simpleStateMachine, AdsProjectSettings provider)
: base(k_StateNameDisabled, simpleStateMachine, provider)
{
ModifyActionForEvent(AdsEvent.Enabling, HandleBinding);
}
public override void EnterState()
{
var generalTemplate = EditorGUIUtility.Load(k_AdsDisabledUxmlPath) as VisualTreeAsset;
var scrollContainer = provider.rootVisualElement.Q(null, k_ServiceScrollContainerClassName);
scrollContainer.Clear();
if (generalTemplate != null)
{
var newVisual = generalTemplate.CloneTree().contentContainer;
ServicesUtils.TranslateStringsInTree(newVisual);
scrollContainer.Add(newVisual);
var gettingStarted = scrollContainer.Q(k_GettingStartedLink);
if (gettingStarted != null)
{
var clickable = new Clickable(() =>
{
Application.Openurl(ServicesConfiguration.instance.adsGettingStartedUrl);
});
gettingStarted.AddManipulator(clickable);
}
provider.UpdateServiceToggleAndDashboardLink(provider.serviceInstance.IsServiceEnabled());
}
scrollContainer.Add(ServicesUtils.SetupSupportedPlatformsBlock(ServicesUtils.GetAdsSupportedPlatforms()));
provider.HandlePermissionRestrictedControls();
}
SimpleStateMachine<AdsEvent>.State HandleBinding(AdsEvent raisedEvent)
{
return stateMachine.GetStateByName(k_StateNameEnabled);
}
}
// Enabled state of the service
sealed class EnabledState : BaseState
{
bool m_AssetStoreWarningHasBeenShown;
public EnabledState(SimpleStateMachine<AdsEvent> simpleStateMachine, AdsProjectSettings provider)
: base(k_StateNameEnabled, simpleStateMachine, provider)
{
ModifyActionForEvent(AdsEvent.Disabling, HandleUnbinding);
// Related protected variables
topicForNotifications = Notification.Topic.AdsService;
notLatestPackageInstalledInfo = string.Format(k_NotLatestPackageInstalledInfo, k_AdsPackageName);
packageInstallationHeadsup = string.Format(k_PackageInstallationHeadsup, k_AdsPackageName);;
duplicateInstallWarning = k_DuplicateInstallWarning;
packageInstallationDialogTitle = string.Format(k_PackageInstallationDialogTitle, k_AdsPackageName);
}
void VerifyAssetStorePackageInstallation()
{
var assetStoreAndroidDllPath = AssetImporter.GetAtPath(AssetDatabase.GUIDToAssetPath(k_AdvertisingAndroidAdsDllGuid /* UnityEngine.Advertising.Android.dll */)) as PluginImporter;
var assetStoreIosDllPath = AssetImporter.GetAtPath(AssetDatabase.GUIDToAssetPath(k_AdvertisingIosAdsDllGuid /* UnityEngine.Advertising.iOS.dll */)) as PluginImporter;
var assetStoreEditorDllPath = AssetImporter.GetAtPath(AssetDatabase.GUIDToAssetPath(k_AdvertisingEditorDllGuid /* UnityEngine.Advertising.Editor.dll */)) as PluginImporter;
// Assume it is not installed...
assetStorePackageInstalled = false;
if ((assetStoreAndroidDllPath != null) || (assetStoreIosDllPath != null) || (assetStoreEditorDllPath != null))
{
assetStorePackageInstalled = true;
}
}
public override void EnterState()
{
// Just get the latest state of the asset store package installation...
VerifyAssetStorePackageInstallation();
if (assetStorePackageInstalled && !m_AssetStoreWarningHasBeenShown)
{
NotificationManager.instance.Publish(Notification.Topic.AdsService, Notification.Severity.Warning, L10n.Tr(k_AdsAssetStorePackageInstalledWarning));
m_AssetStoreWarningHasBeenShown = true;
}
// If we haven't received new bound info, fetch them
var generalTemplate = EditorGUIUtility.Load(k_AdsEnabledUxmlPath) as VisualTreeAsset;
var scrollContainer = provider.rootVisualElement.Q(null, k_ServiceScrollContainerClassName);
if (generalTemplate != null)
{
var newVisual = generalTemplate.CloneTree().contentContainer;
ServicesUtils.TranslateStringsInTree(newVisual);
scrollContainer.Clear();
scrollContainer.Add(newVisual);
var learnMore = scrollContainer.Q(k_LearnMoreLink);
if (learnMore != null)
{
var clickable = new Clickable(() =>
{
Application.Openurl(ServicesConfiguration.instance.adsLearnMoreUrl);
});
learnMore.AddManipulator(clickable);
}
var toggleTestMode = scrollContainer.Q<Toggle>(k_ToggleTestModeName);
if (toggleTestMode != null)
{
toggleTestMode.SetValueWithoutNotify(AdvertisementSettings.testMode);
toggleTestMode.RegisterValueChangedCallback(evt =>
{
AdvertisementSettings.testMode = evt.newValue;
});
}
provider.UpdateServiceToggleAndDashboardLink(provider.serviceInstance.IsServiceEnabled());
// Prepare the package section and update the package information
PreparePackageSection(scrollContainer);
UpdatePackageInformation();
provider.HandlePermissionRestrictedControls();
}
provider.SetUpGameId();
}
SimpleStateMachine<AdsEvent>.State HandleUnbinding(AdsEvent raisedEvent)
{
return stateMachine.GetStateByName(k_StateNameDisabled);
}
}
}
}