Initial refactoring efforts in AttachToController

This commit is contained in:
Kurtis Eveleigh 2017-11-03 15:22:39 -07:00
Родитель 9d2dc55ca5
Коммит 2ae49253fe
9 изменённых файлов: 223 добавлений и 215 удалений

Просмотреть файл

@ -35,7 +35,7 @@ GameObject:
m_Component:
- component: {fileID: 4568986355705174}
- component: {fileID: 95955677609272348}
- component: {fileID: 114639197955965534}
- component: {fileID: 114032642240435876}
- component: {fileID: 65853495505084990}
- component: {fileID: 54232508987435032}
m_Layer: 0
@ -87,7 +87,7 @@ Transform:
m_PrefabInternal: {fileID: 100100000}
m_GameObject: {fileID: 1514540217597166}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.1648, y: 0.1045, z: 0}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0.03, y: 0.03, z: 0.03}
m_Children:
- {fileID: 4656808752176476}
@ -275,7 +275,7 @@ Animator:
m_WarningMessage:
m_HasTransformHierarchy: 1
m_AllowConstantClipSamplingOptimization: 1
--- !u!114 &114639197955965534
--- !u!114 &114032642240435876
MonoBehaviour:
m_ObjectHideFlags: 1
m_PrefabParentObject: {fileID: 0}
@ -286,14 +286,18 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: c867040d80ee6c54ba5482735391537d, type: 3}
m_Name:
m_EditorClassIdentifier:
Handedness: 1
element: 5
positionOffset: {x: 0, y: 0, z: 0}
rotationOffset: {x: 0, y: 0, z: 0}
scale: {x: 1, y: 1, z: 1}
setScaleOnAttach: 0
visible: 1
selectorTransform: {fileID: 4679133003569798}
inputScale: 1.1
selectorPosition: {x: 0, y: 0}
selectedColor: {r: 0.9882353, g: 0.9764706, b: 0.9647059, a: 1}
selectedColor: {r: 1, g: 1, b: 1, a: 1}
colorWheelTexture: {fileID: 2800000, guid: e7bc94dceceb4ec4fbb4c9ada0caebd5, type: 3}
colorWheelObject: {fileID: 1580881552183484}
animator: {fileID: 95955677609272348}
timeout: 2
handedness: 1
element: 5

Просмотреть файл

@ -286,6 +286,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 393789416249a224f963f1f8daf39cdc, type: 3}
m_Name:
m_EditorClassIdentifier:
Handedness: 1
element: 6
positionOffset: {x: 0, y: 0, z: 0}
rotationOffset: {x: 0, y: 0, z: 0}
scale: {x: 1, y: 1, z: 1}
setScaleOnAttach: 0
displayParent: {fileID: 4019373725975686}
scaleParent: {fileID: 4867629368935554}
spawnParent: {fileID: 4986731351061256}
@ -322,5 +328,3 @@ MonoBehaviour:
m_PostInfinity: 2
m_RotationOrder: 0
growTime: 2
handedness: 1
element: 6

Просмотреть файл

