feat: fancy custom editor btns
This commit is contained in:
parent
66240db1ee
commit
a44ccf595e
2 changed files with 18640 additions and 1815 deletions
File diff suppressed because one or more lines are too long
|
|
@ -4,6 +4,7 @@ using Unity.VisualScripting;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Rendering;
|
using UnityEngine.Rendering;
|
||||||
|
using static UnityEngine.GraphicsBuffer;
|
||||||
|
|
||||||
[ExecuteAlways]
|
[ExecuteAlways]
|
||||||
public class GameManager : MonoBehaviour
|
public class GameManager : MonoBehaviour
|
||||||
|
|
@ -17,40 +18,43 @@ public class GameManager : MonoBehaviour
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
private GameObject NodePrefab;
|
private GameObject NodePrefab;
|
||||||
|
|
||||||
|
[Header("Node Generation")]
|
||||||
|
|
||||||
|
[Range(0, 20)]
|
||||||
|
public float maxConnectionLength = 6;
|
||||||
|
|
||||||
|
[Range(0, 1000)]
|
||||||
|
public int nodeCount = 100;
|
||||||
|
|
||||||
[Header("Parameters")]
|
|
||||||
[Range(0, 100)]
|
[Range(0, 100)]
|
||||||
public float maxConnectionLength;
|
public float hoverRadius = 50;
|
||||||
|
|
||||||
|
|
||||||
|
public List<Connection> connections = new List<Connection>();
|
||||||
private List<Node> nodes = new List<Node>();
|
private List<Node> nodes = new List<Node>();
|
||||||
|
|
||||||
[SerializeField]
|
|
||||||
public List<Connection> connections = new List<Connection>();
|
|
||||||
|
|
||||||
[Serializable]
|
|
||||||
public struct Connection
|
public struct Connection
|
||||||
{
|
{
|
||||||
public Node nodeA, nodeB;
|
public Node nodeA, nodeB;
|
||||||
public bool allowed;
|
public bool allowed;
|
||||||
}
|
}
|
||||||
|
|
||||||
[ContextMenu("Generate Along Sphere")]
|
public void GenerateAlongSphere()
|
||||||
void GenerateAlongSphere()
|
|
||||||
{
|
{
|
||||||
connections.Clear();
|
connections.Clear();
|
||||||
nodes.Clear();
|
nodes.Clear();
|
||||||
|
|
||||||
|
for (int i = NodeParent.childCount - 1; i >= 0; i--)
|
||||||
|
DestroyImmediate(NodeParent.GetChild(i).gameObject);
|
||||||
|
|
||||||
int pointCount = 100;
|
|
||||||
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;
|
||||||
|
|
||||||
for (int i = 0; i < pointCount; i++)
|
for (int i = 0; i < nodeCount; i++)
|
||||||
{
|
{
|
||||||
float t = (float)i / pointCount; // von 0 bis 1
|
float t = (float)i / nodeCount; // von 0 bis 1
|
||||||
float inclination = Mathf.Acos(1f - 2f * t);
|
float inclination = Mathf.Acos(1f - 2f * t);
|
||||||
float azimuth = angleIncrement * i;
|
float azimuth = angleIncrement * i;
|
||||||
|
|
||||||
|
|
@ -66,8 +70,7 @@ public class GameManager : MonoBehaviour
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[ContextMenu("Generate Connections")]
|
public void GenerateConnections()
|
||||||
void GenerateConnections()
|
|
||||||
{
|
{
|
||||||
connections.Clear();
|
connections.Clear();
|
||||||
|
|
||||||
|
|
@ -101,19 +104,100 @@ public class GameManager : MonoBehaviour
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
void OnDrawGizmos()
|
void OnDrawGizmos()
|
||||||
{
|
{
|
||||||
if (connections == null) return;
|
if (connections == null || SceneView.lastActiveSceneView?.camera == null) return;
|
||||||
|
|
||||||
// Linien respektieren den Z-Buffer
|
Camera cam = SceneView.lastActiveSceneView.camera;
|
||||||
Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual;
|
|
||||||
|
|
||||||
foreach (var con in connections)
|
// 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;
|
Handles.color = con.allowed ? Color.green : Color.red;
|
||||||
Handles.DrawLine(con.nodeA.transform.position, con.nodeB.transform.position);
|
float thickness = (i == closestIndex && closestDist < hoverRadius) ? 5f : 1f;
|
||||||
|
Handles.DrawLine(con.nodeA.transform.position, con.nodeB.transform.position, thickness);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#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
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue