feat: files ausgelagert + contextmenu connect nodes

This commit is contained in:
LordDemonix 2025-09-17 15:43:26 +02:00
parent 227d017bec
commit 9a5b06387d
7 changed files with 108234 additions and 108176 deletions

File diff suppressed because it is too large Load diff

8
Assets/Editor.meta Normal file
View file

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e2cbe02f0d4576345b0b3821bc75b2d7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,271 @@
using UnityEditor;
using UnityEngine;
using System.Linq;
[InitializeOnLoad]
public static class EditorCustom
{
static EditorCustom()
{
SceneView.duringSceneGui += OnSceneGUI;
}
private static void OnSceneGUI(SceneView sceneView)
{
Event e = Event.current;
if (e.type == EventType.MouseDown && e.button == 1)
{
// Nodes prüfen
GameManager gm = GameObject.FindFirstObjectByType<GameManager>();
if (!gm) return;
Node[] selectedNodes = Selection.gameObjects
.Select(g => g.GetComponent<Node>())
.Where(n => n != null)
.ToArray();
GenericMenu menu = new GenericMenu();
if (selectedNodes.Length == 2)
{
menu.AddItem(new GUIContent("Connect Selected Nodes"), false, () =>
{
GameManager.Connection con = gm.connections.Find(con =>
(con.nodeA == selectedNodes[0] && con.nodeB == selectedNodes[1]) ||
(con.nodeA == selectedNodes[1] && con.nodeB == selectedNodes[0]));
if (con != null)
con.allowed = true;
else
gm.AddConnection(selectedNodes[0], selectedNodes[1]);
});
menu.ShowAsContext();
e.Use(); // verhindert, dass Unity sein eigenes Menü anzeigt
}
}
}
[CustomEditor(typeof(GameManager))]
public class GameManagerEditor : Editor
{
int clickedConIdx = -1;
// Custom Inspector
public override void OnInspectorGUI()
{
DrawDefaultInspector();
GameManager gm = (GameManager)target;
GUILayout.Label("Generation Parameters", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
gm.nodeCount = EditorGUILayout.IntSlider("Node Count", gm.nodeCount, 0, 200);
if (EditorGUI.EndChangeCheck() && gm.regenerateOnChange)
{
gm.GenerateAlongSphere();
gm.GenerateConnections();
SceneView.RepaintAll();
}
EditorGUI.BeginChangeCheck();
gm.maxConnectionLength = EditorGUILayout.Slider("Max Connection Length", gm.maxConnectionLength, 0, 100);
if (EditorGUI.EndChangeCheck() && gm.regenerateOnChange)
{
gm.GenerateConnections();
SceneView.RepaintAll();
}
GUILayout.Space(10);
GUILayout.Label("Connection Gizmos", EditorStyles.boldLabel);
gm.hoverRadiusCon = EditorGUILayout.IntSlider("Hover Radius (Connection)", gm.hoverRadiusCon, 0, 100);
gm.hoverRadiusNode = EditorGUILayout.IntSlider("Hover Radius (Node)", gm.hoverRadiusNode, 0, 100);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Distribute Nodes"))
{
gm.GenerateAlongSphere();
SceneView.RepaintAll();
}
if (GUILayout.Button("Generate Connections"))
{
gm.GenerateConnections();
SceneView.RepaintAll();
}
if (GUILayout.Button("Generate Both"))
{
gm.GenerateAlongSphere();
gm.GenerateConnections();
SceneView.RepaintAll();
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (GUILayout.Button("Fetch all Nodes"))
{
Node[] allNodes = gm.NodeParent.GetComponentsInChildren<Node>();
Debug.Log("Added " + (allNodes.Length - gm.nodeCount) + " new nodes");
gm.nodes = allNodes.ToList();
gm.nodeCount = allNodes.Length;
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.BeginVertical();
for (int i = 0; i < gm.levels.Count; i++)
{
if (gm.selectedLevel == i)
GUILayout.Label("▷ Level " + i, EditorStyles.boldLabel);
else
GUILayout.Label(" Level " + i);
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
for (int i = 0; i < gm.levels.Count; i++)
{
if (GUILayout.Button("Load"))
gm.LoadLevelData(i);
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
for (int i = 0; i < gm.levels.Count; i++)
{
GUILayout.BeginHorizontal();
if (gm.selectedLevel == i)
if (GUILayout.Button("Save"))
gm.SaveLevelData(i);
if (GUILayout.Button("Delete"))
{
gm.levels.Remove(gm.levels[i]);
if (gm.selectedLevel == i)
gm.selectedLevel = -1;
if (gm.selectedLevel > i)
gm.selectedLevel--;
if (gm.levels.Count <= 0)
gm.selectedLevel = -1;
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
if (GUILayout.Button("Save as new"))
gm.SaveLevelData();
}
// Custom Scene Interaction while GameManager selected
private void OnSceneGUI()
{
Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual;
GameManager gm = FindFirstObjectByType<GameManager>();
if (gm == null) return;
GameObject selected = Selection.activeGameObject;
bool allowHover = false;
if (selected == gm.gameObject)
allowHover = true;
else
{
Node node = selected?.GetComponent<Node>();
if (node != null && gm.nodes.Contains(node))
allowHover = true;
}
if (!allowHover) return;
if (gm.connections == null || gm.connections.Count == 0) return;
Camera cam = SceneView.lastActiveSceneView.camera;
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;
bool cancelHover = false;
// Hover-Ermittlung Connections
for (int i = 0; i < gm.connections.Count; i++)
{
gm.connections[i].hovered = false;
if (cancelHover)
continue;
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 (Vector3.Distance(a, mousePos) <= gm.hoverRadiusNode || Vector3.Distance(b, mousePos) <= gm.hoverRadiusNode)
cancelHover = true;
if (dist < closestDist && dist < gm.hoverRadiusCon)
{
closestDist = dist;
closestIdx = i;
}
}
if (closestIdx < 0)
cancelHover = true;
if (!cancelHover)
gm.connections[closestIdx].hovered = true;
// Klick-Behandlung
Event e = Event.current;
if (e.type == EventType.MouseDown && e.button == 0 && !cancelHover)
{
clickedConIdx = closestIdx;
}
if (e.type == EventType.MouseUp && e.button == 0)
{
if (closestIdx == clickedConIdx && !cancelHover)
{
gm.connections[closestIdx].allowed = !gm.connections[closestIdx].allowed;
e.Use(); // Event als verarbeitet markieren
}
clickedConIdx = -1;
}
// SceneView kontinuierlich aktualisieren
SceneView.RepaintAll();
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: ac51d1251f4305d46910678898892e6a

View file

@ -0,0 +1,38 @@
using UnityEngine;
public static class Extensions
{
// Zeichnet eine Quadratic Bezier Kurve (Start, Control, End) mit N Punkten
public static void SetQuadraticBezier(this LineRenderer line, Vector3 start, Vector3 control, Vector3 end, int segments)
{
line.positionCount = segments + 1;
for (int i = 0; i <= segments; i++)
{
float t = i / (float)segments;
// Quadratic Bezier Formel
Vector3 point = (1 - t) * (1 - t) * start +
2 * (1 - t) * t * control +
t * t * end;
line.SetPosition(i, point);
}
}
public static void SetGreatCircleArc(this LineRenderer line, Vector3 start, Vector3 end, int segments, float radius)
{
// Normalize to sphere radius
Vector3 startNorm = start.normalized * radius;
Vector3 endNorm = end.normalized * radius;
line.positionCount = segments + 1;
for (int i = 0; i <= segments; i++)
{
float t = i / (float)segments;
// spherical linear interpolation (slerp) between start and end
Vector3 point = Vector3.Slerp(startNorm, endNorm, t);
line.SetPosition(i, point);
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 231c1aaa581b73946a007e5fc1af6669

View file

@ -7,7 +7,6 @@ using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
using static UnityEditor.PlayerSettings;
using static UnityEngine.GraphicsBuffer;
[ExecuteAlways]
@ -40,6 +39,29 @@ public class GameManager : MonoBehaviour
public LineRenderer lineRenderer;
}
[Serializable]
public class LevelData
{
[Serializable]
public class NodeData
{
public Vector3 position;
public int owner;
}
[Serializable]
public class ConnectionData
{
public int nodeAIndex;
public int nodeBIndex;
public bool allowed = true;
}
public float maxConnectionLength = 0;
public List<NodeData> nodes = new List<NodeData>();
public List<ConnectionData> connections = new List<ConnectionData>();
}
void Awake()
{
if (Instance != null && Instance != this)
@ -112,20 +134,7 @@ public class GameManager : MonoBehaviour
}
if (!conExists)
{
var dummy = new GameObject("dummy");
dummy.transform.SetParent(ConnectionParent);
var newCon = new Connection
{
nodeA = nodeA,
nodeB = nodeB,
lineRenderer = dummy.AddComponent<LineRenderer>()
};
newCon.lineRenderer.enabled = false;
newCon.lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
newCon.lineRenderer.positionCount = 3;
connections.Add(newCon);
AddConnection(nodeA, nodeB);
}
}
@ -154,6 +163,25 @@ public class GameManager : MonoBehaviour
}
}
public void AddConnection(Node nodeA, Node nodeB, bool allowed = true)
{
var dummy = new GameObject("dummy");
dummy.transform.SetParent(ConnectionParent);
var newCon = new Connection
{
nodeA = nodeA,
nodeB = nodeB,
allowed = allowed,
lineRenderer = dummy.AddComponent<LineRenderer>()
};
newCon.lineRenderer.enabled = false;
newCon.lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
newCon.lineRenderer.positionCount = 3;
connections.Add(newCon);
}
public void LoadLevelData(int index)
{
if(index >= levels.Count)
@ -182,21 +210,7 @@ public class GameManager : MonoBehaviour
foreach (LevelData.ConnectionData conData in levels[index].connections)
{
var dummy = new GameObject("dummy");
dummy.transform.SetParent(ConnectionParent);
var newCon = new Connection
{
nodeA = nodes[conData.nodeAIndex],
nodeB = nodes[conData.nodeBIndex],
allowed = conData.allowed,
lineRenderer = dummy.AddComponent<LineRenderer>()
};
newCon.lineRenderer.enabled = false;
newCon.lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
newCon.lineRenderer.positionCount = 3;
connections.Add(newCon);
AddConnection(nodes[conData.nodeAIndex], nodes[conData.nodeBIndex]);
}
selectedLevel = index;
maxConnectionLength = levels[index].maxConnectionLength;
@ -247,281 +261,4 @@ public class GameManager : MonoBehaviour
Debug.Log("Saved Level " + newIndex);
}
}
[Serializable]
public class LevelData
{
[Serializable]
public class NodeData
{
public Vector3 position;
public int owner;
}
[Serializable]
public class ConnectionData
{
public int nodeAIndex;
public int nodeBIndex;
public bool allowed = true;
}
public float maxConnectionLength = 0;
public List<NodeData> nodes = new List<NodeData>();
public List<ConnectionData> connections = new List<ConnectionData>();
}
#if UNITY_EDITOR
[CustomEditor(typeof(GameManager))]
public class GameManagerEditor : Editor
{
int clickedConIdx = -1;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
GameManager gm = (GameManager)target;
GUILayout.Label("Generation Parameters", EditorStyles.boldLabel);
EditorGUI.BeginChangeCheck();
gm.nodeCount = EditorGUILayout.IntSlider("Node Count", gm.nodeCount, 0, 200);
gm.maxConnectionLength = EditorGUILayout.Slider("Max Connection Length", gm.maxConnectionLength, 0, 100);
if (EditorGUI.EndChangeCheck() && gm.regenerateOnChange)
{
gm.GenerateAlongSphere();
gm.GenerateConnections();
SceneView.RepaintAll();
}
GUILayout.Space(10);
GUILayout.Label("Connection Gizmos", EditorStyles.boldLabel);
gm.hoverRadiusCon = EditorGUILayout.IntSlider("Hover Radius (Connection)", gm.hoverRadiusCon, 0, 100);
gm.hoverRadiusNode = EditorGUILayout.IntSlider("Hover Radius (Node)", gm.hoverRadiusNode, 0, 100);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Distribute Nodes"))
{
gm.GenerateAlongSphere();
SceneView.RepaintAll();
}
if (GUILayout.Button("Generate Connections"))
{
gm.GenerateConnections();
SceneView.RepaintAll();
}
if (GUILayout.Button("Generate Both"))
{
gm.GenerateAlongSphere();
gm.GenerateConnections();
SceneView.RepaintAll();
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (GUILayout.Button("Fetch all Nodes"))
{
Node[] allNodes = gm.NodeParent.GetComponentsInChildren<Node>();
Debug.Log("Added " + (allNodes.Length - gm.nodeCount) + " new nodes");
gm.nodes = allNodes.ToList();
gm.nodeCount = allNodes.Length;
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.BeginVertical();
for(int i = 0; i < gm.levels.Count; i++)
{
if (gm.selectedLevel == i)
GUILayout.Label("▷ Level " + i, EditorStyles.boldLabel);
else
GUILayout.Label(" Level " + i);
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
for (int i = 0; i < gm.levels.Count; i++)
{
if (GUILayout.Button("Load"))
gm.LoadLevelData(i);
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
for (int i = 0; i < gm.levels.Count; i++)
{
GUILayout.BeginHorizontal();
if (gm.selectedLevel == i)
if (GUILayout.Button("Save"))
gm.SaveLevelData(i);
if (GUILayout.Button("Delete"))
{
gm.levels.Remove(gm.levels[i]);
if (gm.selectedLevel == i)
gm.selectedLevel = -1;
if (gm.selectedLevel > i)
gm.selectedLevel--;
if(gm.levels.Count <= 0)
gm.selectedLevel = -1;
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
GUILayout.EndHorizontal();
if (GUILayout.Button("Save as new"))
gm.SaveLevelData();
}
private void OnSceneGUI()
{
Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual;
GameManager gm = FindFirstObjectByType<GameManager>();
if (gm == null) return;
GameObject selected = Selection.activeGameObject;
bool allowHover = false;
if (selected == gm.gameObject)
allowHover = true;
else
{
Node node = selected?.GetComponent<Node>();
if (node != null && gm.nodes.Contains(node))
allowHover = true;
}
if (!allowHover) return;
if (gm.connections == null || gm.connections.Count == 0) return;
Camera cam = SceneView.lastActiveSceneView.camera;
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;
bool cancelHover = false;
// Hover-Ermittlung Connections
for (int i = 0; i < gm.connections.Count; i++)
{
gm.connections[i].hovered = false;
if (cancelHover)
continue;
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 (Vector3.Distance(a, mousePos) <= gm.hoverRadiusNode || Vector3.Distance(b, mousePos) <= gm.hoverRadiusNode)
cancelHover = true;
if (dist < closestDist && dist < gm.hoverRadiusCon)
{
closestDist = dist;
closestIdx = i;
}
}
if (closestIdx < 0)
cancelHover = true;
if (!cancelHover)
gm.connections[closestIdx].hovered = true;
// Klick-Behandlung
Event e = Event.current;
if (e.type == EventType.MouseDown && e.button == 0 && !cancelHover)
{
clickedConIdx = closestIdx;
}
if (e.type == EventType.MouseUp && e.button == 0)
{
if (closestIdx == clickedConIdx && !cancelHover)
{
gm.connections[closestIdx].allowed = !gm.connections[closestIdx].allowed;
e.Use(); // Event als verarbeitet markieren
}
clickedConIdx = -1;
}
// SceneView kontinuierlich aktualisieren
SceneView.RepaintAll();
}
}
#endif
public static class LineRendererExtensions
{
// Zeichnet eine Quadratic Bezier Kurve (Start, Control, End) mit N Punkten
public static void SetQuadraticBezier(this LineRenderer line, Vector3 start, Vector3 control, Vector3 end, int segments)
{
line.positionCount = segments + 1;
for (int i = 0; i <= segments; i++)
{
float t = i / (float)segments;
// Quadratic Bezier Formel
Vector3 point = (1 - t) * (1 - t) * start +
2 * (1 - t) * t * control +
t * t * end;
line.SetPosition(i, point);
}
}
public static void SetGreatCircleArc(this LineRenderer line, Vector3 start, Vector3 end, int segments, float radius)
{
// Normalize to sphere radius
Vector3 startNorm = start.normalized * radius;
Vector3 endNorm = end.normalized * radius;
line.positionCount = segments + 1;
for (int i = 0; i <= segments; i++)
{
float t = i / (float)segments;
// spherical linear interpolation (slerp) between start and end
Vector3 point = Vector3.Slerp(startNorm, endNorm, t);
line.SetPosition(i, point);
}
}
}