// %BANNER_BEGIN% // --------------------------------------------------------------------- // %COPYRIGHT_BEGIN% // Copyright (c) 2022-2023 Magic Leap, Inc. All Rights Reserved. // Use of this file is governed by the Magic Leap 2 Software License Agreement, located here: https://www.magicleap.com/software-license-agreement-ml2 // Terms and conditions applicable to third-party materials accompanying this distribution may also be found in the top-level NOTICE file appearing herein. // %COPYRIGHT_END% // --------------------------------------------------------------------- // %BANNER_END% using System.Collections; using System.Linq; using UnityEngine; namespace MagicLeap.Spectator { public class MLSpectatorMarker : MonoBehaviour { private bool destroy = false; public void FadeAway(float after, float over) { StartCoroutine(Fade(after, over)); } private void OnDisable() { Debug.Log($"MLSpectatorMarker: OnDisable()"); if (destroy) return; destroy = true; Destroy(gameObject); } private void OnDestroy() { Debug.Log($"MLSpectatorMarker: OnDestroy()"); } private IEnumerator Fade(float after, float over) { var mrs = gameObject.GetComponentsInChildren(); if (mrs == null || mrs.Length == 0) { Debug.LogWarning($"MLSpectatorMarker: Fade() - No mesh renderer found"); yield return new WaitForSeconds(after + over); enabled = false; yield break; } //else Debug.LogWarning($"MLSpectatorMarker: Fade() - Found {mrs.Length} mesh renderer(s)"); Debug.Log($"MLSpectatorMarker: Fade() - Waiting for {after} seconds"); yield return new WaitForSeconds(after); var startColors = mrs.Select(x => x.material.color).ToArray(); Debug.Log($"MLSpectatorMarker: Fade() - Fading start"); for (float t = 0; t < over; t += Time.deltaTime) { for (int i = 0; i < mrs.Length; ++i) { mrs[i].material.color = Color.Lerp(startColors[i], Color.clear, t / over); } yield return null; } Debug.Log($"MLSpectatorMarker: Fade() - Fading end"); enabled = false; } } }