AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
GrabbableColor.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity.InputModule.Examples.Grabbables
8 {
12  public class GrabbableColor : MonoBehaviour
13  {
14  [Header("Colors")]
15  [SerializeField]
16  private Color colorOnContactSingle = Color.blue;
17 
18  [SerializeField]
19  private Color colorOnContactMulti = Color.cyan;
20 
21  [SerializeField]
22  private Color colorOnGrabSingle = Color.yellow;
23 
24  [SerializeField]
25  private Color colorOnGrabMulti = Color.red;
26 
27  [Header("Objects")]
28  [SerializeField]
29  private Renderer targetRenderer;
30 
31  [SerializeField]
32  private BaseGrabbable grabbable;
33 
34  private Color originalColor;
35  private void Awake()
36  {
37  if (grabbable == null)
38  {
39  grabbable = GetComponent<BaseGrabbable>();
40  }
41 
42  if (targetRenderer == null)
43  {
44  targetRenderer = gameObject.GetComponentInChildren<MeshRenderer>();
45  }
46 
47  originalColor = targetRenderer.material.color;
48  grabbable.OnContactStateChange += RefreshColor;
49  grabbable.OnGrabStateChange += RefreshColor;
50  }
51 
52  private void RefreshColor(BaseGrabbable baseGrab)
53  {
54  Color finalColor = originalColor;
55 
56  switch (baseGrab.ContactState)
57  {
58  case GrabStateEnum.Inactive:
59  break;
60 
61  case GrabStateEnum.Multi:
62  finalColor = colorOnContactMulti;
63  break;
64 
65  case GrabStateEnum.Single:
66  finalColor = colorOnContactSingle;
67  break;
68 
69  default:
70  throw new ArgumentOutOfRangeException();
71  }
72 
73  switch (baseGrab.GrabState)
74  {
75  case GrabStateEnum.Inactive:
76  break;
77 
78  case GrabStateEnum.Multi:
79  finalColor = colorOnGrabMulti;
80  break;
81 
82  case GrabStateEnum.Single:
83  finalColor = colorOnGrabSingle;
84  break;
85 
86  default:
87  throw new ArgumentOutOfRangeException();
88  }
89 
90  targetRenderer.material.color = finalColor;
91  }
92  }
93 }
GrabStateEnum GrabState
Changes based on how many grabbers are grabbing this object
Simple class to change the color of grabbable objects based on state
//Intended Usage// Attach a "grabbable_x" script (a script that inherits from this) to any object tha...
GrabStateEnum ContactState
Changes based on how many grabbers are intersecting with this object