Astar_demo/Assets/Vertex.cs
2023-11-28 11:29:52 +05:30

181 lines
5.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class Vertex : MonoBehaviour
{
public bool isSelected;
public List<Vertex> neighbours;
public Vertex assignNeighbour;
[EasyButtons.Button]
void bindSelected(){
bindSelected();
}
[EasyButtons.Button]
void addNeighbour(){
AddNeighbour();
}
[MenuItem("Vertex/bind_selected #b")]
static void bind_selected()
{
List<Vertex> selectedVerticies = new List<Vertex>();
foreach (Object obj in Selection.objects)
{
try
{
selectedVerticies.Add(((GameObject)obj).GetComponent<Vertex>());
}
catch
{
}
}
foreach (Vertex vertex in selectedVerticies)
{
foreach (Vertex newNeighbour in selectedVerticies)
{
if (newNeighbour == vertex) { continue; }
if (!vertex.neighbours.Contains(newNeighbour)) { vertex.neighbours.Add(newNeighbour); }
if (!newNeighbour.neighbours.Contains(vertex)) { newNeighbour.neighbours.Add(vertex); }
}
}
}
[MenuItem("Vertex/unbind_selected #u")]
static void unbind_selected()
{
List<Vertex> selectedVerticies = new List<Vertex>();
foreach (Object obj in Selection.objects)
{
try
{
selectedVerticies.Add(((GameObject)obj).GetComponent<Vertex>());
}
catch
{
}
}
foreach (Vertex vertex in selectedVerticies)
{
foreach (Vertex newNeighbour in selectedVerticies)
{
if (newNeighbour == vertex) { continue; }
if (vertex.neighbours.Contains(newNeighbour)) { vertex.neighbours.Remove(newNeighbour); }
if (newNeighbour.neighbours.Contains(vertex)) { newNeighbour.neighbours.Remove(vertex); }
}
}
}
[MenuItem("Vertex/add_neighbour #e")]
static void AddNeighbour()
{
List<Vertex> selectedVerticies = new List<Vertex>();
foreach (Object obj in Selection.objects)
{
Debug.Log(obj.name);
try
{
selectedVerticies.Add(((GameObject)obj).GetComponent<Vertex>());
}
catch
{
}
}
if (selectedVerticies.Count == 1)
{
GameObject go = Instantiate(selectedVerticies[0].gameObject, selectedVerticies[0].transform.position, Quaternion.identity);
if (selectedVerticies[0].neighbours == null) { selectedVerticies[0].neighbours = new List<Vertex>(); }
selectedVerticies[0].neighbours.Add(go.GetComponent<Vertex>());
go.GetComponent<Vertex>().neighbours = new List<Vertex>();
go.GetComponent<Vertex>().neighbours.Add(selectedVerticies[0]);
go.transform.parent = selectedVerticies[0].transform.parent;
go.transform.name = selectedVerticies[0].transform.name;
UnityEditor.Selection.activeGameObject = go;
}
else if (selectedVerticies.Count > 1)
{
Object[] selections = new Object[selectedVerticies.Count];
for (int i = 0; i < selectedVerticies.Count; i++)
{
GameObject go = Instantiate(selectedVerticies[i].gameObject, selectedVerticies[i].transform.position, Quaternion.identity);
if (selectedVerticies[i].neighbours == null) { selectedVerticies[i].neighbours = new List<Vertex>(); }
selectedVerticies[i].neighbours.Add(go.GetComponent<Vertex>());
go.GetComponent<Vertex>().neighbours = new List<Vertex>();
go.GetComponent<Vertex>().neighbours.Add(selectedVerticies[i]);
go.transform.parent = selectedVerticies[i].transform.parent;
go.transform.name = selectedVerticies[i].transform.name;
selections[i] = go;
}
Selection.objects = selections;
}
else
{
Debug.LogError("Please select single vertex to add new neighbour!");
}
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnDrawGizmos()
{
isSelected = false;
foreach (Object obj in UnityEditor.Selection.objects)
{
if ((GameObject)obj == gameObject)
{
isSelected = true;
break;
}
}
Gizmos.color = (isSelected) ? Color.green : Color.red;
Gizmos.DrawSphere(transform.position, 0.1f);
foreach (Vertex neighbour in neighbours)
{
if (neighbour == null)
{
neighbours.Remove(neighbour);
}
if (!neighbour.neighbours.Contains(this))
{
neighbour.neighbours.Add(this);
}
Gizmos.color = (isSelected || neighbour.isSelected) ? Color.green : Color.red; Gizmos.DrawLine(transform.position, neighbour.transform.position);
}
}
void OnValidate()
{
if (assignNeighbour != null)
{
if (!neighbours.Contains(assignNeighbour)) { neighbours.Add(assignNeighbour); }
if (!assignNeighbour.neighbours.Contains(this)) { assignNeighbour.neighbours.Add(this); }
assignNeighbour = null;
}
}
}