-
Notifications
You must be signed in to change notification settings - Fork 12
/
PathCreater.cs
100 lines (84 loc) · 2.67 KB
/
PathCreater.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathCreater : MonoBehaviour
{
[SerializeField]
int Num_Path = 2;
[SerializeField]
List<GameObject> ShowPos = new List<GameObject>();
[SerializeField]
List<Vector2> Path = new List<Vector2>();
public int Count => Path.Count;
public Vector2 GetPath(int ID = 0){
if(ID < 0 || ID >= Path.Count){
Debug.LogError("ID invalid! "+this.gameObject.name);
return Vector2.zero;
}
return Path[ID];
}
public void SetPath(int ID,Vector2 pos){
if(ID < 0 || ID >= Path.Count){
Debug.LogError("ID invalid!");
return;
}
Path[ID] = pos;
}
#if UNITY_EDITOR
void OnDrawGizmosSelected(){
if(ShowPos.Count < Num_Path){
GameObject g = new GameObject();
ShowPos.Add(g);
return;
}
Gizmos.color = Color.yellow;
for(int i = 0; i<ShowPos.Count; i++){
if(ShowPos[i] == null)
ShowPos[i] = new GameObject();
ShowPos[i].transform.position = Path[i];
Gizmos.DrawWireSphere(ShowPos[i].transform.position,0.5f);
}
}
void OnDrawGizmos(){
if(Path.Count <= 0){
for(int i = 0; i<Num_Path; i++){
Path.Add((Vector2)this.transform.position + Vector2.up);
}
}
Gizmos.color = Color.yellow;
for(int i = 0; i<Path.Count-1; i++){
Gizmos.DrawLine(Path[i],Path[i+1]);
}
if (CheckSelect()){
for(int i = 0; i<ShowPos.Count; i++){
if (ShowPos[i] == null )
continue;
if (UnityEditor.Selection.activeGameObject == ShowPos[i]){
Path[i] = ShowPos[i].transform.position;
}else{
ShowPos[i].transform.position = Path[i];
}
Gizmos.DrawWireSphere(ShowPos[i].transform.position,0.5f);
}
return;
}
if(ShowPos.Count == 0)
return;
for(int i = 0; i<ShowPos.Count; i++){
DestroyImmediate(ShowPos[i]);
}
ShowPos = new List<GameObject>();
}
bool CheckSelect(){
if (UnityEditor.Selection.activeGameObject == this.gameObject)
return true;
if(ShowPos.Count == 0)
return false;
foreach(GameObject G in ShowPos){
if(UnityEditor.Selection.activeGameObject == G)
return true;
}
return false;
}
#endif
}