@ -1,9 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System;
using System.Collections;
using HoloToolkit.Unity.InputModule;
using System;
using UnityEngine;
using UnityEngine.XR.WSA.Input;
@ -14,65 +13,107 @@ namespace HoloToolkit.Unity.Controllers
/// </summary>
public class AttachToController : MonoBehaviour
{
public Action OnAttach;
[Header("AttachToController Elements")]
protected InteractionSourceHandedness handedness = InteractionSourceHandedness.Left;
[SerializeField]
protected MotionControllerInfo.ControllerElementEnum element = MotionControllerInfo.ControllerElementEnum.PointingPose;
[SerializeField]
protected Vector3 positionOffset = Vector3.zero;
[SerializeField]
protected Vector3 rotationOffset = Vector3.zero;
[SerializeField]
protected Vector3 scale = Vector3.one;
[SerializeField]
protected bool setScaleOnAttach = false;
public bool IsAttached { get; private set; }
public Transform Element { get { return elementTransform; } }
private MotionControllerInfo controller;
public MotionControllerInfo Controller { get { return controller; } }
public InteractionSourceHandedness Handedness { set { handedness = value; } }
public event Action<MotionControllerInfo> OnAttachToController;
public event Action<MotionControllerInfo> OnDetachFromController;
[SerializeField]
private Vector3 positionOffset = Vector3.zero;
[SerializeField]
private Vector3 rotationOffset = Vector3.zero;
[SerializeField]
private bool setScaleOnAttach = false;
[SerializeField]
private Vector3 scale = Vector3.one;
[SerializeField]
private InteractionSourceHandedness handedness = InteractionSourceHandedness.Left;
[SerializeField]
private MotionControllerInfo.ControllerElementEnum element = MotionControllerInfo.ControllerElementEnum.PointingPose;
private MotionControllerInfo controller;
private Transform elementTransform;
private IEnumerator Start()
private void OnEnable()
{
// Wait for our controller to appear
while (!MotionControllerVisualizer.Instance.TryGetControllerModel(handedness, out controller))
// Look if the controller has loaded.
if (MotionControllerVisualizer.Instance.TryGetControllerModel(handedness, out controller))
{
yield return null;
AttachElementToController(controller);
}
if (!controller.TryGetElement(element, out elementTransform))
MotionControllerVisualizer.Instance.OnControllerModelLoaded += AttachElementToController;
MotionControllerVisualizer.Instance.OnControllerModelUnloaded += DetachElementFromController;
}
private void OnDisable()
{
if (MotionControllerVisualizer.IsInitialized)
{
Debug.LogError("Unable to find element of type " + element + " under controller " + controller.ControllerParent.name + ", not attaching.");
yield break;
MotionControllerVisualizer.Instance.OnControllerModelLoaded -= AttachElementToController;
MotionControllerVisualizer.Instance.OnControllerModelUnloaded -= DetachElementFromController;
}
}
private void OnDestroy()
{
if (MotionControllerVisualizer.IsInitialized)
{
MotionControllerVisualizer.Instance.OnControllerModelLoaded -= AttachElementToController;
MotionControllerVisualizer.Instance.OnControllerModelUnloaded -= DetachElementFromController;
}
}
private void AttachElementToController(MotionControllerInfo controller)
{
if (!IsAttached)
{
if (!controller.TryGetElement(element, out elementTransform))
{
Debug.LogError("Unable to find element of type " + element + " under controller " + controller.ControllerParent.name + "; not attaching.");
return;
}
// Parent ourselves under the element and set our offsets
transform.parent = elementTransform;
transform.localPosition = positionOffset;
transform.localEulerAngles = rotationOffset;
if (setScaleOnAttach)
{
transform.localScale = scale;
}
// Announce that we're attached
if (OnAttachToController != null)
{
OnAttachToController(controller);
}
IsAttached = true;
}
else
{
Debug.Log(name + " is already attached.");
}
}
private void DetachElementFromController(MotionControllerInfo e)
{
if (OnDetachFromController != null)
{
OnDetachFromController(e);
}
// Parent ourselves under the element and set our offsets
transform.parent = elementTransform;
transform.localPosition = positionOffset;
transform.localEulerAngles = rotationOffset;
if (setScaleOnAttach)
{
transform.localScale = scale;
}
// Announce that we're attached
if (OnAttach != null)
{
OnAttach();
}
yield break;
IsAttached = false;
}
}
}

Просмотреть файл

