// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Collections.Generic; using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Utilities.Facades { /// /// Lightweight MonoBehavior used to represent active services in scene. /// [ExecuteAlways] [AddComponentMenu("Scripts/MRTK/Core/ServiceFacade")] public class ServiceFacade : MonoBehaviour { public static Dictionary FacadeServiceLookup = new Dictionary(); public static List ActiveFacadeObjects = new List(); public IMixedRealityService Service { get; private set; } = null; public Type ServiceType { get; private set; } = null; public bool Destroyed { get; private set; } = false; public void SetService(IMixedRealityService service) { this.Service = service; if (service == null) { ServiceType = null; name = "(Destroyed)"; gameObject.SetActive(false); return; } else { this.ServiceType = service.GetType(); name = ServiceType.Name; gameObject.SetActive(true); if (!FacadeServiceLookup.ContainsKey(ServiceType)) { FacadeServiceLookup.Add(ServiceType, this); } else { FacadeServiceLookup[ServiceType] = this; } if (!ActiveFacadeObjects.Contains(this)) { ActiveFacadeObjects.Add(this); } } } private void OnDestroy() { Destroyed = true; if (FacadeServiceLookup != null && ServiceType != null) { FacadeServiceLookup.Remove(ServiceType); } ActiveFacadeObjects.Remove(this); } } }