You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.0 KiB
C#

using UnityEngine;
using UnityEngine.UIElements;
namespace WebGLSupport
{
public class WebGLInputManipulator : Manipulator
{
private GameObject go;
private bool showHtmlElement;
public WebGLInputManipulator(bool showHtmlElement = false)
{
this.showHtmlElement = showHtmlElement;
}
protected override void RegisterCallbacksOnTarget()
{
// uitoolkit is already support mobile.
if (!Application.isMobilePlatform)
{
var textInput = target.Q("unity-text-input");
textInput.RegisterCallback<FocusInEvent>(OnFocusInEvent);
textInput.RegisterCallback<FocusOutEvent>(OnFocusOutEvent);
}
}
protected override void UnregisterCallbacksFromTarget()
{
// uitoolkit is already support mobile.
if (!Application.isMobilePlatform)
{
var textInput = target.Q("unity-text-input");
textInput.UnregisterCallback<FocusInEvent>(OnFocusInEvent);
textInput.UnregisterCallback<FocusOutEvent>(OnFocusOutEvent);
}
}
private void OnFocusInEvent(FocusInEvent evt)
{
if (go != null)
{
GameObject.Destroy(go);
go = null;
}
go = new GameObject("WebGLInputManipulator");
// add WebGLUIToolkitMonoBehaviour for hold TextField!
var uitoolkit = go.AddComponent<WebGLUIToolkitTextField>();
uitoolkit.TextField = target as TextField;
// add WebGLInput to handle the event!
var webglInput = go.AddComponent<WebGLInput>();
webglInput.showHtmlElement = showHtmlElement;
// select it!!
webglInput.OnSelect();
}
private void OnFocusOutEvent(FocusOutEvent evt)
{
if (go != null)
{
GameObject.Destroy(go);
go = null;
}
}
}
}