// Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved. using System; using UnityEngine; using UnityEngine.Assertions; using UnityEngine.SceneManagement; namespace Meta.XR.Movement.Samples { /// /// Aligns scene selection icon based on current scene. /// public class MovementSceneSelectIcon : MonoBehaviour { /// /// Information about the icon used to select button. /// [Serializable] protected class IconPositionInformation { /// /// Button to highlight. /// public Transform ButtonTransform; /// /// Scene name to check for. /// public string SceneName; /// /// Valids fields on class using asserts. /// public void Validate() { Assert.IsNotNull(ButtonTransform); Assert.IsFalse(String.IsNullOrEmpty(SceneName)); } } /// /// Icon positions array. /// [SerializeField] protected IconPositionInformation[] _iconInformationArray; /// /// Icon transform to affect. /// [SerializeField] protected Transform _iconTransform; /// /// Offset the icon so that it is centered around the image of each button, /// and enforce a z-value to stay above buttons. These values are based /// on trial and error. /// private float _iconYOffset = 0.0171f; private float _iconZValue = -0.04f; private void Awake() { Assert.IsTrue(_iconInformationArray != null && _iconInformationArray.Length > 0); foreach (var iconInfo in _iconInformationArray) { iconInfo.Validate(); } } private void Start() { bool scenePosSet = false; foreach (var iconPosInfo in _iconInformationArray) { if (iconPosInfo.SceneName == SceneManager.GetActiveScene().name) { var buttonTransformLocalPosition = iconPosInfo.ButtonTransform.localPosition; _iconTransform.localPosition = new Vector3(buttonTransformLocalPosition.x, buttonTransformLocalPosition.y + _iconYOffset, _iconZValue); scenePosSet = true; break; } } if (!scenePosSet) { Debug.LogWarning("Scene selection icon's position not set properly."); } } } }