AR + GPS Location  3.0.0
All Classes Namespaces Functions Variables Properties Events Pages
DebugDistance.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using System.Globalization;
4 using UnityEngine;
5 using UnityEngine.Rendering;
6 
7 namespace ARLocation.UI
8 {
9  public class DebugDistance : MonoBehaviour
10  {
11  private LineRenderer lineRenderer;
12  private Camera mainCamera;
13  private TextMesh textMesh;
14  private GameObject textMeshGo;
15  private ARLocationManager arLocationManager;
16  private bool hasArLocationManager;
17 
18  // Start is called before the first frame update
19  void Start()
20  {
21  mainCamera = ARLocationManager.Instance.MainCamera;
22  lineRenderer = GetComponent<LineRenderer>();
23  arLocationManager = ARLocationManager.Instance;
24  hasArLocationManager = arLocationManager != null;
25 
26  if (!lineRenderer)
27  {
28  lineRenderer = gameObject.AddComponent<LineRenderer>();
29 
30  var shader = Shader.Find("Unlit/Color");
31  if (shader)
32  {
33  lineRenderer.material = new Material(shader)
34  {
35  color = new Color(0.3960f, 0.6901f, 0.9725f)
36  };
37  }
38  }
39 
40  lineRenderer.useWorldSpace = true;
41  lineRenderer.alignment = LineAlignment.View;
42  lineRenderer.receiveShadows = false;
43  lineRenderer.shadowCastingMode = ShadowCastingMode.Off;
44  lineRenderer.allowOcclusionWhenDynamic = false;
45  lineRenderer.positionCount = 2;
46  lineRenderer.startWidth = 0.1f;
47  lineRenderer.endWidth = 0.1f;
48 
49  textMeshGo = new GameObject(gameObject.name + "_text");
50  textMeshGo.transform.localScale = new Vector3(0.05f, 0.05f, 0.05f);
51  textMesh = textMeshGo.AddComponent<TextMesh>();
52  textMesh.fontSize = 100;
53  }
54  void Update()
55  {
56 
57  var floorLevel = hasArLocationManager ? arLocationManager.CurrentGroundY : -ARLocation.Config.InitialGroundHeightGuess;
58  var startPos = MathUtils.SetY(mainCamera.transform.position, floorLevel);
59  var endPos = MathUtils.SetY(transform.position, floorLevel);
60 
61  var lineDir = (endPos - startPos).normalized;
62 
63  lineRenderer.SetPosition(0, startPos);
64  lineRenderer.SetPosition(1, endPos);
65 
66  var textPos = startPos + lineDir * 6.0f;
67 
68  textMeshGo.transform.position = textPos;
69  textMeshGo.transform.LookAt(endPos, new Vector3(0, 1, 0));
70  textMeshGo.transform.Rotate(90, 90, 0);
71  textMesh.text = Vector3.Distance(startPos, endPos).ToString("0.00", CultureInfo.InvariantCulture) + "m";
72  }
73  }
74 
75 }
ARLocation.ARLocationManager
This Component manages all positioned GameObjects, synchronizing their world position in the scene wi...
Definition: ARLocationManager.cs:30
ARLocation.UI.DebugDistance
Definition: DebugDistance.cs:10
ARLocation.Utils.Singleton.Instance
static T Instance
Access singleton instance through this propriety.
Definition: Singleton.cs:18
ARLocation.UI
Definition: ARTrackingInfo.cs:5
ARLocation.ARLocationManager.CurrentGroundY
float CurrentGroundY
Returns the Y world-coordinate of the detected plane which is nearest to the user/camera.
Definition: ARLocationManager.cs:66
ARLocation
Definition: ARLocationConfigInspector.cs:7