namespaces

This commit is contained in:
max 2023-07-03 00:47:53 +02:00
parent d225f897ba
commit 6f600c38ab
2 changed files with 44 additions and 42 deletions

View File

@ -1,69 +1,71 @@
using UnityEditor; using UnityEditor;
using UnityEngine; using UnityEngine;
using VertexColor.ScenePartition.Editor;
[InitializeOnLoad] namespace VertexColor.ScenePartition.Editor
public class ScenePartitionSceneViewEditor : Editor
{ {
private const int cellSize = 10; [InitializeOnLoad]
public class ScenePartitionSceneViewEditor : UnityEditor.Editor
static ScenePartitionSceneViewEditor()
{ {
SceneView.duringSceneGui += HandleDuringSceneGui; private const int cellSize = 10;
}
private static void HandleDuringSceneGui(SceneView sceneView) static ScenePartitionSceneViewEditor()
{ {
if (Event.current.modifiers != EventModifiers.Control) return; SceneView.duringSceneGui += HandleDuringSceneGui;
}
Vector3 gridPosition = CalculateGridPosition(Event.current.mousePosition); private static void HandleDuringSceneGui(SceneView sceneView)
int gridId = SceneGrid.CalculateGridPosition(gridPosition, 10); {
if (Event.current.modifiers != EventModifiers.Control) return;
int x = Mathf.FloorToInt(gridPosition.x / cellSize); Vector3 gridPosition = CalculateGridPosition(Event.current.mousePosition);
int z = Mathf.FloorToInt(gridPosition.z / cellSize); int gridId = SceneGrid.CalculateGridPosition(gridPosition, 10);
Vector3 cellOrign = new Vector3(x * cellSize, 0, z * cellSize); int x = Mathf.FloorToInt(gridPosition.x / cellSize);
Vector3[] lines = new Vector3[] { int z = Mathf.FloorToInt(gridPosition.z / cellSize);
Vector3 cellOrign = new Vector3(x * cellSize, 0, z * cellSize);
Vector3[] lines = new Vector3[] {
new Vector3(0, 0, 0) + cellOrign, new Vector3(0, 0, cellSize) + cellOrign, // Left Bottom -> Left Top new Vector3(0, 0, 0) + cellOrign, new Vector3(0, 0, cellSize) + cellOrign, // Left Bottom -> Left Top
new Vector3(cellSize, 0, 0) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign, // Right Bottom -> Right Top new Vector3(cellSize, 0, 0) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign, // Right Bottom -> Right Top
new Vector3(0, 0, 0) + cellOrign, new Vector3(cellSize, 0, 0) + cellOrign, // Left Bottom -> Right Bottom new Vector3(0, 0, 0) + cellOrign, new Vector3(cellSize, 0, 0) + cellOrign, // Left Bottom -> Right Bottom
new Vector3(0, 0, cellSize) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign, // Left Top -> Right Top new Vector3(0, 0, cellSize) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign, // Left Top -> Right Top
}; };
Handles.DrawDottedLines(lines, 10.0f); Handles.DrawDottedLines(lines, 10.0f);
Handles.Label(cellOrign + new Vector3(cellSize * 0.5f, 0, cellSize * 0.5f), new GUIContent($"{gridId}")); Handles.Label(cellOrign + new Vector3(cellSize * 0.5f, 0, cellSize * 0.5f), new GUIContent($"{gridId}"));
if (Event.current.button == 1 && Event.current.type == EventType.MouseDown) if (Event.current.button == 1 && Event.current.type == EventType.MouseDown)
{ {
GenericMenu menu = new GenericMenu(); GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent($"Load {gridId}"), false, Load, gridId); menu.AddItem(new GUIContent($"Load {gridId}"), false, Load, gridId);
menu.ShowAsContext(); menu.ShowAsContext();
Event.current.Use(); Event.current.Use();
}
SceneView.RepaintAll();
} }
SceneView.RepaintAll(); private static Vector3 CalculateGridPosition(Vector2 mousePosition)
} {
if (Event.current == null) return Vector3.zero;
private static Vector3 CalculateGridPosition(Vector2 mousePosition) Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition);
{ Plane plane = new Plane(Vector3.up, Vector3.zero); // Horizontal plane at y = 0
if (Event.current == null) return Vector3.zero;
Ray ray = HandleUtility.GUIPointToWorldRay(mousePosition); if (!plane.Raycast(ray, out float distance)) return Vector3.zero;
Plane plane = new Plane(Vector3.up, Vector3.zero); // Horizontal plane at y = 0
if (!plane.Raycast(ray, out float distance)) return Vector3.zero; Vector3 intersectionPoint = ray.GetPoint(distance);
return intersectionPoint;
}
Vector3 intersectionPoint = ray.GetPoint(distance); private static void Load(object gridId)
return intersectionPoint; {
} if (gridId == null) return;
if (gridId is not int gridPos) return;
private static void Load(object gridId)
{
if (gridId == null) return;
if (gridId is not int gridPos) return;
}
} }
} }

View File

@ -1,7 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
namespace VertexColor.ScenePartition.Editor namespace VertexColor.ScenePartition
{ {
[System.Serializable] [System.Serializable]
public class SceneGrid public class SceneGrid