feat: better editor
This commit is contained in:
parent
a44ccf595e
commit
b7d38dcbb9
2 changed files with 2545 additions and 16863 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -1,5 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Dynamic;
|
||||||
using Unity.VisualScripting;
|
using Unity.VisualScripting;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
@ -12,11 +13,9 @@ public class GameManager : MonoBehaviour
|
||||||
[HideInInspector]
|
[HideInInspector]
|
||||||
public static GameManager instance;
|
public static GameManager instance;
|
||||||
|
|
||||||
[SerializeField]
|
public Transform NodeParent;
|
||||||
private Transform NodeParent;
|
public GameObject NodePrefab;
|
||||||
|
|
||||||
[SerializeField]
|
|
||||||
private GameObject NodePrefab;
|
|
||||||
|
|
||||||
[Header("Node Generation")]
|
[Header("Node Generation")]
|
||||||
|
|
||||||
|
|
@ -29,11 +28,29 @@ public class GameManager : MonoBehaviour
|
||||||
[Range(0, 100)]
|
[Range(0, 100)]
|
||||||
public float hoverRadius = 50;
|
public float hoverRadius = 50;
|
||||||
|
|
||||||
|
[Space]
|
||||||
|
[Range(0,10)]
|
||||||
|
public int editorConnectionAllowedWidth = 3;
|
||||||
|
public Color editorConnectionAllowedColor = Color.white;
|
||||||
|
|
||||||
|
[Space]
|
||||||
|
[Range(0, 10)]
|
||||||
|
public int editorConnectionHoveredWidth = 2;
|
||||||
|
public Color editorConnectionHoveredColor = Color.gray;
|
||||||
|
|
||||||
|
[Space]
|
||||||
|
[Range(0,10)]
|
||||||
|
public int editorConnectionForbiddenWidth = 1;
|
||||||
|
public Color editorConnectionForbiddenColor = Color.black;
|
||||||
|
|
||||||
|
|
||||||
public List<Connection> connections = new List<Connection>();
|
|
||||||
private List<Node> nodes = new List<Node>();
|
private List<Node> nodes = new List<Node>();
|
||||||
|
|
||||||
public struct Connection
|
[SerializeField]
|
||||||
|
public List<Connection> connections = new List<Connection>();
|
||||||
|
|
||||||
|
[Serializable]
|
||||||
|
public class Connection
|
||||||
{
|
{
|
||||||
public Node nodeA, nodeB;
|
public Node nodeA, nodeB;
|
||||||
public bool allowed;
|
public bool allowed;
|
||||||
|
|
@ -48,7 +65,6 @@ public class GameManager : MonoBehaviour
|
||||||
DestroyImmediate(NodeParent.GetChild(i).gameObject);
|
DestroyImmediate(NodeParent.GetChild(i).gameObject);
|
||||||
|
|
||||||
float radius = 20f;
|
float radius = 20f;
|
||||||
|
|
||||||
float goldenRatio = (1f + Mathf.Sqrt(5f)) / 2f;
|
float goldenRatio = (1f + Mathf.Sqrt(5f)) / 2f;
|
||||||
float angleIncrement = 2f * Mathf.PI * goldenRatio;
|
float angleIncrement = 2f * Mathf.PI * goldenRatio;
|
||||||
|
|
||||||
|
|
@ -73,7 +89,7 @@ public class GameManager : MonoBehaviour
|
||||||
public void GenerateConnections()
|
public void GenerateConnections()
|
||||||
{
|
{
|
||||||
connections.Clear();
|
connections.Clear();
|
||||||
|
|
||||||
foreach (Node nodeA in nodes)
|
foreach (Node nodeA in nodes)
|
||||||
{
|
{
|
||||||
if (nodeA == null) continue;
|
if (nodeA == null) continue;
|
||||||
|
|
@ -97,107 +113,100 @@ public class GameManager : MonoBehaviour
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
||||||
void Start()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
void OnDrawGizmos()
|
[CustomEditor(typeof(GameManager))]
|
||||||
|
public class GameManagerEditor : Editor
|
||||||
|
{
|
||||||
|
int clickedConIdx = -1;
|
||||||
|
|
||||||
|
public override void OnInspectorGUI()
|
||||||
|
{
|
||||||
|
DrawDefaultInspector();
|
||||||
|
|
||||||
|
GameManager gm = (GameManager)target;
|
||||||
|
GUILayout.BeginHorizontal();
|
||||||
|
|
||||||
|
if (GUILayout.Button("Generate Sphere"))
|
||||||
|
gm.GenerateAlongSphere();
|
||||||
|
|
||||||
|
if (GUILayout.Button("Generate Connections"))
|
||||||
|
gm.GenerateConnections();
|
||||||
|
|
||||||
|
GUILayout.EndHorizontal();
|
||||||
|
|
||||||
|
if (GUILayout.Button("Generate"))
|
||||||
|
{
|
||||||
|
gm.GenerateAlongSphere();
|
||||||
|
gm.GenerateConnections();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void OnSceneGUI()
|
||||||
{
|
{
|
||||||
if (connections == null || SceneView.lastActiveSceneView?.camera == null) return;
|
Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual;
|
||||||
|
|
||||||
|
GameManager gm = (GameManager)target;
|
||||||
|
if (gm.connections == null || gm.connections.Count == 0) return;
|
||||||
Camera cam = SceneView.lastActiveSceneView.camera;
|
Camera cam = SceneView.lastActiveSceneView.camera;
|
||||||
|
if (cam == null) return;
|
||||||
|
|
||||||
// Maus Screen-Koordinaten
|
Vector2 mouse = Event.current.mousePosition;
|
||||||
Vector2 guiPos = Event.current.mousePosition;
|
mouse.y = cam.pixelHeight - mouse.y;
|
||||||
guiPos.y = cam.pixelHeight - guiPos.y;
|
Vector3 mousePos = new Vector3(mouse.x, mouse.y, 0);
|
||||||
Vector3 mousePos = new Vector3(guiPos.x, guiPos.y, 0);
|
|
||||||
|
|
||||||
int closestIndex = -1;
|
int closestIdx = -1;
|
||||||
float closestDist = float.MaxValue;
|
float closestDist = float.MaxValue;
|
||||||
|
|
||||||
// Erster Durchlauf: kleinste Distanz merken
|
// Hover-Ermittlung
|
||||||
for (int i = 0; i < connections.Count; i++)
|
for (int i = 0; i < gm.connections.Count; i++)
|
||||||
{
|
{
|
||||||
var con = connections[i];
|
var con = gm.connections[i];
|
||||||
Vector3 a = cam.WorldToScreenPoint(con.nodeA.transform.position);
|
Vector3 a = cam.WorldToScreenPoint(con.nodeA.transform.position);
|
||||||
Vector3 b = cam.WorldToScreenPoint(con.nodeB.transform.position);
|
Vector3 b = cam.WorldToScreenPoint(con.nodeB.transform.position);
|
||||||
|
|
||||||
// Punkt-zu-Segment Abstand statt Dreiecksformel
|
|
||||||
Vector3 ab = b - a;
|
Vector3 ab = b - a;
|
||||||
Vector3 am = mousePos - a;
|
Vector3 am = mousePos - a;
|
||||||
float t = Mathf.Clamp01(Vector3.Dot(am, ab) / ab.sqrMagnitude);
|
float t = Mathf.Clamp01(Vector3.Dot(am, ab) / ab.sqrMagnitude);
|
||||||
Vector3 proj = a + t * ab;
|
Vector3 proj = a + t * ab;
|
||||||
float dist = Vector3.Distance(mousePos, proj);
|
float dist = Vector3.Distance(mousePos, proj);
|
||||||
|
|
||||||
if (dist < closestDist)
|
if (dist < closestDist && dist < gm.hoverRadius)
|
||||||
{
|
{
|
||||||
closestDist = dist;
|
closestDist = dist;
|
||||||
closestIndex = i;
|
closestIdx = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zweiter Durchlauf: zeichnen
|
// Klick-Behandlung
|
||||||
for (int i = 0; i < connections.Count; i++)
|
Event e = Event.current;
|
||||||
|
if (e.type == EventType.MouseDown && e.button == 0 && closestIdx >= 0)
|
||||||
{
|
{
|
||||||
var con = connections[i];
|
clickedConIdx = closestIdx;
|
||||||
Handles.color = con.allowed ? Color.green : Color.red;
|
}
|
||||||
float thickness = (i == closestIndex && closestDist < hoverRadius) ? 5f : 1f;
|
|
||||||
|
if (e.type == EventType.MouseUp && e.button == 0)
|
||||||
|
{
|
||||||
|
if (closestIdx == clickedConIdx && closestIdx != -1)
|
||||||
|
{
|
||||||
|
gm.connections[closestIdx].allowed = !gm.connections[closestIdx].allowed;
|
||||||
|
e.Use(); // Event als verarbeitet markieren
|
||||||
|
}
|
||||||
|
|
||||||
|
clickedConIdx = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zeichnen
|
||||||
|
for (int i = 0; i < gm.connections.Count; i++)
|
||||||
|
{
|
||||||
|
var con = gm.connections[i];
|
||||||
|
Handles.color = (i == closestIdx && !con.allowed) ? gm.editorConnectionHoveredColor : con.allowed ? gm.editorConnectionAllowedColor : gm.editorConnectionForbiddenColor;
|
||||||
|
float thickness = (i == closestIdx) ? gm.editorConnectionHoveredWidth : con.allowed ? gm.editorConnectionAllowedWidth : gm.editorConnectionForbiddenWidth;
|
||||||
Handles.DrawLine(con.nodeA.transform.position, con.nodeB.transform.position, thickness);
|
Handles.DrawLine(con.nodeA.transform.position, con.nodeB.transform.position, thickness);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
}
|
// SceneView kontinuierlich aktualisieren
|
||||||
|
SceneView.RepaintAll();
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
|
||||||
|
|
||||||
[CustomEditor(typeof(GameManager))]
|
|
||||||
public class GameManagerEditor : Editor
|
|
||||||
{
|
|
||||||
public override void OnInspectorGUI()
|
|
||||||
{
|
|
||||||
// Standard-Inspector zeichnen
|
|
||||||
DrawDefaultInspector();
|
|
||||||
|
|
||||||
GameManager gm = (GameManager)target;
|
|
||||||
|
|
||||||
GUILayout.BeginHorizontal();
|
|
||||||
|
|
||||||
if (GUILayout.Button("Generate Sphere"))
|
|
||||||
{
|
|
||||||
gm.GenerateAlongSphere();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (GUILayout.Button("Generate Connections"))
|
|
||||||
{
|
|
||||||
gm.GenerateConnections();
|
|
||||||
}
|
|
||||||
|
|
||||||
GUILayout.EndHorizontal();
|
|
||||||
|
|
||||||
if (GUILayout.Button("Generate"))
|
|
||||||
{
|
|
||||||
gm.GenerateAlongSphere();
|
|
||||||
gm.GenerateConnections();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[InitializeOnLoad]
|
|
||||||
public static class SceneViewUpdater
|
|
||||||
{
|
|
||||||
static SceneViewUpdater()
|
|
||||||
{
|
|
||||||
EditorApplication.update += () =>
|
|
||||||
{
|
|
||||||
if (SceneView.lastActiveSceneView != null)
|
|
||||||
SceneView.lastActiveSceneView.Repaint();
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue