// %BANNER_BEGIN% // --------------------------------------------------------------------- // %COPYRIGHT_BEGIN% // Copyright (c) (2024) Magic Leap, Inc. All Rights Reserved. // Use of this file is governed by the 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.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.UI; using UnityEngine.XR.OpenXR; using UnityEngine.XR.OpenXR.NativeTypes; using MagicLeap.OpenXR.Features; public class SegmentedDimmerSample : MonoBehaviour { [SerializeField] private Text status; [SerializeField] private Dropdown blendModeSelection; [Header("Objects")] [SerializeField] private List testObjects = new(); [SerializeField] private float colorUpdateInterval = 2.5f; private XrEnvironmentBlendMode currentBlendMode; private XrEnvironmentBlendMode defaultBlendMode; private IEnumerable materials; private bool dimmerEnabled = false; private MagicLeapRenderingExtensionsFeature rendering; private void Start() { materials = testObjects.Select(to => to.material); rendering = OpenXRSettings.Instance.GetFeature(); rendering.BlendMode = XrEnvironmentBlendMode.AlphaBlend; defaultBlendMode = currentBlendMode = rendering.BlendMode; blendModeSelection.value = ((int)currentBlendMode) - 2; blendModeSelection.onValueChanged.AddListener(OnBlendModeSelectionChange); dimmerEnabled = IsDimmerEnabledInSettings(); UpdateStatusText(); StartCoroutine(UpdateColors()); } private void OnApplicationPause(bool pause) { if (!pause) { dimmerEnabled = IsDimmerEnabledInSettings(); UpdateStatusText(); } } private void OnBlendModeSelectionChange(int blendModeIndex) { var selectedBlendMode = (XrEnvironmentBlendMode)(blendModeIndex + 2); rendering.BlendMode = selectedBlendMode; currentBlendMode = rendering.BlendMode; UpdateStatusText(); } private void OnDestroy() { blendModeSelection.onValueChanged.RemoveAllListeners(); rendering.BlendMode = defaultBlendMode; } private IEnumerator UpdateColors() { while (true) { yield return new WaitForSeconds(colorUpdateInterval); foreach (var mat in materials) { mat.color = Random.ColorHSV(0f, 1, 1f, 1f, 1f, 1f, .01f, 1f); } } } private void UpdateStatusText() { string vis = currentBlendMode == XrEnvironmentBlendMode.AlphaBlend && dimmerEnabled ? "VISIBLE" : "NOT VISIBLE"; status.text = $"Segmented Dimmer Enabled:\n{dimmerEnabled}\n" + $"Current blend mode:\n{currentBlendMode}\n" + $"Segmented Dimmer:\n{vis}"; } public bool IsDimmerEnabledInSettings() { if (!Application.isEditor) { // Get context using var actClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); var context = actClass.GetStatic("currentActivity"); var systemGlobal = new AndroidJavaClass("android.provider.Settings$System"); var dimmerMode = systemGlobal.CallStatic("getInt", context.Call("getContentResolver"), "is_segmented_dimmer_enabled"); return dimmerMode > 0; } return false; } }