void AdjustWallBoxes(List <Box> boxes, MapTileDetails details, Vector3 offset, ref int counter) { var total = (instance.length + 1) * (instance.width + 1); var bottomHeight = details.wallOffset; var totalHeight = details.wallHeight + (details.wallUseMapHeight ? instance.height : 0f); var topHeight = bottomHeight + totalHeight; var topUV = details.wallScaleUVHeight ? totalHeight : 1f; // get position and vertex first foreach (var b in boxes) { var corners = b.corners; for (var i = 0; i < 4; i++) { var corner = corners[i]; var index = corner.index; if (i < 2) { corner.position = CornerToPosition(index, bottomHeight) - offset; } else { corner.position = CornerToPosition(index % total, topHeight) - offset; } corner.vertex = counter++; } b.size = b.topRight.position - b.bottomLeft.position; } // then get uv var distance = 0f; var d = 0f; foreach (var b in boxes) { var corners = b.corners; d = Mathf.Max(Mathf.Abs(b.size.x), Mathf.Abs(b.size.z)); for (var i = 0; i < 4; i++) { var corner = corners[i]; var index = corner.index; corner.uv = CornerToWallNormalPosition(distance + (i % 2 == 1 ? d : 0f), i >= 2 ? topUV : 0f); } distance += d; } }
Vector3 AdjustGroundBoxes(List <Box> boxes, MapTileDetails details, ref int counter) { var conversion = new Dictionary <int, int>(); var groundPos = details.groundOffset + (details.groundUseMapHeight ? instance.height : 0f); var offset = Vector3.zero; foreach (var b in boxes) { var corners = b.corners; for (var i = 0; i < 4; i++) { var corner = corners[i]; var index = corner.index; corner.position = CornerToPosition(index, groundPos); corner.uv = CornerToGroundNormalPosition(index); int value; if (!conversion.TryGetValue(index, out value)) { corner.vertex = counter++; conversion.Add(index, corner.vertex); offset += corner.position; } else { corner.vertex = value; } } b.size = b.topRight.position - b.bottomLeft.position; } offset /= counter; foreach (var b in boxes) { foreach (var c in b.corners) { c.position -= offset; } } return(offset); }
/// <summary> /// Editor Window /// </summary> private void OnGUI() { if (instance == null) { GetMapInstance(SceneManager.GetActiveScene()); } GetGUIStyles(); var enums = System.Enum.GetValues(typeof(MapScriptableObject.CellType)); EditorGUILayout.LabelField(instance.name); EditorGUILayout.Separator(); EditorGUILayout.LabelField("Map Cell Editor", boldLabel); length_scr = EditorGUILayout.IntField("Length", length_scr); width_scr = EditorGUILayout.IntField("Width", width_scr); if ((instance.length != length_scr && length_scr > 0) || (instance.width != width_scr && width_scr > 0)) { EditorGUILayout.BeginHorizontal(); //CreateAnchorTextures(); for (var a = 0; a < anchorSet.Length; a++) { if (GUILayout.Button(anchorText[a], anchor == anchorSet[a] ? pressedButton : standardButton)) { anchor = anchorSet[a]; } if ((a + 1) % 3 == 0) { EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); } } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Apply")) { instance.Resize(length_scr, width_scr, anchor); } if (GUILayout.Button("Reset")) { length_scr = instance.length; width_scr = instance.width; } EditorGUILayout.EndHorizontal(); EditorGUILayout.Separator(); } instance.height = EditorGUILayout.IntField("Height", instance.height); instance.cellsize = EditorGUILayout.FloatField("Cell Size", instance.cellsize); // guarantee details are set properly var insdetails = instance.details; var details = new MapTileDetails[enums.Length]; for (var k = 0; k < enums.Length; k++) { var cell = (MapScriptableObject.CellType)enums.GetValue(k); if (insdetails != null && k < insdetails.Length) { details[k] = insdetails[k]; } else { details[k] = new MapTileDetails { cell = cell } }; } instance.details = details; EditorGUILayout.Separator(); EditorGUILayout.LabelField("Tile Editor", boldLabel); // Paint buttons EditorGUILayout.BeginHorizontal(); var i = 0; foreach (var cellvalue in tileOptions) { if (GUILayout.Button(cellvalue.ToString(), selectedCellType == cellvalue ? pressedButton : standardButton)) { selectedCellType = cellvalue; } if (++i == 4) { i = 0; EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); } } EditorGUILayout.EndHorizontal(); // Show options for whichever is selected var item = details[(int)selectedCellType]; item.inactiveColor = EditorGUILayout.ColorField("Inactive Color", item.inactiveColor); item.activeColor = EditorGUILayout.ColorField("Active Color", item.activeColor); item.layer = EditorGUILayout.LayerField("Layer", item.layer); item.areaType = EditorGUILayout.Popup("Area Type", item.areaType, GameObjectUtility.GetNavMeshAreaNames()); EditorGUILayout.Space(); item.groundCreate = EditorGUILayout.Toggle("Create Ground?", item.groundCreate); if (item.groundCreate) { EditorGUI.indentLevel++; item.groundMaterial = (Material)EditorGUILayout.ObjectField("Ground Material", item.groundMaterial, typeof(Material), false); item.groundUseMapHeight = EditorGUILayout.Toggle("Use Height?", item.groundUseMapHeight); item.groundOffset = EditorGUILayout.FloatField("Ground Offset", item.groundOffset); EditorGUI.indentLevel--; EditorGUILayout.Space(); } item.colliderCreate = EditorGUILayout.Toggle("Create Colliders?", item.colliderCreate); if (item.colliderCreate) { EditorGUI.indentLevel++; item.colliderHeightUseMapHeight = EditorGUILayout.Toggle("Use Height?", item.colliderHeightUseMapHeight); item.colliderHeight = EditorGUILayout.FloatField("Collider Height", item.colliderHeight); item.colliderOffset = EditorGUILayout.FloatField("Collider Offset", item.colliderOffset); EditorGUI.indentLevel--; EditorGUILayout.Space(); } item.wallCreate = EditorGUILayout.Toggle("Create Walls?", item.wallCreate); if (item.wallCreate) { EditorGUI.indentLevel++; item.wallUseMapHeight = EditorGUILayout.Toggle("Use Height?", item.wallUseMapHeight); item.wallMaterial = (Material)EditorGUILayout.ObjectField("Wall Material", item.wallMaterial, typeof(Material), false); item.wallOffset = EditorGUILayout.FloatField("Wall Offset", item.wallOffset); item.wallHeight = EditorGUILayout.FloatField("Wall Height", item.wallHeight); item.wallScaleUVHeight = EditorGUILayout.Toggle("Scale UV Height?", item.wallScaleUVHeight); item.wallInvert = EditorGUILayout.Toggle("Invert?", item.wallInvert); EditorGUI.indentLevel--; EditorGUILayout.Space(); } item.mapUniqueCreate = EditorGUILayout.Toggle("Store in Map_Unique?", item.mapUniqueCreate); // Copy and paste EditorGUILayout.Separator(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Copy All", standardButton)) { savedDetailsAll = instance.details; } var copyState = savedDetailsAll != null && savedDetailsAll.Length > 0; if (GUILayout.Button("Paste All", copyState ? standardButton : disabledButton) && copyState) { instance.details = savedDetailsAll; } EditorGUILayout.EndHorizontal(); // Show/Hide maps. Create Map EditorGUILayout.Separator(); EditorGUILayout.LabelField("Map", boldLabel); if (GUILayout.Button(instance.display ? "Hide Map" : "Show Map")) { instance.display = !instance.display; onView = true; } if (GUILayout.Button("Create Map")) { CreateMap(); } EditorUtility.SetDirty(instance); }
private void CreateMesh(Transform parent, MapTileDetails details, MapScriptableObject.CellType cellType) { if (!details.groundCreate && !details.wallCreate && !details.colliderCreate) { return; } System.Func <int, bool> compareFunc = (i) => instance.GetCellType(i, (int)cellType); HashSet <int> free = new HashSet <int>(); var cellSize = instance.cellsize; var length = instance.length; var width = instance.width; var clength = length + 1; var cornerDirect = new[] { 0, 1, clength, clength + 1 }; for (var i = 0; i < length * width; i++) { if (compareFunc(i)) { free.Add(i); } } var freekeys = free.ToArray(); foreach (var freeIndex in freekeys) { // get neighboring if (free.Contains(freeIndex)) { var tiles = GetNeighboringTiles(freeIndex, free, compareFunc); var mesh = new Mesh(); var boxes = new List <Box>(); var vertexCount = 0; var offset = Vector3.zero; if (details.groundCreate) { boxes = GetGroundBoxes(tiles); offset = AdjustGroundBoxes(boxes, details, ref vertexCount); AddVertices(mesh, boxes); BuildMainMesh(mesh, boxes, 0); } if (details.wallCreate) { var wallboxes = GetWallBoxes(tiles, details.wallInvert); AdjustWallBoxes(wallboxes, details, offset, ref vertexCount); AddVertices(mesh, wallboxes); BuildMainMesh(mesh, wallboxes, 1); } var go = new GameObject("Object", typeof(MeshRenderer), typeof(MeshFilter)); var gotransform = go.transform; go.isStatic = true; go.layer = details.layer; gotransform.parent = parent; gotransform.position = offset; var mats = new List <Material>(); if (details.groundCreate) { mats.Add(details.groundMaterial); } if (details.wallCreate) { mats.Add(details.wallMaterial); } go.GetComponent <MeshRenderer>().sharedMaterials = mats.ToArray(); go.GetComponent <MeshFilter>().sharedMesh = mesh; if (details.colliderCreate) { var colliderHeight = details.colliderHeight + (details.colliderHeightUseMapHeight ? instance.height : 0f); var colliderOffset = new Vector3(0f, details.colliderOffset - (details.groundOffset + (details.groundUseMapHeight ? instance.height : 0f)), 0f); foreach (var b in boxes) { var item = new GameObject("Collider", typeof(BoxCollider), typeof(UnityEngine.AI.NavMeshModifier)); var itemtransform = item.transform; item.isStatic = true; item.layer = details.layer; var corner = b.bottomLeft.position; var size = b.size; size.y = colliderHeight; itemtransform.parent = gotransform; itemtransform.position = corner + offset + colliderOffset; var collider = item.GetComponent <BoxCollider>(); collider.size = size; collider.center = size * 0.5f; var ai = item.GetComponent <UnityEngine.AI.NavMeshModifier>(); ai.overrideArea = true; ai.area = details.areaType; } } } } if (details.mapUniqueCreate) { foreach (var f in freekeys) { instance.SetCellType(f, (int)MapScriptableObject.CellType.Empty); } } }