LoadingSpinner.cs
2.34 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
using UnityEngine;
using UnityEngine.Experimental.UIElements;
namespace UnityEditor.PackageManager.UI
{
#if !UNITY_2018_3_OR_NEWER
internal class LoadingSpinnerFactory : UxmlFactory<LoadingSpinner>
{
protected override LoadingSpinner DoCreate(IUxmlAttributes bag, CreationContext cc)
{
return new LoadingSpinner();
}
}
#endif
internal class LoadingSpinner : VisualElement
{
#if UNITY_2018_3_OR_NEWER
internal new class UxmlFactory : UxmlFactory<LoadingSpinner, UxmlTraits>
{
}
// This works around an issue with UXML instantiation
// See https://fogbugz.unity3d.com/f/cases/1046459/
internal new class UxmlTraits : VisualElement.UxmlTraits
{
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
UIUtils.SetElementDisplay(ve, false);
}
}
#endif
public bool InvertColor { get; set; }
public bool Started { get; private set; }
private int rotation;
public LoadingSpinner()
{
InvertColor = false;
Started = false;
UIUtils.SetElementDisplay(this, false);
}
private void UpdateProgress()
{
if (parent == null)
return;
parent.transform.rotation = Quaternion.Euler(0, 0, rotation);
rotation += 3;
if (rotation > 360)
rotation -= 360;
}
public void Start()
{
if (Started)
return;
// Weird hack to make sure loading spinner doesn't generate an error every frame.
// Cannot put in constructor as it give really strange result.
if (parent != null && parent.parent != null)
parent.parent.clippingOptions = ClippingOptions.ClipAndCacheContents;
rotation = 0;
EditorApplication.update += UpdateProgress;
Started = true;
UIUtils.SetElementDisplay(this, true);
}
public void Stop()
{
if (!Started)
return;
EditorApplication.update -= UpdateProgress;
Started = false;
UIUtils.SetElementDisplay(this, false);
}
}
}