forked from TrackMan/Unity.Package.FigmaToUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFigma.cs
56 lines (45 loc) · 1.61 KB
/
Figma.cs
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
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
namespace Figma
{
using Attributes;
[DefaultExecutionOrder(-10)]
[AddComponentMenu("Figma/Figma")]
[RequireComponent(typeof(UIDocument))]
public class Figma : MonoBehaviour
{
#region Fields
[SerializeField] string title;
[SerializeField] bool filter;
[SerializeField] bool reorder;
[SerializeField] bool waitFrameBeforeRebuild = true;
[SerializeField] string[] fontsDirs;
#endregion
#region Properties
public string Title => title;
public bool Filter => filter;
#endregion
#region Base Methods
async void OnEnable()
{
if (Application.isBatchMode) return;
UIDocument document = GetComponent<UIDocument>();
if (document.rootVisualElement is null) return;
IRootElement[] elements = GetComponentsInChildren<IRootElement>();
VisualElementMetadata.Initialize(document, elements);
if (!Application.isPlaying) return;
if (waitFrameBeforeRebuild) await new WaitForEndOfFrame();
VisualElementMetadata.Rebuild(elements);
VisualElement root = document.rootVisualElement.Q(UxmlAttribute.prefix);
if (root is null || !reorder) return;
foreach (IRootElement element in elements.OrderBy(x => x.RootOrder))
{
if (element.Root is null) continue;
element.Root.RemoveFromHierarchy();
root.Add(element.Root);
}
}
#endregion
}
}