using System.Collections.Generic;
using Unity.XR.CoreUtils;
using UnityEngine.UI;
namespace UnityEngine.XR.Interaction.Toolkit.Samples.SpatialKeyboard
{
///
/// This script is used to optimize the keyboard rendering performance by updating the canvas hierarchy
/// into separate parent transforms based on UI Grouping. This will greatly reduce the number of draw calls.
/// Optimization is only done at runtime to prevent breaking the prefab.
///
public class KeyboardOptimizer : MonoBehaviour
{
[SerializeField]
bool m_OptimizeOnStart = true;
///
/// If enabled, the optimization will be called on .
///
public bool optimizeOnStart
{
get => m_OptimizeOnStart;
set => m_OptimizeOnStart = value;
}
[SerializeField]
Transform m_BatchGroupParentTransform;
///
/// The parent transform for batch groups.
///
public Transform batchGroupParentTransform
{
get => m_BatchGroupParentTransform;
set => m_BatchGroupParentTransform = value;
}
[SerializeField]
Transform m_ButtonParentTransform;
///
/// The parent transform for buttons.
///
public Transform buttonParentTransform
{
get => m_ButtonParentTransform;
set => m_ButtonParentTransform = value;
}
[SerializeField]
Transform m_ImageParentTransform;
///
/// The parent transform for images.
///
public Transform imageParentTransform
{
get => m_ImageParentTransform;
set => m_ImageParentTransform = value;
}
[SerializeField]
Transform m_TextParentTransform;
///
/// The parent transform for text elements.
///
public Transform textParentTransform
{
get => m_TextParentTransform;
set => m_TextParentTransform = value;
}
[SerializeField]
Transform m_IconParentTransform;
///
/// The parent transform for icons.
///
public Transform iconParentTransform
{
get => m_IconParentTransform;
set => m_IconParentTransform = value;
}
[SerializeField]
Transform m_HighlightParentTransform;
///
/// The parent transform for highlights.
///
public Transform highlightParentTransform
{
get => m_HighlightParentTransform;
set => m_HighlightParentTransform = value;
}
bool m_IsCurrentlyOptimized;
///
/// Is the keyboard currently optimized?
///
public bool isCurrentlyOptimized => m_IsCurrentlyOptimized;
///
/// Horizontal layout groups need to be disabled when optimizing the keyboard
/// otherwise the input field will not position correctly.
///
HorizontalLayoutGroup[] m_LayoutGroups;
///
/// List of key data. This is used to store information that allows us
/// to revert the keyboard back to its original state (aka unoptimize).
///
readonly List m_KeyData = new List();
///
/// See .
///
protected void Start()
{
CheckReferences();
Canvas.ForceUpdateCanvases();
if (m_OptimizeOnStart)
Optimize();
}
///
/// Check all the references needed for optimization.
///
void CheckReferences()
{
if (!TryGetOrCreateTransformReferences())
{
Debug.LogError("Failed to get or create transform references. Optimization will not be possible.", this);
return;
}
if (m_KeyData.Count == 0)
GetKeys();
if (m_LayoutGroups == null || m_LayoutGroups.Length == 0)
GetLayoutGroups();
}
bool TryGetOrCreateTransformReferences()
{
if (m_BatchGroupParentTransform == null)
{
var canvasComponent = GetComponentInChildren