using System; using System.Collections.Generic; using Unity.VisualScripting; using UnityEditor; using UnityEngine; using UnityEngine.Rendering; [ExecuteAlways] public class GameManager : MonoBehaviour { [HideInInspector] public static GameManager instance; [SerializeField] private Transform NodeParent; [SerializeField] private GameObject NodePrefab; [Header("Parameters")] [Range(0, 100)] public float maxConnectionLength; private List nodes = new List(); [SerializeField] public List connections = new List(); [Serializable] public struct Connection { public Node nodeA, nodeB; public bool allowed; } [ContextMenu("Generate Along Sphere")] void GenerateAlongSphere() { connections.Clear(); nodes.Clear(); int pointCount = 100; float radius = 20f; float goldenRatio = (1f + Mathf.Sqrt(5f)) / 2f; float angleIncrement = 2f * Mathf.PI * goldenRatio; for (int i = 0; i < pointCount; i++) { float t = (float)i / pointCount; // von 0 bis 1 float inclination = Mathf.Acos(1f - 2f * t); float azimuth = angleIncrement * i; float x = Mathf.Sin(inclination) * Mathf.Cos(azimuth); float y = Mathf.Sin(inclination) * Mathf.Sin(azimuth); float z = Mathf.Cos(inclination); Vector3 pos = new Vector3(x, y, z) * radius; var auto = Instantiate(NodePrefab, NodeParent); auto.transform.localPosition = pos; nodes.Add(auto.GetComponent()); } } [ContextMenu("Generate Connections")] void GenerateConnections() { connections.Clear(); foreach (Node nodeA in nodes) { if (nodeA == null) continue; foreach (Node nodeB in nodes) { if (nodeB == null) continue; bool conExists = false; if (nodeA == nodeB || Math.Abs(Vector3.Distance(nodeA.transform.position, nodeB.transform.position)) > maxConnectionLength) continue; foreach (Connection con in connections) { if ((con.nodeA == nodeA && con.nodeB == nodeB) || (con.nodeA == nodeB && con.nodeB == nodeA)) { conExists = true; break; } } if (!conExists) connections.Add(new Connection { nodeA = nodeA, nodeB = nodeB }); } } } // 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) return; // Linien respektieren den Z-Buffer Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual; foreach (var con in connections) { Handles.color = con.allowed ? Color.green : Color.red; Handles.DrawLine(con.nodeA.transform.position, con.nodeB.transform.position); } } #endif }