forked from ReactUnity/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactRendererUIToolkit.cs
More file actions
62 lines (57 loc) · 2.16 KB
/
Copy pathReactRendererUIToolkit.cs
File metadata and controls
62 lines (57 loc) · 2.16 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
#if UNITY_2021_2_OR_NEWER
using System.Collections.Generic;
using ReactUnity.Scheduling;
using ReactUnity.Styling;
using ReactUnity.Styling.Rules;
using UnityEngine;
using UnityEngine.UIElements;
namespace ReactUnity.UIToolkit
{
[RequireComponent(typeof(UIDocument))]
[RequireComponent(typeof(AudioSource))]
public class ReactRendererUIToolkit : ReactRendererBase
{
public VisualElement Root => GetComponent<UIDocument>()?.rootVisualElement;
public IconSet DefaultIconSet;
public List<IconSet> IconSets;
protected override void ClearRoot()
{
Root?.Clear();
}
protected override ReactContext CreateContext(ScriptSource script)
{
var ctx = new UIToolkitContext(new UIToolkitContext.Options
{
HostElement = Root,
Globals = Globals,
Source = script,
Timer = Timer ?? UnscaledTimer.Instance,
MediaProvider = MediaProvider,
OnRestart = () => Render(),
IconSets = IconSets,
DefaultIconSet = DefaultIconSet,
OnAudioPlayback = PlayAudio,
EngineType = EngineType,
Debug = AdvancedOptions.DebugMode != DebugMode.None,
AwaitDebugger = AdvancedOptions.DebugMode == DebugMode.DebugAndAwait,
BeforeStart = AdvancedOptions.BeforeStart.Invoke,
AfterStart = AdvancedOptions.AfterStart.Invoke,
Pooling = AdvancedOptions.Pooling,
UnknownPropertyHandling = AdvancedOptions.UnknownPropertyHandling,
});
ctx.Initialize();
return ctx;
}
public void PlayAudio(AudioClip clip, float volume, float pitch)
{
var source = GetComponent<AudioSource>();
source.pitch = pitch;
source.PlayOneShot(clip, volume);
}
protected override IMediaProvider CreateMediaProvider()
{
return DefaultMediaProvider.CreateMediaProvider("runtime", "uitoolkit", false);
}
}
}
#endif