Done: VR Eye UI interaction
Add demo object for VR eye interaction
Showing
10 changed files
with
67 additions
and
95 deletions
This diff could not be displayed because it is too large.
1 | +using System.Collections; | ||
2 | +using System.Collections.Generic; | ||
3 | +using UnityEngine; | ||
4 | +using UnityEngine.UI; | ||
5 | + | ||
6 | +public class ColorChanage : MonoBehaviour { | ||
7 | + | ||
8 | + public Image target; | ||
9 | + | ||
10 | + public void SetWhite() | ||
11 | + { | ||
12 | + target.color = Color.white; | ||
13 | + } | ||
14 | + | ||
15 | + public void SetRed() | ||
16 | + { | ||
17 | + target.color = Color.red; | ||
18 | + } | ||
19 | + | ||
20 | + public void SetRandomColor() | ||
21 | + { | ||
22 | + target.color = Random.ColorHSV(); | ||
23 | + } | ||
24 | +} |
1 | fileFormatVersion: 2 | 1 | fileFormatVersion: 2 |
2 | -guid: 133d0a875b0a248f78d1a42196c9f49a | 2 | +guid: 2d6c2dd67ddb4436cabe16ca3449ade3 |
3 | -timeCreated: 1512274876 | 3 | +timeCreated: 1512280867 |
4 | licenseType: Pro | 4 | licenseType: Pro |
5 | MonoImporter: | 5 | MonoImporter: |
6 | externalObjects: {} | 6 | externalObjects: {} | ... | ... |
... | @@ -118,8 +118,7 @@ public class Gun : MonoBehaviour | ... | @@ -118,8 +118,7 @@ public class Gun : MonoBehaviour |
118 | // 찍어내기(찍어낼 피탄효과 원본, 위치, 회전) | 118 | // 찍어내기(찍어낼 피탄효과 원본, 위치, 회전) |
119 | // Quaternion.LookRotation(방향) => 해당 방향을 보는 쪽으로 회전을 만들어줌 | 119 | // Quaternion.LookRotation(방향) => 해당 방향을 보는 쪽으로 회전을 만들어줌 |
120 | // hit.normal = 충돌각 (정확하게는 충돌한 표면이 바라보는 방향) | 120 | // hit.normal = 충돌각 (정확하게는 충돌한 표면이 바라보는 방향) |
121 | - GameObject impactInstance | 121 | + Instantiate(impactPrefab, hit.point, Quaternion.LookRotation(hit.normal)); |
122 | - = Instantiate(impactPrefab, hit.point, Quaternion.LookRotation(hit.normal)); | ||
123 | 122 | ||
124 | // 만약 상대방이 IDamageable 로서 가져와진다면... | 123 | // 만약 상대방이 IDamageable 로서 가져와진다면... |
125 | // 상대방이 무조건 IDamageable 이 강제하는 | 124 | // 상대방이 무조건 IDamageable 이 강제하는 | ... | ... |
... | @@ -5,7 +5,7 @@ using UnityEngine; | ... | @@ -5,7 +5,7 @@ using UnityEngine; |
5 | // VR 컨트롤러의 인풋을 받아 Gun 을 제어하는 스크립트 | 5 | // VR 컨트롤러의 인풋을 받아 Gun 을 제어하는 스크립트 |
6 | public class GunController : MonoBehaviour { | 6 | public class GunController : MonoBehaviour { |
7 | 7 | ||
8 | - /* VR 입력을 받아 처리해야 하는 클래스는 VRInputController 싱긑톤의 두 함수만 체크 하면 된다 */ | 8 | + /* VR 입력을 받아 처리해야 하는 클래스는 VRInput의 두 함수만 체크 하면 된다 */ |
9 | // 단 두개의 함수 GetGripButton 와 GetTriggerButton | 9 | // 단 두개의 함수 GetGripButton 와 GetTriggerButton |
10 | 10 | ||
11 | public Gun gun; | 11 | public Gun gun; | ... | ... |
1 | -using UnityEngine; | ||
2 | -using UnityEngine.UI; | ||
3 | - | ||
4 | - | ||
5 | -public class Reticle : MonoBehaviour | ||
6 | -{ | ||
7 | - [SerializeField] private float m_DefaultDistance = 5f; // The default distance away from the camera the reticle is placed. | ||
8 | - [SerializeField] private bool m_UseNormal; // Whether the reticle should be placed parallel to a surface. | ||
9 | - [SerializeField] private Image m_Image; // Reference to the image component that represents the reticle. | ||
10 | - [SerializeField] private Transform m_ReticleTransform; // We need to affect the reticle's transform. | ||
11 | - [SerializeField] private Transform m_Camera; // The reticle is always placed relative to the camera. | ||
12 | - | ||
13 | - | ||
14 | - private Vector3 m_OriginalScale; // Since the scale of the reticle changes, the original scale needs to be stored. | ||
15 | - private Quaternion m_OriginalRotation; // Used to store the original rotation of the reticle. | ||
16 | - | ||
17 | - | ||
18 | - public bool UseNormal | ||
19 | - { | ||
20 | - get { return m_UseNormal; } | ||
21 | - set { m_UseNormal = value; } | ||
22 | - } | ||
23 | - | ||
24 | - | ||
25 | - public Transform ReticleTransform { get { return m_ReticleTransform; } } | ||
26 | - | ||
27 | - | ||
28 | - private void Awake() | ||
29 | - { | ||
30 | - // Store the original scale and rotation. | ||
31 | - m_OriginalScale = m_ReticleTransform.localScale; | ||
32 | - m_OriginalRotation = m_ReticleTransform.localRotation; | ||
33 | - } | ||
34 | - | ||
35 | - | ||
36 | - public void Hide() | ||
37 | - { | ||
38 | - m_Image.enabled = false; | ||
39 | - } | ||
40 | - | ||
41 | - | ||
42 | - public void Show() | ||
43 | - { | ||
44 | - m_Image.enabled = true; | ||
45 | - } | ||
46 | - | ||
47 | - | ||
48 | - // This overload of SetPosition is used when the the VREyeRaycaster hasn't hit anything. | ||
49 | - public void SetPosition () | ||
50 | - { | ||
51 | - // Set the position of the reticle to the default distance in front of the camera. | ||
52 | - m_ReticleTransform.position = m_Camera.position + m_Camera.forward * m_DefaultDistance; | ||
53 | - | ||
54 | - // Set the scale based on the original and the distance from the camera. | ||
55 | - m_ReticleTransform.localScale = m_OriginalScale * m_DefaultDistance; | ||
56 | - | ||
57 | - // The rotation should just be the default. | ||
58 | - m_ReticleTransform.localRotation = m_OriginalRotation; | ||
59 | - } | ||
60 | - | ||
61 | - | ||
62 | - // This overload of SetPosition is used when the VREyeRaycaster has hit something. | ||
63 | - public void SetPosition (RaycastHit hit) | ||
64 | - { | ||
65 | - m_ReticleTransform.position = hit.point; | ||
66 | - m_ReticleTransform.localScale = m_OriginalScale * hit.distance; | ||
67 | - | ||
68 | - // If the reticle should use the normal of what has been hit... | ||
69 | - if (m_UseNormal) | ||
70 | - // ... set it's rotation based on it's forward vector facing along the normal. | ||
71 | - m_ReticleTransform.rotation = Quaternion.FromToRotation (Vector3.forward, hit.normal); | ||
72 | - else | ||
73 | - // However if it isn't using the normal then it's local rotation should be as it was originally. | ||
74 | - m_ReticleTransform.localRotation = m_OriginalRotation; | ||
75 | - } | ||
76 | -} |
... | @@ -9,11 +9,10 @@ public class VREyeRaycaster : MonoBehaviour | ... | @@ -9,11 +9,10 @@ public class VREyeRaycaster : MonoBehaviour |
9 | 9 | ||
10 | [SerializeField] private Transform m_Camera; | 10 | [SerializeField] private Transform m_Camera; |
11 | [SerializeField] public LayerMask m_ExclusionLayers; // Layers to exclude from the raycast. | 11 | [SerializeField] public LayerMask m_ExclusionLayers; // Layers to exclude from the raycast. |
12 | - [SerializeField] private Reticle m_Reticle; // The reticle, if applicable. | ||
13 | [SerializeField] private float m_RayLength = 500f; | 12 | [SerializeField] private float m_RayLength = 500f; |
14 | [SerializeField] private bool m_ShowDebugRay; // Optionally show the debug ray. [SerializeField] private float m_RayLength = 500f; // How far into the scene the ray is cast. | 13 | [SerializeField] private bool m_ShowDebugRay; // Optionally show the debug ray. [SerializeField] private float m_RayLength = 500f; // How far into the scene the ray is cast. |
15 | 14 | ||
16 | - | 15 | + |
17 | private VRInteratable m_CurrentInteractible; //The current interactive item | 16 | private VRInteratable m_CurrentInteractible; //The current interactive item |
18 | private VRInteratable m_LastInteractible; //The last interactive item | 17 | private VRInteratable m_LastInteractible; //The last interactive item |
19 | 18 | ||
... | @@ -21,6 +20,16 @@ public class VREyeRaycaster : MonoBehaviour | ... | @@ -21,6 +20,16 @@ public class VREyeRaycaster : MonoBehaviour |
21 | void Update() | 20 | void Update() |
22 | { | 21 | { |
23 | EyeRaycast(); | 22 | EyeRaycast(); |
23 | + | ||
24 | + if(VRInput.GetTriggerButton(VRInput.Hand.Right)) | ||
25 | + { | ||
26 | + // Xbox 나 VR 컨트롤러의 'A' 키를 누르면 인터렉터블 오브젝트의 OnClick 이벤트를 발동시킴 | ||
27 | + // TODO: VR 컨트롤러의 트리거 버튼 클릭으로 대체 | ||
28 | + if(m_CurrentInteractible && Input.GetButtonDown("Fire1")) | ||
29 | + { | ||
30 | + m_CurrentInteractible.OnClick(); | ||
31 | + } | ||
32 | + } | ||
24 | } | 33 | } |
25 | 34 | ||
26 | 35 | ||
... | @@ -50,22 +59,18 @@ public class VREyeRaycaster : MonoBehaviour | ... | @@ -50,22 +59,18 @@ public class VREyeRaycaster : MonoBehaviour |
50 | m_LastInteractible = interactible; | 59 | m_LastInteractible = interactible; |
51 | } | 60 | } |
52 | 61 | ||
53 | - | ||
54 | - // Something was hit, set at the hit position. | ||
55 | - if (m_Reticle) | ||
56 | - m_Reticle.SetPosition(hit); | ||
57 | - | ||
58 | if (OnRaycasthit != null) | 62 | if (OnRaycasthit != null) |
59 | OnRaycasthit(hit); | 63 | OnRaycasthit(hit); |
60 | } | 64 | } |
61 | else | 65 | else |
62 | { | 66 | { |
67 | + if(m_LastInteractible && m_CurrentInteractible) | ||
68 | + { | ||
69 | + m_LastInteractible.OnVREyeExit(); | ||
70 | + } | ||
71 | + | ||
63 | m_LastInteractible = null; | 72 | m_LastInteractible = null; |
64 | m_CurrentInteractible = null; | 73 | m_CurrentInteractible = null; |
65 | - | ||
66 | - // Position the reticle at default distance. | ||
67 | - if (m_Reticle) | ||
68 | - m_Reticle.SetPosition(); | ||
69 | } | 74 | } |
70 | } | 75 | } |
71 | } | 76 | } | ... | ... |
... | @@ -6,18 +6,26 @@ using UnityEngine.Events; | ... | @@ -6,18 +6,26 @@ using UnityEngine.Events; |
6 | public class VRInteratable : MonoBehaviour { | 6 | public class VRInteratable : MonoBehaviour { |
7 | 7 | ||
8 | [SerializeField] | 8 | [SerializeField] |
9 | - private UnityEvent onVRTriggerClick; | 9 | + private UnityEvent onClick; |
10 | 10 | ||
11 | [SerializeField] | 11 | [SerializeField] |
12 | private UnityEvent onVREyeEnter; | 12 | private UnityEvent onVREyeEnter; |
13 | 13 | ||
14 | - public void OnTriggerClick() | 14 | + [SerializeField] |
15 | + private UnityEvent onVREyeExit; | ||
16 | + | ||
17 | + public void OnClick() | ||
15 | { | 18 | { |
16 | - onVRTriggerClick.Invoke(); | 19 | + onClick.Invoke(); |
17 | } | 20 | } |
18 | 21 | ||
19 | public void OnVREyeEnter() | 22 | public void OnVREyeEnter() |
20 | { | 23 | { |
21 | onVREyeEnter.Invoke(); | 24 | onVREyeEnter.Invoke(); |
22 | } | 25 | } |
26 | + | ||
27 | + public void OnVREyeExit() | ||
28 | + { | ||
29 | + onVREyeExit.Invoke(); | ||
30 | + } | ||
23 | } | 31 | } | ... | ... |
-
Please register or login to post a comment