using UnityEditor; using UnityEditor.UIElements; using UnityEngine; using UnityEngine.UIElements; using Unity.Entities; using GarmentButton.EntityEditor; using Unity.Collections; using Unity.Mathematics; using System.Collections.Generic; namespace GarmentButton.PathFinding.Editor { [CustomEditor(typeof(PathGridCreationMono))] public class PathfinderMonoEditor : UnityEditor.Editor { private EntityEditorWorld _entityEditor; private EntityQuery _query; private List _reverseChange; private Button _button; private bool _isDebugging; private NativeList _points; public override VisualElement CreateInspectorGUI() { var root = new VisualElement(); SetUpOldInspector(root); _button = new Button(); _button.text = "Get Height Data"; _button.clicked += SetUpPoint; var buttonDebugBox = new Button(DebugPoints); buttonDebugBox.text = "DebugPath Box Size"; root.Add(_button); root.Add(buttonDebugBox); return root; } private void OnDisable() { EditorApplication.update -= UpdateForDebug; if (_points.IsCreated) _points.Dispose(); } private void OnEnable() { EditorApplication.update += UpdateForDebug; } private void SetUpOldInspector(VisualElement root) { Label title = new Label("Pathfind Script"); title.style.unityFontStyleAndWeight = FontStyle.Bold; root.Add(title); var baseElement = new VisualElement(); InspectorElement.FillDefaultInspector(baseElement, serializedObject, this); root.Add(baseElement); } private void SetUpPoint() { _button.SetEnabled(false); _reverseChange = new List(); var script = ((PathGridCreationMono)target); var assistnes = script.AssistPlatform; if (assistnes != null) { foreach (var assist in assistnes) { if (assist.activeSelf) continue; assist.SetActive(true); _reverseChange.Add(assist); } } var disable = script.DisablePlatforms; if (disable != null) { foreach (var assist in disable) { if (!assist.activeSelf) continue; assist.SetActive(false); _reverseChange.Add(assist); } } SetUpEntityWorld(); EditorApplication.update += UpdateForBaking; } private void SetUpEntityWorld() { _entityEditor = new EntityEditorWorld(); var world = _entityEditor.World; SystemHandle[] systemHandles = new SystemHandle[] { world.CreateSystem(), }; _entityEditor.CreateSystems(systemHandles); var entity = _entityEditor.EntityManager.CreateEntity(); NativeArray points = new NativeArray(GetPoints().AsArray(), Allocator.Persistent); _entityEditor.EntityManager.AddComponentData(entity, new WorldIndexs { Value = points, }); } private void UpdateForBaking() { if (_entityEditor == null) return; var query = _entityEditor.EntityManager.CreateEntityQuery(typeof(WorldHeights)); if (query.IsEmpty) return; FinishSet(query); } private void FinishSet(EntityQuery query) { var data = query.GetSingletonRW(); var lenght = data.ValueRO.Value.Length; var script = (PathGridCreationMono)target; var array = new float[lenght]; for (int i = 0; i < lenght; i++) { array[i] = data.ValueRO.Value[i]; } data.ValueRW.Value.Dispose(); Undo.RecordObject(target, "Change arrya points"); script.HieghtsPoints = array; _entityEditor.DestroyWorld(); _entityEditor = null; EditorApplication.update -= UpdateForBaking; _button.SetEnabled(true); foreach (var revertObject in _reverseChange) { revertObject.SetActive(!revertObject.activeSelf); } } private NativeList GetPoints() { var authoring = (PathGridCreationMono)target; var totalSize = new float2 { x = authoring.Size.x * Global.ElementSize, y = authoring.Size.y * Global.ElementSize, }; var center = authoring.transform.position; var halfGridSize = new float3(totalSize.x / 2, 0, totalSize.y / 2); var halfCellSize = new float3(Global.ElementSize / 2, 0, Global.ElementSize / 2); NativeList grid = new NativeList(authoring.Size.x * authoring.Size.y, Allocator.Persistent); for (int y = 0; y < authoring.Size.y; y++) { for (int x = 0; x < authoring.Size.x; x++) { var position = new float3(x * (Global.ElementSize), 0, y * (Global.ElementSize)); position -= new float3(halfGridSize); position += new float3(halfCellSize); position += new float3(center.x, 0, center.z); var intPosition = (int3)(position * 10); position = (float3)intPosition * 0.1f; grid.Add(new float2(position.x, position.z)); } } return grid; } private void DebugPoints() { if (_points.IsCreated) _points.Dispose(); if (_isDebugging) { _isDebugging = false; return; } _points = GetPoints(); _isDebugging = true; } private void UpdateForDebug() { if (!_isDebugging) return; DebugPoint(); } private void DebugPoint() { var script = ((PathGridCreationMono)target); for (int i = 0; i < _points.Length; i++) { var height = (float)(int)(script.HieghtsPoints[i] * 10); var center = new float3(_points[i].x, height * 0.1f, _points[i].y); Debug.DrawLine(center, center + new float3(0, -Global.ElementSize, 0),Color.red); } } } }