-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathDrawLine.cs
103 lines (93 loc) · 3.07 KB
/
DrawLine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityLibrary
{
public class DrawLine : MonoBehaviour
{
[SerializeField]
protected LineRenderer m_LineRenderer;
[SerializeField]
protected Camera m_Camera;
protected List<Vector3> m_Points;
public virtual LineRenderer lineRenderer {
get {
return m_LineRenderer;
}
}
public virtual new Camera camera {
get {
return m_Camera;
}
}
public virtual List<Vector3> points {
get {
return m_Points;
}
}
protected virtual void Awake()
{
if (m_LineRenderer == null)
{
Debug.LogWarning("DrawLine: Line Renderer not assigned, Adding and Using default Line Renderer.");
CreateDefaultLineRenderer();
}
if (m_Camera == null)
{
Debug.LogWarning("DrawLine: Camera not assigned, Using Main Camera or Creating Camera if main not exists.");
CreateDefaultCamera();
}
m_Points = new List<Vector3>();
}
protected virtual void Update()
{
if (Input.GetMouseButtonDown(0))
{
Reset();
}
if (Input.GetMouseButton(0))
{
var pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, lineRenderer.transform.position.z - m_Camera.transform.position.z);
Vector3 mousePosition = m_Camera.ScreenToWorldPoint(pos);
mousePosition.z = 0;
if (!m_Points.Contains(mousePosition))
{
m_Points.Add(mousePosition);
m_LineRenderer.positionCount = m_Points.Count;
m_LineRenderer.SetPosition(m_LineRenderer.positionCount - 1, mousePosition);
}
}
}
protected virtual void Reset()
{
if (m_LineRenderer != null)
{
m_LineRenderer.positionCount = 0;
}
if (m_Points != null)
{
m_Points.Clear();
}
}
protected virtual void CreateDefaultLineRenderer()
{
m_LineRenderer = gameObject.AddComponent<LineRenderer>();
m_LineRenderer.positionCount = 0;
m_LineRenderer.material = new Material(Shader.Find("Particles/Additive"));
m_LineRenderer.startColor = Color.white;
m_LineRenderer.endColor = Color.white;
m_LineRenderer.startWidth = 0.3f;
m_LineRenderer.endWidth = 0.3f;
m_LineRenderer.useWorldSpace = true;
}
protected virtual void CreateDefaultCamera()
{
m_Camera = Camera.main;
if (m_Camera == null)
{
m_Camera = gameObject.AddComponent<Camera>();
}
m_Camera.orthographic = true;
}
}
}