diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index f2d7ce7..3cc7ada 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -15,11 +15,12 @@ public class GameManager : MonoBehaviour public Transform NodeParent; public GameObject NodePrefab; - [HideInInspector] public static GameManager instance; + [HideInInspector] public static GameManager Instance { get; private set; } [HideInInspector] public float maxConnectionLength = 6; [HideInInspector] public int nodeCount = 100; - [HideInInspector] public int hoverRadius = 50; + [HideInInspector] public int hoverRadiusCon = 50; + [HideInInspector] public int hoverRadiusNode = 50; [SerializeField] [HideInInspector] public List connections = new List(); @@ -34,7 +35,30 @@ public class GameManager : MonoBehaviour public LineRenderer lineRenderer; } + void Start() + { + if (Instance != null && Instance != this) + { + if (Application.isPlaying) + Destroy(gameObject); + return; + } + Instance = this; + if (Application.isPlaying) + DontDestroyOnLoad(gameObject); + + } + +#if UNITY_EDITOR + void OnEnable() + { + if (Instance == null) + Instance = this; + else if (Instance != this) + DestroyImmediate(gameObject); + } +#endif public void GenerateAlongSphere() { @@ -117,6 +141,12 @@ public class GameManager : MonoBehaviour { foreach (var con in connections) { + if(Application.isPlaying && !con.allowed) + { + con.lineRenderer.enabled = false; + continue; + } + float width = (con.hovered ? 0.6f : 0.3f) * (con.allowed ? 1f : 0.3f); con.lineRenderer.startColor = con.allowed ? con.nodeA.transform.GetChild(0).GetComponent().sharedMaterial.color : new Color(0.2f, 0.2f, 0.2f); @@ -156,7 +186,8 @@ public class GameManagerEditor : Editor GUILayout.Space(10); GUILayout.Label("Connection Gizmos", EditorStyles.boldLabel); - gm.hoverRadius = EditorGUILayout.IntSlider("Hover Radius", gm.hoverRadius, 0, 100); + gm.hoverRadiusCon = EditorGUILayout.IntSlider("Hover Radius (Connection)", gm.hoverRadiusCon, 0, 100); + gm.hoverRadiusNode = EditorGUILayout.IntSlider("Hover Radius (Node)", gm.hoverRadiusNode, 0, 100); @@ -184,11 +215,28 @@ public class GameManagerEditor : Editor GUILayout.EndHorizontal(); } + private void OnSceneGUI() { Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual; - GameManager gm = (GameManager)target; + GameManager gm = GameManager.Instance; + if (gm == null) return; + + GameObject selected = Selection.activeGameObject; + bool allowHover = false; + + if (selected == gm.gameObject) + allowHover = true; + else + { + Node node = selected?.GetComponent(); + 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; @@ -199,12 +247,16 @@ public class GameManagerEditor : Editor int closestIdx = -1; float closestDist = float.MaxValue; + bool cancelHover = false; - // Hover-Ermittlung + // 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); @@ -215,14 +267,21 @@ public class GameManagerEditor : Editor Vector3 proj = a + t * ab; float dist = Vector3.Distance(mousePos, proj); - if (dist < closestDist && dist < gm.hoverRadius) + 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) + if (closestIdx < 0) + cancelHover = true; + + + if (!cancelHover) gm.connections[closestIdx].hovered = true; @@ -230,14 +289,14 @@ public class GameManagerEditor : Editor Event e = Event.current; - if (e.type == EventType.MouseDown && e.button == 0 && closestIdx >= 0) + if (e.type == EventType.MouseDown && e.button == 0 && !cancelHover) { clickedConIdx = closestIdx; } if (e.type == EventType.MouseUp && e.button == 0) { - if (closestIdx == clickedConIdx && closestIdx != -1) + if (closestIdx == clickedConIdx && !cancelHover) { gm.connections[closestIdx].allowed = !gm.connections[closestIdx].allowed; e.Use(); // Event als verarbeitet markieren diff --git a/Assets/Scripts/Node.cs b/Assets/Scripts/Node.cs index e73a3ba..a880849 100644 --- a/Assets/Scripts/Node.cs +++ b/Assets/Scripts/Node.cs @@ -30,9 +30,5 @@ public class Node : MonoBehaviour } } - - private void OnMouseDown() - { - Owner = Owner >= 1 ? -1 : Owner + 1; - } + }