AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SizeToRectTransform.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 {
25  [ExecuteInEditMode]
26  public class SizeToRectTransform : MonoBehaviour
27  {
28  [Tooltip("The RectTransform that will drive the size and position of this object")]
29  public RectTransform ParentRectTransform;
30 
31  [Tooltip("The pixel ratio conversion to Unity Units or scale")]
32  public float ScaleFactor = 2048;
33 
34  [Tooltip("Add depth, a value RectTransforms do not support at this time")]
35  public float Depth = 10;
36 
37  [Tooltip("The x and y margins")]
38  public Vector2 EdgeOffset;
39 
40  private RectTransform mRectTransform;
41 
45  private void Awake()
46  {
47  if (ParentRectTransform == null)
48  {
49  ParentRectTransform = transform.parent.GetComponent<RectTransform>();
50  }
51 
52  if (ParentRectTransform == null)
53  {
54  Debug.LogError("The parent of " + name + "does not have a RectTransform!");
55  }
56 
57  mRectTransform = GetComponent<RectTransform>();
58  }
59 
63  private void UpdateScale()
64  {
65  if (ParentRectTransform != null)
66  {
67  if (mRectTransform == null)
68  {
69  transform.localScale = new Vector3((ParentRectTransform.rect.width - EdgeOffset.x) / ScaleFactor, (ParentRectTransform.rect.height - EdgeOffset.y) / ScaleFactor, Depth / ScaleFactor);
70  }
71  else
72  {
73  mRectTransform.sizeDelta = new Vector2(ParentRectTransform.rect.width - EdgeOffset.x, ParentRectTransform.rect.height - EdgeOffset.y);
74  }
75  }
76  }
77 
78  // Update is called once per frame
79  void Update()
80  {
81  UpdateScale();
82  }
83  }
84 }
A sizing and layout system for RectTransforms that help in building and laying out UI ...