@ -1,21 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using HoloToolkit.Unity.InputModule;
using HoloToolkit.Unity.Controllers;
using HoloToolkit.Unity.Design;
using HoloToolkit.Unity.InputModule;
using System.Collections;
using UnityEngine;
using UnityEngine.XR.WSA.Input;
namespace HoloToolkit.Unity.ControllerExamples
{
[RequireComponent(typeof(AttachToController))]
public class BrushSelector : MonoBehaviour
{
public enum SwipeEnum
{
None,
Left,
Right,
Right
}
[SerializeField]
@ -214,14 +216,17 @@ namespace HoloToolkit.Unity.ControllerExamples
{
if (obj.state.source.handedness == handedness)
{
if (obj.state.touchpadPressed)
{
// Check which side we clicked
if (obj.state.touchpadPosition.x < 0)
{
currentAction = SwipeEnum.Left;
}
else
{
currentAction = SwipeEnum.Right;
}
// Ping the touchpad material so it gets bright
touchpadTouchTime = Time.unscaledTime;

Просмотреть файл

@ -1,15 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using HoloToolkit.Unity.InputModule;
using HoloToolkit.Unity.Controllers;
using System.Collections;
using HoloToolkit.Unity.InputModule;
using UnityEngine;
using UnityEngine.XR.WSA.Input;
namespace HoloToolkit.Unity.ControllerExamples
{
public class ColorPickerWheel : MonoBehaviour, IPointerTarget
public class ColorPickerWheel : AttachToController, IPointerTarget
{
public bool Visible
{
@ -54,37 +53,20 @@ namespace HoloToolkit.Unity.ControllerExamples
[SerializeField]
private float timeout = 2f;
/*[Header("Color Dabs")]
[SerializeField]
private Color[] colorDabs = new Color[8];*/
private float lastTimeVisible;
private bool visibleLastFrame = false;
[SerializeField]
private InteractionSourceHandedness handedness = InteractionSourceHandedness.Left;
[SerializeField]
private MotionControllerInfo.ControllerElementEnum element = MotionControllerInfo.ControllerElementEnum.Touchpad;
private MotionControllerInfo controller;
public void OnPointerTarget(PhysicsPointer source)
private void Awake()
{
Visible = true;
OnAttachToController += This_OnAttachToController;
OnDetachFromController += This_OnDetachFromController;
// If we're opening or closing, don't set the color value
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
if (stateInfo.IsName("Show") || stateInfo.IsName("Hide"))
{
return;
}
Vector3 localHitPoint = selectorTransform.parent.InverseTransformPoint(source.TargetPoint);
SelectorPosition = new Vector2(localHitPoint.x, localHitPoint.z);
visible = false;
}
private void Update()
{
if (controller == null)
if (Controller == null)
{
return;
}
@ -133,40 +115,39 @@ namespace HoloToolkit.Unity.ControllerExamples
}
}
private IEnumerator Start()
private void This_OnAttachToController(MotionControllerInfo e)
{
while (!MotionControllerVisualizer.Instance.TryGetControllerModel(handedness, out controller))
{
visible = false;
yield return null;
}
// Parent the picker wheel under the element of choice
Transform elementTransform;
if (!controller.TryGetElement(element, out elementTransform))
{
Debug.LogError("Element " + element.ToString() + " not found in controller, can't proceed.");
gameObject.SetActive(false);
yield break;
}
transform.parent = elementTransform;
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
// Subscribe to input now that we're parented under the controller
InteractionManager.InteractionSourceUpdated += InteractionSourceUpdated;
}
private void This_OnDetachFromController(MotionControllerInfo e)
{
// Unsubscribe from input now that we've detached from the controller
InteractionManager.InteractionSourceUpdated -= InteractionSourceUpdated;
}
public void OnPointerTarget(PhysicsPointer source)
{
Visible = true;
// If we're opening or closing, don't set the color value
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
if (stateInfo.IsName("Show") || stateInfo.IsName("Hide"))
{
return;
}
Vector3 localHitPoint = selectorTransform.parent.InverseTransformPoint(source.TargetPoint);
SelectorPosition = new Vector2(localHitPoint.x, localHitPoint.z);
}
private void InteractionSourceUpdated(InteractionSourceUpdatedEventArgs obj)
{
if (obj.state.source.handedness == handedness)
if (obj.state.source.handedness == handedness && obj.state.touchpadTouched)
{
if (obj.state.touchpadTouched)
{
Visible = true;
SelectorPosition = obj.state.touchpadPosition;
}
Visible = true;
SelectorPosition = obj.state.touchpadPosition;
}
}
}

Просмотреть файл

@ -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 HoloToolkit.Unity.Controllers;
using HoloToolkit.Unity.InputModule;
using System;
using System.Collections;
@ -9,19 +10,19 @@ using UnityEngine.XR.WSA.Input;
namespace HoloToolkit.Unity.ControllerExamples
{
public class ObjectSpawner : MonoBehaviour
public class ObjectSpawner : AttachToController
{
public enum StateEnum
{
Uninitialized,
Idle,
Switching,
Spawning,
Spawning
}
public int MeshIndex
{
get { return meshIndex; }
get { return meshIndex; }
set
{
if (state != StateEnum.Idle)
@ -67,58 +68,16 @@ namespace HoloToolkit.Unity.ControllerExamples
[SerializeField]
private float growTime = 2f;
[SerializeField]
private InteractionSourceHandedness handedness = InteractionSourceHandedness.Left;
[SerializeField]
private MotionControllerInfo.ControllerElementEnum element = MotionControllerInfo.ControllerElementEnum.PointingPose;
private MotionControllerInfo controller;
private int meshIndex = 0;
private StateEnum state = StateEnum.Uninitialized;
private Material instantiatedMaterial;
private bool released;
private float timePressed;
private void SpawnObject()
private void Awake()
{
if (state != StateEnum.Idle)
{
return;
}
state = StateEnum.Spawning;
StartCoroutine(SpawnOverTime());
}
private IEnumerator Start()
{
while (!MotionControllerVisualizer.Instance.TryGetControllerModel(handedness, out controller))
{
yield return null;
}
instantiatedMaterial = new Material(objectMaterial);
displayObject.sharedMesh = availableMeshes[meshIndex];
displayObject.GetComponent<Renderer>().sharedMaterial = instantiatedMaterial;
// Parent the picker wheel under the element of choice
Transform elementTransform;
if (!controller.TryGetElement(element, out elementTransform))
{
Debug.LogError("Element " + element.ToString() + " not found in controller, can't proceed.");
gameObject.SetActive(false);
yield break;
}
transform.parent = elementTransform;
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
// Subscribe to input now that we're parented under the controller
InteractionManager.InteractionSourcePressed += InteractionSourcePressed;
InteractionManager.InteractionSourceReleased += InteractionSourceReleased;
state = StateEnum.Idle;
OnAttachToController += This_OnAttachToController;
OnDetachFromController += This_OnDetachFromController;
}
private void Update()
@ -137,6 +96,44 @@ namespace HoloToolkit.Unity.ControllerExamples
instantiatedMaterial.color = colorSource.SelectedColor;
}
private void SpawnObject()
{
if (state != StateEnum.Idle)
{
return;
}
state = StateEnum.Spawning;
StartCoroutine(SpawnOverTime());
}
private void This_OnAttachToController(MotionControllerInfo controllerInfo)
{
gameObject.SetActive(true);
instantiatedMaterial = new Material(objectMaterial);
displayObject.sharedMesh = availableMeshes[meshIndex];
displayObject.GetComponent<Renderer>().sharedMaterial = instantiatedMaterial;
// Subscribe to input now that we're parented under the controller
InteractionManager.InteractionSourcePressed += InteractionSourcePressed;
InteractionManager.InteractionSourceReleased += InteractionSourceReleased;
state = StateEnum.Idle;
}
private void This_OnDetachFromController(MotionControllerInfo controllerInfo)
{
// Unsubscribe from input now that we've detached from the controller
InteractionManager.InteractionSourcePressed -= InteractionSourcePressed;
InteractionManager.InteractionSourceReleased -= InteractionSourceReleased;
Destroy(instantiatedMaterial);
//displayObject.sharedMesh = null;
state = StateEnum.Uninitialized;
}
private IEnumerator SwitchOverTime()
{
animator.SetTrigger("Switch");
@ -225,7 +222,9 @@ namespace HoloToolkit.Unity.ControllerExamples
case InteractionSourcePressType.Select:
meshIndex++;
if (meshIndex >= NumAvailableMeshes)
{
meshIndex = 0;
}
break;
default:

Просмотреть файл

@ -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 HoloToolkit.Unity.InputModule;
using UnityEngine;
using UnityEngine.XR.WSA.Input;
@ -9,23 +10,16 @@ namespace HoloToolkit.Unity.Controllers
/// <summary>
/// Routes controller input to a physics pointer
/// </summary>
[RequireComponent(typeof(AttachToController))]
public class PointerInput : MonoBehaviour
public class PointerInput : AttachToController
{
[SerializeField]
private PhysicsPointer pointer = null;
[SerializeField]
private InteractionSourcePressType activePressType = InteractionSourcePressType.Select;
[SerializeField]
private InteractionSourceHandedness handedness = InteractionSourceHandedness.Left;
private AttachToController attachToController;
private void Awake()
{
attachToController = GetComponent<AttachToController>();
attachToController.Handedness = handedness;
attachToController.OnAttach += OnAttach;
OnAttachToController += This_OnAttachToController;
if (pointer == null)
{
@ -35,7 +29,7 @@ namespace HoloToolkit.Unity.Controllers
pointer.Active = false;
}
private void OnAttach()
private void This_OnAttachToController(MotionControllerInfo controllerInfo)
{
// Subscribe to interaction events
InteractionManager.InteractionSourceUpdated += InteractionSourceUpdated;

Просмотреть файл

@ -155,7 +155,7 @@ Prefab:
type: 2}
propertyPath: colorSource
value:
objectReference: {fileID: 301956525}
objectReference: {fileID: 790017090}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: c0ccc611baa955a4080f09f3434d9d6e, type: 2}
m_IsPrefabParent: 0
@ -218,12 +218,6 @@ Transform:
m_PrefabParentObject: {fileID: 4333231778490866, guid: a71041be74ebc5546895208261bbc326,
type: 2}
m_PrefabInternal: {fileID: 291685366}
--- !u!114 &301956525 stripped
MonoBehaviour:
m_PrefabParentObject: {fileID: 114639197955965534, guid: 56a934385f12cbd4db6dfaa5bdcb9f9f,
type: 2}
m_PrefabInternal: {fileID: 790017089}
m_Script: {fileID: 11500000, guid: c867040d80ee6c54ba5482735391537d, type: 3}
--- !u!1001 &790017089
Prefab:
m_ObjectHideFlags: 0
@ -263,49 +257,15 @@ Prefab:
propertyPath: m_RootOrder
value: 6
objectReference: {fileID: 0}
- target: {fileID: 114639197955965534, guid: 56a934385f12cbd4db6dfaa5bdcb9f9f,
type: 2}
propertyPath: colorDabs.Array.data[0].a
value: 1
objectReference: {fileID: 0}
- target: {fileID: 114639197955965534, guid: 56a934385f12cbd4db6dfaa5bdcb9f9f,
type: 2}
propertyPath: colorDabs.Array.data[1].a
value: 1
objectReference: {fileID: 0}
- target: {fileID: 114639197955965534, guid: 56a934385f12cbd4db6dfaa5bdcb9f9f,
type: 2}
propertyPath: colorDabs.Array.data[2].a
value: 1
objectReference: {fileID: 0}
- target: {fileID: 114639197955965534, guid: 56a934385f12cbd4db6dfaa5bdcb9f9f,
type: 2}
propertyPath: colorDabs.Array.data[3].a
value: 1
objectReference: {fileID: 0}
- target: {fileID: 114639197955965534, guid: 56a934385f12cbd4db6dfaa5bdcb9f9f,
type: 2}
propertyPath: colorDabs.Array.data[4].a
value: 1
objectReference: {fileID: 0}
- target: {fileID: 114639197955965534, guid: 56a934385f12cbd4db6dfaa5bdcb9f9f,
type: 2}
propertyPath: colorDabs.Array.data[5].a
value: 1
objectReference: {fileID: 0}
- target: {fileID: 114639197955965534, guid: 56a934385f12cbd4db6dfaa5bdcb9f9f,
type: 2}
propertyPath: colorDabs.Array.data[6].a
value: 1
objectReference: {fileID: 0}
- target: {fileID: 114639197955965534, guid: 56a934385f12cbd4db6dfaa5bdcb9f9f,
type: 2}
propertyPath: colorDabs.Array.data[7].a
value: 1
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: 56a934385f12cbd4db6dfaa5bdcb9f9f, type: 2}
m_IsPrefabParent: 0
--- !u!114 &790017090 stripped
MonoBehaviour:
m_PrefabParentObject: {fileID: 114032642240435876, guid: 56a934385f12cbd4db6dfaa5bdcb9f9f,
type: 2}
m_PrefabInternal: {fileID: 790017089}
m_Script: {fileID: 11500000, guid: c867040d80ee6c54ba5482735391537d, type: 3}
--- !u!1001 &817233150
Prefab:
m_ObjectHideFlags: 0
@ -518,6 +478,21 @@ Prefab:
propertyPath: m_Enabled
value: 1
objectReference: {fileID: 0}
- target: {fileID: 114668976894459158, guid: 073f5fc18f8dd3f43bfc0a26a586fdca,
type: 2}
propertyPath: End.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 114668976894459158, guid: 073f5fc18f8dd3f43bfc0a26a586fdca,
type: 2}
propertyPath: End.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 114668976894459158, guid: 073f5fc18f8dd3f43bfc0a26a586fdca,
type: 2}
propertyPath: End.z
value: 100
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: 073f5fc18f8dd3f43bfc0a26a586fdca, type: 2}
m_IsPrefabParent: 0
@ -594,7 +569,7 @@ Prefab:
type: 2}
propertyPath: colorPicker
value:
objectReference: {fileID: 301956525}
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: 54018739d95b963469324ffd1b785e1b, type: 2}
m_IsPrefabParent: 0
@ -896,6 +871,11 @@ Prefab:
value:
objectReference: {fileID: 1430356196732642, guid: 58f27e3d07d02114d9feb8eb5448261a,
type: 2}
- target: {fileID: 114895734277317420, guid: fa4aec5413103264c884daee464826d9,
type: 2}
propertyPath: AlwaysUseAlternateRightModel
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: fa4aec5413103264c884daee464826d9, type: 2}
m_IsPrefabParent: 0

Просмотреть файл

@ -1 +1 @@
m_EditorVersion: 2017.2.0f3-MRTP3
m_EditorVersion: 2017.2.0p1-MRTP4