diff --git a/Assets/HoloToolkit/Input/Scripts/Cursor/Cursor.cs b/Assets/HoloToolkit/Input/Scripts/Cursor/Cursor.cs index ecf8059e4d2..220b8453892 100644 --- a/Assets/HoloToolkit/Input/Scripts/Cursor/Cursor.cs +++ b/Assets/HoloToolkit/Input/Scripts/Cursor/Cursor.cs @@ -290,7 +290,8 @@ protected virtual void UpdateCursorTransform() GameObject newTargetedObject = focusDetails.Object; // Get the forward vector looking back along the pointing ray. - Vector3 lookForward = -Pointer.Ray.direction; + RayStep lastStep = Pointer.Rays[Pointer.Rays.Length - 1]; + Vector3 lookForward = -lastStep.direction; // Normalize scale on before update targetScale = Vector3.one; @@ -300,7 +301,7 @@ protected virtual void UpdateCursorTransform() { TargetedObject = null; TargetedCursorModifier = null; - targetPosition = Pointer.Ray.origin + Pointer.Ray.direction * DefaultCursorDistance; + targetPosition = lastStep.terminus; targetRotation = lookForward.magnitude > 0 ? Quaternion.LookRotation(lookForward, Vector3.up) : transform.rotation; } else diff --git a/Assets/HoloToolkit/Input/Scripts/Cursor/CursorModifier.cs b/Assets/HoloToolkit/Input/Scripts/Cursor/CursorModifier.cs index cbec5bfa415..db3eda645d2 100644 --- a/Assets/HoloToolkit/Input/Scripts/Cursor/CursorModifier.cs +++ b/Assets/HoloToolkit/Input/Scripts/Cursor/CursorModifier.cs @@ -78,7 +78,8 @@ public Quaternion GetModifiedRotation(ICursor cursor) { Quaternion rotation; - Vector3 forward = UseGazeBasedNormal ? -cursor.Pointer.Ray.direction : HostTransform.rotation * CursorNormal; + RayStep lastStep = cursor.Pointer.Rays[cursor.Pointer.Rays.Length - 1]; + Vector3 forward = UseGazeBasedNormal ? -lastStep.direction : HostTransform.rotation * CursorNormal; // Determine the cursor forward if (forward.magnitude > 0) diff --git a/Assets/HoloToolkit/Input/Scripts/Focus/FocusDetails.cs b/Assets/HoloToolkit/Input/Scripts/Focus/FocusDetails.cs index c7c2fa708f7..23eec78b0e5 100644 --- a/Assets/HoloToolkit/Input/Scripts/Focus/FocusDetails.cs +++ b/Assets/HoloToolkit/Input/Scripts/Focus/FocusDetails.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. +using System; using UnityEngine; namespace HoloToolkit.Unity.InputModule @@ -9,6 +10,7 @@ namespace HoloToolkit.Unity.InputModule /// FocusDetails struct contains information about which game object has the focus currently. /// Also contains information about the normal of that point. /// + [Serializable] public struct FocusDetails { public Vector3 Point; diff --git a/Assets/HoloToolkit/Input/Scripts/Focus/FocusManager.cs b/Assets/HoloToolkit/Input/Scripts/Focus/FocusManager.cs index d7318a28ea0..7983e86cd1c 100644 --- a/Assets/HoloToolkit/Input/Scripts/Focus/FocusManager.cs +++ b/Assets/HoloToolkit/Input/Scripts/Focus/FocusManager.cs @@ -128,7 +128,7 @@ private void OnValidate() #region Data - private class PointerData + private class PointerData : PointerResult { public readonly IPointingSource PointingSource; @@ -146,25 +146,24 @@ public PointerInputEventData UnityUIPointerData } } - public Vector3 StartPoint { get; private set; } - - public FocusDetails End { get; private set; } - - public GameObject PreviousEndObject { get; private set; } - - public RaycastHit LastRaycastHit { get; private set; } - public PointerData(IPointingSource pointingSource) { PointingSource = pointingSource; } + [Obsolete("Use UpdateHit(RaycastHit hit, RayStep sourceRay, int rayStepIndex) or UpdateHit (float extent)")] public void UpdateHit(RaycastHit hit) + { + throw new NotImplementedException(); + } + + public void UpdateHit(RaycastHit hit, RayStep sourceRay, int rayStepIndex) { LastRaycastHit = hit; PreviousEndObject = End.Object; + RayStepIndex = rayStepIndex; - StartPoint = PointingSource.Ray.origin; + StartPoint = sourceRay.origin; End = new FocusDetails { Point = hit.point, @@ -173,12 +172,13 @@ public void UpdateHit(RaycastHit hit) }; } - public void UpdateHit(RaycastResult result, RaycastHit hit) + public void UpdateHit(RaycastResult result, RaycastHit hit, RayStep sourceRay, int rayStepIndex) { // We do not update the PreviousEndObject here because // it's already been updated in the first physics raycast. - StartPoint = PointingSource.Ray.origin; + RayStepIndex = rayStepIndex; + StartPoint = sourceRay.origin; End = new FocusDetails { Point = hit.point, @@ -191,18 +191,26 @@ public void UpdateHit(float extent) { PreviousEndObject = End.Object; - StartPoint = PointingSource.Ray.origin; + RayStep firstStep = PointingSource.Rays[0]; + RayStep finalStep = PointingSource.Rays[PointingSource.Rays.Length - 1]; + RayStepIndex = 0; + + StartPoint = firstStep.origin; End = new FocusDetails { - Point = (StartPoint + (extent * PointingSource.Ray.direction)), - Normal = (-PointingSource.Ray.direction), + Point = finalStep.terminus, + Normal = (-finalStep.direction), Object = null }; } - public void ResetFocusedObjects() + public void ResetFocusedObjects(bool clearPreviousObject = true) { - PreviousEndObject = null; + if (clearPreviousObject) + { + PreviousEndObject = null; + } + End = new FocusDetails { Point = End.Point, @@ -489,7 +497,20 @@ private void UpdatePointers() private void UpdatePointer(PointerData pointer) { - pointer.PointingSource.UpdatePointer(); + // Call the pointer's OnPreRaycast function + // This will give it a chance to prepare itself for raycasts + // eg, by building its Rays array + pointer.PointingSource.OnPreRaycast(); + + // If pointer interaction isn't enabled, clear its result object and return + if (!pointer.PointingSource.InteractionEnabled) + { + // Don't clear the previous focused object since we still want to trigger FocusExit events + pointer.ResetFocusedObjects(false); + return; + } + + // Otherwise, continue var prioritizedLayerMasks = (pointer.PointingSource.PrioritizedLayerMasksOverride ?? pointingRaycastLayerMasks); // Perform raycast to determine focused object @@ -501,6 +522,14 @@ private void UpdatePointer(PointerData pointer) // NOTE: We need to do this AFTER RaycastPhysics so we use the current hit point to perform the correct 2D UI Raycast. RaycastUnityUI(pointer, prioritizedLayerMasks); } + + // Set the pointer's result last + pointer.PointingSource.Result = pointer; + + // Call the pointer's OnPostRaycast function + // This will give it a chance to respond to raycast results + // eg by updating its appearance + pointer.PointingSource.OnPostRaycast(); } /// @@ -508,19 +537,49 @@ private void UpdatePointer(PointerData pointer) /// private void RaycastPhysics(PointerData pointer, LayerMask[] prioritizedLayerMasks) { - bool isHit; RaycastHit physicsHit = default(RaycastHit); - float extent = GetPointingExtent(pointer); + RayStep rayStep = default(RayStep); + bool isHit = false; + int rayStepIndex = 0; + + // Check raycast for each step in the pointing source + for (int i = 0; i < pointer.PointingSource.Rays.Length; i++) + { + if (RaycastPhysicsStep(pointer.PointingSource.Rays[i], prioritizedLayerMasks, out physicsHit)) + { + // Set the pointer source's origin ray to this step + isHit = true; + rayStep = pointer.PointingSource.Rays[i]; + rayStepIndex = i; + // No need to continue once we've hit something + break; + } + } + + if (isHit) + { + pointer.UpdateHit(physicsHit, rayStep, rayStepIndex); + } + else + { + pointer.UpdateHit(GetPointingExtent(pointer)); + } + } + + private bool RaycastPhysicsStep(RayStep step, LayerMask[] prioritizedLayerMasks, out RaycastHit physicsHit) + { + bool isHit = false; + physicsHit = default(RaycastHit); // If there is only one priority, don't prioritize if (prioritizedLayerMasks.Length == 1) { - isHit = Physics.Raycast(pointer.PointingSource.Ray, out physicsHit, extent, prioritizedLayerMasks[0]); + isHit = Physics.Raycast(step.origin, step.direction, out physicsHit, step.length, prioritizedLayerMasks[0]); } else { // Raycast across all layers and prioritize - RaycastHit? hit = PrioritizeHits(Physics.RaycastAll(pointer.PointingSource.Ray, extent, /*All layers*/ -1), prioritizedLayerMasks); + RaycastHit? hit = PrioritizeHits(Physics.RaycastAll(step.origin, step.direction, step.length, Physics.AllLayers), prioritizedLayerMasks); isHit = hit.HasValue; if (isHit) @@ -529,14 +588,7 @@ private void RaycastPhysics(PointerData pointer, LayerMask[] prioritizedLayerMas } } - if (isHit) - { - pointer.UpdateHit(physicsHit); - } - else - { - pointer.UpdateHit(GetPointingExtent(pointer)); - } + return isHit; } private void RaycastUnityUI(PointerData pointer, LayerMask[] prioritizedLayerMasks) @@ -544,21 +596,59 @@ private void RaycastUnityUI(PointerData pointer, LayerMask[] prioritizedLayerMas Debug.Assert(pointer.End.Point != Vector3.zero, string.Format("No pointer {0} end point found to raycast against!", pointer.PointingSource.GetType())); Debug.Assert(UIRaycastCamera != null, "You must assign a UIRaycastCamera on the FocusManager before you can process uGUI raycasting."); + RaycastResult uiRaycastResult = default(RaycastResult); + bool overridePhysicsRaycast = false; + RayStep rayStep = default(RayStep); + int rayStepIndex = 0; + + // Cast rays for every step until we score a hit + for (int i = 0; i < pointer.PointingSource.Rays.Length; i++) + { + if (RaycastUnityUIStep(pointer, pointer.PointingSource.Rays[i], prioritizedLayerMasks, out overridePhysicsRaycast, out uiRaycastResult)) + { + rayStepIndex = i; + rayStep = pointer.PointingSource.Rays[i]; + break; + } + } + + // Check if we need to overwrite the physics raycast info + if ((pointer.End.Object == null || overridePhysicsRaycast) && uiRaycastResult.module.eventCamera == UIRaycastCamera) + { + newUiRaycastPosition.x = uiRaycastResult.screenPosition.x; + newUiRaycastPosition.y = uiRaycastResult.screenPosition.y; + newUiRaycastPosition.z = uiRaycastResult.distance; + + Vector3 worldPos = UIRaycastCamera.ScreenToWorldPoint(newUiRaycastPosition); + + var hitInfo = new RaycastHit + { + point = worldPos, + normal = -uiRaycastResult.gameObject.transform.forward + }; + + pointer.UpdateHit(uiRaycastResult, hitInfo, rayStep, rayStepIndex); + } + } + + private bool RaycastUnityUIStep(PointerData pointer, RayStep step, LayerMask[] prioritizedLayerMasks, out bool overridePhysicsRaycast, out RaycastResult uiRaycastResult) + { // Move the uiRaycast camera to the the current pointer's position. - UIRaycastCamera.transform.position = pointer.PointingSource.Ray.origin; - UIRaycastCamera.transform.forward = pointer.PointingSource.Ray.direction; + UIRaycastCamera.transform.position = step.origin; + UIRaycastCamera.transform.forward = step.direction; // We always raycast from the center of the camera. pointer.UnityUIPointerData.position = new Vector2(UIRaycastCamera.pixelWidth * 0.5f, UIRaycastCamera.pixelHeight * 0.5f); // Graphics raycast - RaycastResult uiRaycastResult = EventSystem.current.Raycast(pointer.UnityUIPointerData, prioritizedLayerMasks); + uiRaycastResult = EventSystem.current.Raycast(pointer.UnityUIPointerData, prioritizedLayerMasks); pointer.UnityUIPointerData.pointerCurrentRaycast = uiRaycastResult; + overridePhysicsRaycast = false; + // If we have a raycast result, check if we need to overwrite the physics raycast info if (uiRaycastResult.gameObject != null) { - bool overridePhysicsRaycast = false; if (pointer.End.Object != null) { // Check layer prioritization @@ -588,25 +678,11 @@ private void RaycastUnityUI(PointerData pointer, LayerMask[] prioritizedLayerMas } } } - - // Check if we need to overwrite the physics raycast info - if ((pointer.End.Object == null || overridePhysicsRaycast) && uiRaycastResult.module.eventCamera == UIRaycastCamera) - { - newUiRaycastPosition.x = uiRaycastResult.screenPosition.x; - newUiRaycastPosition.y = uiRaycastResult.screenPosition.y; - newUiRaycastPosition.z = uiRaycastResult.distance; - - Vector3 worldPos = UIRaycastCamera.ScreenToWorldPoint(newUiRaycastPosition); - - var hitInfo = new RaycastHit - { - point = worldPos, - normal = -uiRaycastResult.gameObject.transform.forward - }; - - pointer.UpdateHit(uiRaycastResult, hitInfo); - } + // If we've hit somthing, no need to go further + return true; } + // If we haven't hit something, keep going + return false; } private void UpdateFocusedObjects() diff --git a/Assets/HoloToolkit/Input/Scripts/Focus/IPointingSource.cs b/Assets/HoloToolkit/Input/Scripts/Focus/IPointingSource.cs index 4714f15f041..4ddd81ceec6 100644 --- a/Assets/HoloToolkit/Input/Scripts/Focus/IPointingSource.cs +++ b/Assets/HoloToolkit/Input/Scripts/Focus/IPointingSource.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. +using System; using UnityEngine; using UnityEngine.EventSystems; @@ -11,13 +12,22 @@ namespace HoloToolkit.Unity.InputModule /// public interface IPointingSource { - Ray Ray { get; } + bool InteractionEnabled { get; } float? ExtentOverride { get; } + [Obsolete("Will be removed in a later version. For equivalent behavior have Rays return a RayStep array with a single element.")] + Ray Ray { get; } + + RayStep[] Rays { get; } + LayerMask[] PrioritizedLayerMasksOverride { get; } - void UpdatePointer(); + PointerResult Result { get; set; } + + void OnPreRaycast(); + + void OnPostRaycast(); bool OwnsInput(BaseEventData eventData); } diff --git a/Assets/HoloToolkit/Input/Scripts/Focus/InputSourcePointer.cs b/Assets/HoloToolkit/Input/Scripts/Focus/InputSourcePointer.cs index 331dbd65230..5b96240e18b 100644 --- a/Assets/HoloToolkit/Input/Scripts/Focus/InputSourcePointer.cs +++ b/Assets/HoloToolkit/Input/Scripts/Focus/InputSourcePointer.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. +using System; using UnityEngine; using UnityEngine.EventSystems; @@ -20,41 +21,61 @@ public class InputSourcePointer : IPointingSource public bool OwnAllInput { get; set; } - public Ray Ray - { + [Obsolete("Will be removed in a later version. Use Rays instead.")] + public Ray Ray { get { return Rays[0]; } } + + public RayStep[] Rays { get { - return (RayStabilizer == null) - ? rawRay - : RayStabilizer.StableRay; + return rays; } } + public PointerResult Result { get; set; } + public float? ExtentOverride { get; set; } public LayerMask[] PrioritizedLayerMasksOverride { get; set; } - private Ray rawRay = default(Ray); + public bool InteractionEnabled + { + get + { + return true; + } + } - public void UpdatePointer() + private RayStep[] rays = new RayStep[1] { new RayStep(Vector3.zero, Vector3.forward) }; + + public void OnPreRaycast() { if (InputSource == null) { - rawRay = default(Ray); + rays[0] = default(RayStep); } else { Debug.Assert(InputSource.SupportsInputInfo(InputSourceId, SupportedInputInfo.Pointing)); - InputSource.TryGetPointingRay(InputSourceId, out rawRay); + Ray pointingRay = default(Ray); + if (InputSource.TryGetPointingRay(InputSourceId, out pointingRay)) + { + rays[0].CopyRay(pointingRay, FocusManager.Instance.GetPointingExtent (this)); + } } if (RayStabilizer != null) { - RayStabilizer.UpdateStability(rawRay.origin, rawRay.direction); + RayStabilizer.UpdateStability(rays[0].origin, rays[0].direction); + rays[0].CopyRay(RayStabilizer.StableRay, FocusManager.Instance.GetPointingExtent(this)); } } + public void OnPostRaycast() + { + // Nothing needed + } + public bool OwnsInput(BaseEventData eventData) { return (OwnAllInput || InputIsFromSource(eventData)); @@ -67,6 +88,6 @@ public bool InputIsFromSource(BaseEventData eventData) return (inputData != null) && (inputData.InputSource == InputSource) && (inputData.SourceId == InputSourceId); - } + } } } diff --git a/Assets/HoloToolkit/Input/Scripts/Focus/PointerResult.cs b/Assets/HoloToolkit/Input/Scripts/Focus/PointerResult.cs new file mode 100644 index 00000000000..3a38828fa9e --- /dev/null +++ b/Assets/HoloToolkit/Input/Scripts/Focus/PointerResult.cs @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using System; +using UnityEngine; + +namespace HoloToolkit.Unity.InputModule +{ + [Serializable] + public class PointerResult + { + public Vector3 StartPoint { get; protected set; } + + public FocusDetails End { get; protected set; } + + public GameObject PreviousEndObject { get; protected set; } + + public RaycastHit LastRaycastHit { get; protected set; } + + /// + /// The index of the step that produced the last raycast hit + /// 0 when no raycast hit + /// + public int RayStepIndex { get; protected set; } + } +} \ No newline at end of file diff --git a/Assets/HoloToolkit/Input/Scripts/Focus/PointerResult.cs.meta b/Assets/HoloToolkit/Input/Scripts/Focus/PointerResult.cs.meta new file mode 100644 index 00000000000..06f0967325b --- /dev/null +++ b/Assets/HoloToolkit/Input/Scripts/Focus/PointerResult.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: b87b365aaff84aa4a9f18f6323d917e5 +timeCreated: 1510163952 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/HoloToolkit/Input/Scripts/Focus/RayStep.cs b/Assets/HoloToolkit/Input/Scripts/Focus/RayStep.cs new file mode 100644 index 00000000000..734f8758814 --- /dev/null +++ b/Assets/HoloToolkit/Input/Scripts/Focus/RayStep.cs @@ -0,0 +1,48 @@ +using System; +using UnityEngine; + +namespace HoloToolkit.Unity +{ + [Serializable] + public struct RayStep + { + public RayStep(Vector3 origin, Vector3 terminus) + { + this.origin = origin; + this.terminus = terminus; + length = Vector3.Distance(origin, terminus); + direction = (this.terminus - this.origin).normalized; + } + + public Vector3 origin { get; private set; } + public Vector3 terminus { get; private set; } + public Vector3 direction { get; private set; } + public float length { get; private set; } + + public Vector3 GetPoint(float distance) + { + return Vector3.MoveTowards(origin, terminus, distance); + } + + public void UpdateRayStep(Vector3 origin, Vector3 terminus) + { + this.origin = origin; + this.terminus = terminus; + length = Vector3.Distance(origin, terminus); + direction = (this.terminus - this.origin).normalized; + } + + public void CopyRay (Ray ray, float rayLength) + { + length = rayLength; + origin = ray.origin; + direction = ray.direction; + terminus = origin + (direction * length); + } + + public static implicit operator Ray(RayStep r) + { + return new Ray(r.origin, r.direction); + } + } +} \ No newline at end of file diff --git a/Assets/HoloToolkit/Input/Scripts/Focus/RayStep.cs.meta b/Assets/HoloToolkit/Input/Scripts/Focus/RayStep.cs.meta new file mode 100644 index 00000000000..ac791a097a0 --- /dev/null +++ b/Assets/HoloToolkit/Input/Scripts/Focus/RayStep.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: d4f2bb3817435f84a8471a990b4820c2 +timeCreated: 1509375252 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/HoloToolkit/Input/Scripts/Gaze/GazeManager.cs b/Assets/HoloToolkit/Input/Scripts/Gaze/GazeManager.cs index 289302227a7..3b0b955ea87 100644 --- a/Assets/HoloToolkit/Input/Scripts/Gaze/GazeManager.cs +++ b/Assets/HoloToolkit/Input/Scripts/Gaze/GazeManager.cs @@ -65,7 +65,7 @@ public class GazeManager : Singleton, IPointingSource /// public Vector3 GazeOrigin { - get { return Ray.origin; } + get { return Rays[0].origin; } } /// @@ -73,7 +73,7 @@ public Vector3 GazeOrigin /// public Vector3 GazeNormal { - get { return Ray.direction; } + get { return Rays[0].direction; } } /// @@ -112,9 +112,15 @@ public Vector3 GazeNormal [Tooltip("True to draw a debug view of the ray.")] public bool DebugDrawRay; - - public Ray Ray { get; private set; } - + public PointerResult Result { get; set; } + + [Obsolete("Will be removed in a later version. Use Rays instead.")] + public Ray Ray { get { return Rays[0]; } } + + public RayStep[] Rays { get { return rays; } } + + private RayStep[] rays = new RayStep[1] { new RayStep(Vector3.zero, Vector3.zero) }; + public float? ExtentOverride { get { return MaxGazeCollisionDistance; } @@ -125,6 +131,14 @@ public LayerMask[] PrioritizedLayerMasksOverride get { return RaycastLayerMasks; } } + public bool InteractionEnabled + { + get + { + return true; + } + } + private float lastHitDistance = 2.0f; protected override void Awake() @@ -174,7 +188,7 @@ private void UpdateGazeInfo() { if (GazeTransform == null) { - Ray = default(Ray); + Rays[0] = default(RayStep); } else { @@ -189,17 +203,22 @@ private void UpdateGazeInfo() newGazeNormal = Stabilizer.StableRay.direction; } - Ray = new Ray(newGazeOrigin, newGazeNormal); + Rays[0].UpdateRayStep(newGazeOrigin, newGazeOrigin + (newGazeNormal * FocusManager.Instance.GetPointingExtent (this))); } UpdateHitPosition(); } - public void UpdatePointer() + public void OnPreRaycast() { UpdateGazeInfo(); } + public void OnPostRaycast() + { + // Nothing needed + } + public bool OwnsInput(BaseEventData eventData) { // NOTE: This is a simple pointer and not meant to be used simultaneously with others. @@ -221,7 +240,7 @@ public void UpdateHitDetails(FocusDetails focusDetails, RaycastHit hitInfo, bool if (focusDetails.Object != null) { - lastHitDistance = (focusDetails.Point - Ray.origin).magnitude; + lastHitDistance = (focusDetails.Point - Rays[0].origin).magnitude; UpdateHitPosition(); HitNormal = focusDetails.Normal; } @@ -229,7 +248,7 @@ public void UpdateHitDetails(FocusDetails focusDetails, RaycastHit hitInfo, bool private void UpdateHitPosition() { - HitPosition = (Ray.origin + (lastHitDistance * Ray.direction)); + HitPosition = (Rays[0].origin + (lastHitDistance * Rays[0].direction)); } } } diff --git a/Assets/HoloToolkit/Utilities/Scripts/Editor/CustomHeaderDrawer.cs b/Assets/HoloToolkit/Utilities/Scripts/Editor/CustomHeaderDrawer.cs index eccb31c5cff..61c702ca38c 100644 --- a/Assets/HoloToolkit/Utilities/Scripts/Editor/CustomHeaderDrawer.cs +++ b/Assets/HoloToolkit/Utilities/Scripts/Editor/CustomHeaderDrawer.cs @@ -15,18 +15,28 @@ public class CustomHeaderDrawer : DecoratorDrawer { public override float GetHeight() { - return MRTKEditor.ShowCustomEditors ? 0f : 24f; + return (MRTKEditor.ShowCustomEditors && MRTKEditor.CustomEditorActive) ? 0f : 24f; } public override void OnGUI(Rect position) { + if (headerStyle == null) + { + headerStyle = new GUIStyle(EditorStyles.boldLabel); + headerStyle.alignment = TextAnchor.LowerLeft; + } + // If we're using MRDL custom editors, don't show the header - if (MRTKEditor.ShowCustomEditors) + if (MRTKEditor.ShowCustomEditors && MRTKEditor.CustomEditorActive) + { return; + } // Otherwise draw it normally - GUI.Label(position, (base.attribute as HeaderAttribute).header, EditorStyles.boldLabel); + GUI.Label(position, (base.attribute as HeaderAttribute).header, headerStyle); } + + private static GUIStyle headerStyle = null; } #endif diff --git a/Assets/HoloToolkit/Utilities/Scripts/Inspectors/MRTKEditor.cs b/Assets/HoloToolkit/Utilities/Scripts/Inspectors/MRTKEditor.cs index 735181c172f..73a73889b3b 100644 --- a/Assets/HoloToolkit/Utilities/Scripts/Inspectors/MRTKEditor.cs +++ b/Assets/HoloToolkit/Utilities/Scripts/Inspectors/MRTKEditor.cs @@ -28,7 +28,8 @@ public class MRTKEditor : Editor { #region static vars // Toggles custom editors on / off - public static bool ShowCustomEditors = true; + public static bool ShowCustomEditors { get; private set; } + public static bool CustomEditorActive { get; private set; } public static GameObject lastTarget; // Styles @@ -74,21 +75,32 @@ public class MRTKEditor : Editor public override void OnInspectorGUI() { - CreateStyles(); - DrawInspectorHeader(); - Undo.RecordObject(target, target.name); + // Set this to true so + CustomEditorActive = true; - if (ShowCustomEditors) + try { - DrawCustomEditor(); - DrawCustomFooter(); - } - else + CreateStyles(); + DrawInspectorHeader(); + Undo.RecordObject(target, target.name); + + if (ShowCustomEditors) + { + DrawCustomEditor(); + DrawCustomFooter(); + } + else + { + base.DrawDefaultInspector(); + } + + SaveChanges(); + } catch (Exception e) { - base.DrawDefaultInspector(); + DrawError(e.ToString()); } - SaveChanges(); + CustomEditorActive = false; } public void OnSceneGUI()