Skip to content

Commit 00ecea0

Browse files
committed
Create new CesiumCreditSystemManager class
1 parent 6360b6e commit 00ecea0

File tree

3 files changed

+76
-39
lines changed

3 files changed

+76
-39
lines changed

Editor/CesiumCreditSystemManager.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using UnityEditor;
2+
using UnityEditor.SceneManagement;
3+
using UnityEngine;
4+
using UnityEngine.SceneManagement;
5+
6+
namespace CesiumForUnity
7+
{
8+
[InitializeOnLoad]
9+
public static class CesiumCreditSystemManager
10+
{
11+
static CesiumCreditSystemManager()
12+
{
13+
EditorApplication.playModeStateChanged += HandleEnteringPlayMode;
14+
EditorSceneManager.sceneClosing += HandleClosingScene;
15+
}
16+
17+
/// <summary>
18+
/// This handles the destruction of the default credit system between scene switches in the
19+
/// Unity Editor.
20+
/// Without this, the credit system will live between instances and fail to capture the current
21+
/// scene's credits.
22+
/// </summary>
23+
/// <param name="scene">The scene.</param>
24+
/// <param name="removingScene">Whether the scene is being removed.</param>
25+
private static void HandleClosingScene(Scene scene, bool removingScene)
26+
{
27+
Destroy(CesiumCreditSystem.GetDefaultCreditSystem());
28+
}
29+
30+
/// <summary>
31+
/// This handles the destruction of the default credit system while entering Play Mode.
32+
/// Without this, the persisting credit system's UI will not register with the Play Mode view, leading
33+
/// to missing credits.
34+
/// </summary>
35+
/// <param name="state">The state change between the Edit and Play modes.</param>
36+
private static void HandleEnteringPlayMode(PlayModeStateChange state)
37+
{
38+
if (state == PlayModeStateChange.EnteredPlayMode)
39+
{
40+
Destroy(CesiumCreditSystem.GetDefaultCreditSystem());
41+
}
42+
}
43+
44+
/// <summary>
45+
/// Destroys the input credit system depending on the current Editor context. This is the same as
46+
/// the internal UnityLifetime class (which is currently inaccessible from Editor classes).
47+
/// </summary>
48+
/// <param name="creditSystem">The credit system.</param>
49+
private static void Destroy(CesiumCreditSystem creditSystem)
50+
{
51+
if (creditSystem == null) { return; }
52+
53+
// In the Editor, we must use DestroyImmediate because Destroy won't
54+
// actually destroy the object.
55+
if (!EditorApplication.isPlaying)
56+
{
57+
Object.DestroyImmediate(creditSystem.gameObject);
58+
return;
59+
}
60+
61+
Object.Destroy(creditSystem.gameObject);
62+
}
63+
}
64+
}

Editor/CesiumCreditSystemManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/CesiumCreditSystem.cs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ private void OnEnable()
160160
this._images = new List<Texture2D>();
161161

162162
Cesium3DTileset.OnSetShowCreditsOnScreen += this.ForceUpdateCredits;
163-
#if UNITY_EDITOR
164-
EditorApplication.playModeStateChanged += HandleEnteringPlayMode;
165-
EditorSceneManager.sceneClosing += HandleClosingSceneView;
166-
#endif
167163
}
168164

169165
private void Update()
@@ -187,10 +183,6 @@ private void OnDestroy()
187183

188184
if (this == _defaultCreditSystem)
189185
{
190-
#if UNITY_EDITOR
191-
EditorApplication.playModeStateChanged -= HandleEnteringPlayMode;
192-
EditorSceneManager.sceneClosing -= HandleClosingSceneView;
193-
#endif
194186
_defaultCreditSystem = null;
195187
}
196188
}
@@ -252,7 +244,7 @@ private static CesiumCreditSystem CreateDefaultCreditSystem()
252244
/// Gets the default credit system, or creates a new default credit system instance if none exist.
253245
/// </summary>
254246
/// <returns>The default CesiumCreditSystem instance.</returns>
255-
internal static CesiumCreditSystem GetDefaultCreditSystem()
247+
public static CesiumCreditSystem GetDefaultCreditSystem()
256248
{
257249
if (_defaultCreditSystem == null)
258250
{
@@ -327,35 +319,5 @@ internal IEnumerator LoadImage(string url)
327319

328320
texture.wrapMode = TextureWrapMode.Clamp;
329321
}
330-
331-
#if UNITY_EDITOR
332-
/// <summary>
333-
/// This handles the destruction of the credit system between scene switches in the Unity Editor.
334-
/// Without this, the credit system will live between instances and won't properly render the
335-
/// current scene's credits.
336-
/// </summary>
337-
/// <param name="scene">The scene.</param>
338-
/// <param name="removingScene">Whether or not the closing scene is also being removed.</param>
339-
private static void HandleClosingSceneView(Scene scene, bool removingScene)
340-
{
341-
if (_defaultCreditSystem != null && _defaultCreditSystem.gameObject.scene == scene)
342-
{
343-
UnityLifetime.Destroy(_defaultCreditSystem.gameObject);
344-
}
345-
}
346-
347-
/// <summary>
348-
/// This handles the destruction of the credit system while entering Play Mode.
349-
/// Without this, the persisting credit system's UI will not register with the Play Mode view, leading
350-
/// to missing credits.
351-
/// </summary>
352-
/// <param name="state">The state change between the Edit and Play modes.</param>
353-
private static void HandleEnteringPlayMode(PlayModeStateChange state)
354-
{
355-
if (state == PlayModeStateChange.EnteredPlayMode && _defaultCreditSystem != null) {
356-
UnityLifetime.Destroy(_defaultCreditSystem.gameObject);
357-
}
358-
}
359-
#endif
360322
}
361323
}

0 commit comments

Comments
 (0)