/
BaseUwbClientManager.cs
148 lines (127 loc) · 4.4 KB
/
BaseUwbClientManager.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
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
// UnityWebBrowser (UWB)
// Copyright (c) 2021-2022 Voltstro-Studios
//
// This project is under the MIT license. See the LICENSE.md file for more details.
using System;
using UnityEngine;
namespace VoltstroStudios.UnityWebBrowser.Core
{
/// <summary>
/// Base for all other systems that want to build custom rendering with UWB.
/// <para>Provides controls that can be used with Unity events (such as OnClick).</para>
/// <para>For other controls and events, use the <see cref="browserClient" />.</para>
/// <para>
/// For those who want to implement this base, DO NOT USE <c>Start()</c>, <c>FixedUpdate</c> or <c>OnDestroy</c>,
/// instead override <see cref="OnStart" />, <see cref="OnFixedUpdate" /> and <see cref="OnDestroyed" />.
/// </para>
/// </summary>
[DisallowMultipleComponent]
public abstract class BaseUwbClientManager : MonoBehaviour
{
/// <summary>
/// The <see cref="WebBrowserClient" />, what handles the communication between the UWB engine and Unity
/// </summary>
[Tooltip("The browser client, what handles the communication between the UWB engine and Unity")]
public WebBrowserClient browserClient = new();
private void Start()
{
//Start the browser client
browserClient.Init();
OnStart();
}
private void Update()
{
browserClient.UpdateFps();
}
private void FixedUpdate()
{
browserClient.LoadTextureData();
OnFixedUpdate();
}
private void OnDestroy()
{
browserClient.Dispose();
OnDestroyed();
}
/// <summary>
/// Override this instead of using <see cref="Start" />
/// </summary>
protected virtual void OnStart()
{
}
/// <summary>
/// Override this instead of using <see cref="FixedUpdate" />
/// </summary>
protected virtual void OnFixedUpdate()
{
}
/// <summary>
/// Override this instead of using <see cref="OnDestroy" />
/// </summary>
protected virtual void OnDestroyed()
{
}
#region Client Methods
/// <summary>
/// Makes the browser go back a page
/// </summary>
/// <exception cref="UwbIsNotConnectedException"></exception>
public void GoBack()
{
browserClient.GoBack();
}
/// <summary>
/// Make the browser go forward a page
/// </summary>
/// <exception cref="UwbIsNotConnectedException"></exception>
public void GoForward()
{
browserClient.GoForward();
}
/// <summary>
/// Makes the browser go to a url
/// </summary>
/// <param name="url"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="UwbIsNotConnectedException"></exception>
public void NavigateUrl(string url)
{
if (string.IsNullOrWhiteSpace(url))
throw new ArgumentNullException(nameof(url));
browserClient.LoadUrl(url);
}
/// <summary>
/// Refreshes the browser
/// </summary>
/// <exception cref="UwbIsNotConnectedException"></exception>
public void Refresh()
{
browserClient.Refresh();
}
/// <summary>
/// Loads HTML code
/// </summary>
/// <param name="html"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="UwbIsNotConnectedException"></exception>
public void LoadHtml(string html)
{
if (string.IsNullOrWhiteSpace(html))
throw new ArgumentNullException(nameof(html));
browserClient.LoadHtml(html);
}
/// <summary>
/// Executes JS
/// </summary>
/// <param name="js"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="UwbIsNotConnectedException"></exception>
public void ExecuteJs(string js)
{
if (string.IsNullOrWhiteSpace(js))
throw new ArgumentNullException(nameof(js));
browserClient.ExecuteJs(js);
}
#endregion
}
}