generated from max/template-unity-project
	Updated ScenePartitionViewEditor
- Grid is now drawn based on the cell size of the current active open scene - Added function to get ScenePartitionSO of current active scene
This commit is contained in:
		| @@ -6,7 +6,7 @@ namespace VertexColor.ScenePartition.Editor | |||||||
|     [InitializeOnLoad] |     [InitializeOnLoad] | ||||||
|     public class ScenePartitionSceneViewEditor : UnityEditor.Editor |     public class ScenePartitionSceneViewEditor : UnityEditor.Editor | ||||||
|     { |     { | ||||||
|         private const int cellSize = 10; |         private static ScenePartitionSO scenePartitionSO = null; | ||||||
| 
 | 
 | ||||||
|         static ScenePartitionSceneViewEditor() |         static ScenePartitionSceneViewEditor() | ||||||
|         { |         { | ||||||
| @@ -16,24 +16,50 @@ namespace VertexColor.ScenePartition.Editor | |||||||
|         private static void HandleDuringSceneGui(SceneView sceneView) |         private static void HandleDuringSceneGui(SceneView sceneView) | ||||||
|         { |         { | ||||||
|             if (Event.current.modifiers != EventModifiers.Control) return; |             if (Event.current.modifiers != EventModifiers.Control) return; | ||||||
|  |             if (!ScenePartitionUtils.TryGetScenePartitionSOForActiveScene(out scenePartitionSO)) return; | ||||||
| 
 | 
 | ||||||
|  |             int cellSize = scenePartitionSO.Data.SceneGrid.cellSize; | ||||||
|             Vector3 gridPosition = CalculateGridPosition(Event.current.mousePosition); |             Vector3 gridPosition = CalculateGridPosition(Event.current.mousePosition); | ||||||
|             int gridId = SceneGrid.CalculateGridPosition(gridPosition, 10); |             int gridId = SceneGrid.CalculateGridPosition(gridPosition, cellSize); | ||||||
| 
 | 
 | ||||||
|             int x = Mathf.FloorToInt(gridPosition.x / cellSize); |             {   // Draw Cells. | ||||||
|             int z = Mathf.FloorToInt(gridPosition.z / cellSize); |                 foreach (int id in scenePartitionSO.Data.SceneGrid.Grid.Keys) | ||||||
|  |                 { | ||||||
|  |                     (int x, int y) = SceneGrid.IntToIntPair(id); | ||||||
| 
 | 
 | ||||||
|             Vector3 cellOrign = new Vector3(x * cellSize, 0, z * cellSize); |                     Vector3 cellOrign = new Vector3(x * cellSize, 0, y * cellSize); | ||||||
|             Vector3[] lines = new Vector3[] { |                     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.DrawLines(lines); | ||||||
|             Handles.Label(cellOrign + new Vector3(cellSize * 0.5f, 0, cellSize * 0.5f), new GUIContent($"{gridId}")); |                 } | ||||||
|  |             } | ||||||
| 
 | 
 | ||||||
|  |             if (!scenePartitionSO.Data.SceneGrid.Grid.ContainsKey(gridId)) return; | ||||||
|  | 
 | ||||||
|  |             {   // Draw Selection. | ||||||
|  |                 int x = Mathf.FloorToInt(gridPosition.x / cellSize); | ||||||
|  |                 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(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, cellSize) + cellOrign, new Vector3(cellSize, 0, cellSize) + cellOrign,    // Left Top -> Right Top | ||||||
|  |                 }; | ||||||
|  | 
 | ||||||
|  |                 Color c = Handles.color; | ||||||
|  |                 Handles.color = Color.yellow; | ||||||
|  |                 Handles.DrawLines(lines); | ||||||
|  |                 Handles.color = c; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             // Context menu to load. | ||||||
|             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(); | ||||||
| @@ -62,10 +88,11 @@ namespace VertexColor.ScenePartition.Editor | |||||||
| 
 | 
 | ||||||
|         private static void Load(object gridId) |         private static void Load(object gridId) | ||||||
|         { |         { | ||||||
|  |             if (scenePartitionSO == null) return; | ||||||
|             if (gridId == null) return; |             if (gridId == null) return; | ||||||
|             if (gridId is not int gridPos) return; |             if (gridId is not int cellId) return; | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
|  |             scenePartitionSO.LoadCell(cellId); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -57,5 +57,35 @@ namespace VertexColor.ScenePartition.Editor | |||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  | 
 | ||||||
|  |         public static bool TryGetScenePartitionSOForActiveScene(out ScenePartitionSO scenePartitionSO) | ||||||
|  |         { | ||||||
|  |             scenePartitionSO = null; | ||||||
|  | 
 | ||||||
|  |             UnityEngine.SceneManagement.Scene activeScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene(); | ||||||
|  | 
 | ||||||
|  |             if (activeScene == null) return false; | ||||||
|  | 
 | ||||||
|  |             string filter = $"t:{nameof(ScenePartitionSO)}"; | ||||||
|  |             string[] assets = AssetDatabase.FindAssets(filter); | ||||||
|  | 
 | ||||||
|  |             if (assets == null || assets.Length <= 0) return false; | ||||||
|  | 
 | ||||||
|  |             for (int i = 0; i < assets.Length; i++) | ||||||
|  |             { | ||||||
|  |                 string path = AssetDatabase.GUIDToAssetPath(assets[i]); | ||||||
|  |                 ScenePartitionSO scenePartition = AssetDatabase.LoadAssetAtPath<ScenePartitionSO>(path); | ||||||
|  | 
 | ||||||
|  |                 if (scenePartition == null) continue; | ||||||
|  | 
 | ||||||
|  |                 if (scenePartition.SceneName == activeScene.name) | ||||||
|  |                 { | ||||||
|  |                     scenePartitionSO = scenePartition; | ||||||
|  |                     return true; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user