AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
PanelTransformPosition.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.Collections;
5 using System.Collections.Generic;
6 using UnityEngine;
7 
8 namespace HoloToolkit.Examples.Prototyping
9 {
19  [ExecuteInEditMode]
20  public class PanelTransformPosition : MonoBehaviour
21  {
22  // alignment choices, sets the center of the transform to the chosen position of the anchor
23  public enum AlignmentTypes { None, TopLeft, Top, TopRight, CenterLeft, Center, CenterRight, BottomLeft, Bottom, BottomRight }
24 
25  [Tooltip("Where to set this object's center point in relation to the Anchor's center point")]
27 
28  [Tooltip("A pixel to Unity unit conversion, Default: 2048x2048 pixels covers a 1x1 Unity Unit or default primitive size")]
29  public float BasePixelSize = 2048;
30 
31  [Tooltip("The transform this object should be linked and aligned to")]
32  public Transform Anchor;
33 
34  [Tooltip("Offset this object's position based on the same pixel based size ratio")]
35  public Vector3 AnchorOffset;
36 
37  [Tooltip("Ignore the anchor's z scaling when positioning this object")]
38  public bool IgnoreZScale;
39 
40  protected Vector3 mAnchorScale;
41  protected Vector3 mAnchorPosition;
42 
46  protected virtual void Awake()
47  {
48  if (Anchor == null)
49  {
50  Anchor = this.transform;
51  }
52  }
53 
57  protected virtual void SetScale()
58  {
59  mAnchorScale = Anchor.localScale;
60  mAnchorPosition = Anchor.localPosition;
61  }
62 
66  protected virtual void UpdatePosition()
67  {
68  SetScale();
69 
70  // set the default directions
71  Vector3 horizontalVector = Vector3.right;
72  Vector3 verticalVector = Vector3.up;
73  Vector3 startPosition = mAnchorPosition;
74 
75  // set the offset distances
76  float xMagnitude = AnchorOffset.x / BasePixelSize;
77  float yMagnitude = AnchorOffset.y / BasePixelSize;
78  float zMagnitude = AnchorOffset.z / BasePixelSize;
79 
80  // set the default position
81  if (Alignment == AlignmentTypes.None)
82  {
83 
84  Vector3 newPosition = startPosition + horizontalVector * xMagnitude + verticalVector * yMagnitude + Vector3.forward * zMagnitude;
85  transform.localPosition = newPosition;
86  return;
87  }
88 
89  // update the directions and anchor alignment position based on the alignment setting
90  switch (Alignment)
91  {
92  case AlignmentTypes.TopLeft:
93  horizontalVector = Vector3.right;
94  verticalVector = Vector3.down;
95  startPosition.x = mAnchorPosition.x - mAnchorScale.x * 0.5f;
96  startPosition.y = mAnchorPosition.y + mAnchorScale.y * 0.5f;
97  break;
98  case AlignmentTypes.Top:
99  horizontalVector = Vector3.right;
100  verticalVector = Vector3.down;
101  startPosition.x = mAnchorPosition.x;
102  startPosition.y = mAnchorPosition.y + mAnchorScale.y * 0.5f;
103  break;
104  case AlignmentTypes.TopRight:
105  horizontalVector = Vector3.left;
106  verticalVector = Vector3.down;
107  startPosition.x = mAnchorPosition.x + mAnchorScale.x * 0.5f;
108  startPosition.y = mAnchorPosition.y + mAnchorScale.y * 0.5f;
109  break;
110  case AlignmentTypes.CenterLeft:
111  horizontalVector = Vector3.right;
112  verticalVector = Vector3.up;
113  startPosition.x = mAnchorPosition.x - mAnchorScale.x * 0.5f;
114  startPosition.y = mAnchorPosition.y;
115  break;
116  case AlignmentTypes.Center:
117  horizontalVector = Vector3.right;
118  verticalVector = Vector3.up;
119  startPosition.x = mAnchorPosition.x;
120  startPosition.y = mAnchorPosition.y;
121  break;
122  case AlignmentTypes.CenterRight:
123  horizontalVector = Vector3.left;
124  verticalVector = Vector3.up;
125  startPosition.x = mAnchorPosition.x + mAnchorScale.x * 0.5f;
126  startPosition.y = mAnchorPosition.y;
127  break;
128  case AlignmentTypes.BottomLeft:
129  horizontalVector = Vector3.right;
130  verticalVector = Vector3.up;
131  startPosition.x = mAnchorPosition.x - mAnchorScale.x * 0.5f;
132  startPosition.y = mAnchorPosition.y - mAnchorScale.y * 0.5f;
133  break;
134  case AlignmentTypes.Bottom:
135  horizontalVector = Vector3.right;
136  verticalVector = Vector3.up;
137  startPosition.x = mAnchorPosition.x;
138  startPosition.y = mAnchorPosition.y - mAnchorScale.y * 0.5f;
139  break;
140  case AlignmentTypes.BottomRight:
141  horizontalVector = Vector3.left;
142  verticalVector = Vector3.up;
143  startPosition.x = mAnchorPosition.x + mAnchorScale.x * 0.5f;
144  startPosition.y = mAnchorPosition.y - mAnchorScale.y * 0.5f;
145  break;
146  default:
147  break;
148  }
149 
150  // set the aligned position
151  if (!IgnoreZScale)
152  {
153  startPosition.z = mAnchorPosition.z - mAnchorScale.z * 0.5f;
154  }
155 
156  transform.localPosition = startPosition + horizontalVector * xMagnitude + verticalVector * yMagnitude + Vector3.forward * zMagnitude;
157  }
158 
159  // Update is called once per frame
160  protected virtual void Update()
161  {
162  if (Anchor != null)
163  {
164  UpdatePosition();
165 
166  }
167  }
168  }
169 }
virtual void UpdatePosition()
Set this object's position
Takes size values, similar to RectTransorm position values or pixel values used in designer tools and...
virtual void SetScale()
Get the scale and position of the anchor
virtual void Awake()
A transform is required for alignment