feat: better editor

This commit is contained in:
LordDemonix 2025-09-16 10:45:09 +02:00
parent a44ccf595e
commit b7d38dcbb9
2 changed files with 2545 additions and 16863 deletions

File diff suppressed because it is too large Load diff

View file

@ -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;
@ -97,86 +113,26 @@ public class GameManager : MonoBehaviour
} }
} }
} }
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
#if UNITY_EDITOR
void OnDrawGizmos()
{
if (connections == null || SceneView.lastActiveSceneView?.camera == null) return;
Camera cam = SceneView.lastActiveSceneView.camera;
// Maus Screen-Koordinaten
Vector2 guiPos = Event.current.mousePosition;
guiPos.y = cam.pixelHeight - guiPos.y;
Vector3 mousePos = new Vector3(guiPos.x, guiPos.y, 0);
int closestIndex = -1;
float closestDist = float.MaxValue;
// Erster Durchlauf: kleinste Distanz merken
for (int i = 0; i < connections.Count; i++)
{
var con = connections[i];
Vector3 a = cam.WorldToScreenPoint(con.nodeA.transform.position);
Vector3 b = cam.WorldToScreenPoint(con.nodeB.transform.position);
// Punkt-zu-Segment Abstand statt Dreiecksformel
Vector3 ab = b - a;
Vector3 am = mousePos - a;
float t = Mathf.Clamp01(Vector3.Dot(am, ab) / ab.sqrMagnitude);
Vector3 proj = a + t * ab;
float dist = Vector3.Distance(mousePos, proj);
if (dist < closestDist)
{
closestDist = dist;
closestIndex = i;
}
}
// Zweiter Durchlauf: zeichnen
for (int i = 0; i < connections.Count; i++)
{
var con = connections[i];
Handles.color = con.allowed ? Color.green : Color.red;
float thickness = (i == closestIndex && closestDist < hoverRadius) ? 5f : 1f;
Handles.DrawLine(con.nodeA.transform.position, con.nodeB.transform.position, thickness);
}
}
#endif
} }
#if UNITY_EDITOR #if UNITY_EDITOR
[CustomEditor(typeof(GameManager))] [CustomEditor(typeof(GameManager))]
public class GameManagerEditor : Editor public class GameManagerEditor : Editor
{ {
int clickedConIdx = -1;
public override void OnInspectorGUI() public override void OnInspectorGUI()
{ {
// Standard-Inspector zeichnen
DrawDefaultInspector(); DrawDefaultInspector();
GameManager gm = (GameManager)target; GameManager gm = (GameManager)target;
GUILayout.BeginHorizontal(); GUILayout.BeginHorizontal();
if (GUILayout.Button("Generate Sphere")) if (GUILayout.Button("Generate Sphere"))
{
gm.GenerateAlongSphere(); gm.GenerateAlongSphere();
}
if (GUILayout.Button("Generate Connections")) if (GUILayout.Button("Generate Connections"))
{
gm.GenerateConnections(); gm.GenerateConnections();
}
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
@ -186,18 +142,71 @@ public class GameManagerEditor : Editor
gm.GenerateConnections(); gm.GenerateConnections();
} }
} }
} private void OnSceneGUI()
{
Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual;
[InitializeOnLoad] GameManager gm = (GameManager)target;
public static class SceneViewUpdater if (gm.connections == null || gm.connections.Count == 0) return;
{ Camera cam = SceneView.lastActiveSceneView.camera;
static SceneViewUpdater() if (cam == null) return;
Vector2 mouse = Event.current.mousePosition;
mouse.y = cam.pixelHeight - mouse.y;
Vector3 mousePos = new Vector3(mouse.x, mouse.y, 0);
int closestIdx = -1;
float closestDist = float.MaxValue;
// Hover-Ermittlung
for (int i = 0; i < gm.connections.Count; i++)
{ {
EditorApplication.update += () => var con = gm.connections[i];
Vector3 a = cam.WorldToScreenPoint(con.nodeA.transform.position);
Vector3 b = cam.WorldToScreenPoint(con.nodeB.transform.position);
Vector3 ab = b - a;
Vector3 am = mousePos - a;
float t = Mathf.Clamp01(Vector3.Dot(am, ab) / ab.sqrMagnitude);
Vector3 proj = a + t * ab;
float dist = Vector3.Distance(mousePos, proj);
if (dist < closestDist && dist < gm.hoverRadius)
{ {
if (SceneView.lastActiveSceneView != null) closestDist = dist;
SceneView.lastActiveSceneView.Repaint(); closestIdx = i;
}; }
}
// Klick-Behandlung
Event e = Event.current;
if (e.type == EventType.MouseDown && e.button == 0 && closestIdx >= 0)
{
clickedConIdx = closestIdx;
}
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);
}
// SceneView kontinuierlich aktualisieren
SceneView.RepaintAll();
} }
} }
#endif #endif