void EditRoad() { if (GUILayout.Button("Add waypoint")) { editedRoad.AddWaypoint(editedRoad.WayPoints[editedRoad.WayPoints.Count - 1].transform.position, editedMap); } GUILayout.Label("Road sprites", EditorStyles.boldLabel); pack = EditorGUILayout.ObjectField(pack, typeof(RoadSpritePack), false) as RoadSpritePack; if (GUILayout.Button("Bake road")) { editedMap.Recovery(); editedMap.BakeRoad(editedRoad, pack); } }
public void BakeRoad(Road road, RoadSpritePack pack) { GameObject spriteGroup = new GameObject("RoadGraphics"); spriteGroup.transform.parent = road.transform; spriteGroup.transform.position = road.WayPoints[0].transform.position; GameObject temp; temp = new GameObject("road"); temp.transform.parent = spriteGroup.transform; temp.AddComponent <SpriteRenderer>().sortingOrder = -1; temp.transform.position = road.WayPoints[0].transform.position; SerializedObject serialized = new SerializedObject(this[FromVector(road.WayPoints[0].transform.position)]); SerializedProperty property = serialized.FindProperty("type"); property.intValue = (int)TileType.ROAD; serialized.ApplyModifiedProperties(); if (road.WayPoints == null || road.WayPoints.Count < 2) { return; } TileCoords wp1, wp2; TileCoords diff; for (int i = 0; i < road.WayPoints.Count; i++) { if (i == 0) { wp1 = FromVector(road.WayPoints[0].transform.position); wp2 = FromVector(road.WayPoints[1].transform.position); diff = wp2 - wp1; if (diff.x == 0) { temp.GetComponent <SpriteRenderer>().sprite = pack.vertical; } else { temp.GetComponent <SpriteRenderer>().sprite = pack.horizontal; } continue; } wp1 = FromVector(road.WayPoints[i - 1].transform.position); wp2 = FromVector(road.WayPoints[i].transform.position); diff = wp2 - wp1; int steps = Mathf.Abs(diff.x) > Mathf.Abs(diff.y) ? Mathf.Abs(diff.x) : Mathf.Abs(diff.y); Debug.Log(steps); TileCoords step = new TileCoords(diff.x / steps, diff.y / steps); Vector2 currPoint = ToVector(wp1); TileCoords coords = wp1; temp = new GameObject("road"); temp.transform.parent = spriteGroup.transform; temp.AddComponent <SpriteRenderer>().sortingOrder = -1; temp.transform.position = ToVector(wp2); serialized = new SerializedObject(this[wp2]); property = serialized.FindProperty("type"); property.intValue = (int)TileType.ROAD; serialized.ApplyModifiedProperties(); //temp.GetComponent<SpriteRenderer>().sortingLayerID = -1; if (i + 1 == road.WayPoints.Count) { if (diff.x == 0) { temp.GetComponent <SpriteRenderer>().sprite = pack.vertical; } else { temp.GetComponent <SpriteRenderer>().sprite = pack.horizontal; } } else if (i != 0) { temp.transform.position = ToVector(wp2); TileCoords wp3 = FromVector(road.WayPoints[i + 1].transform.position); TileCoords diff21 = wp2 - wp1, diff32 = wp3 - wp2; if (diff21.y > 0) { temp.GetComponent <SpriteRenderer>().sprite = diff32.x > 0 ? pack.DownRight : pack.DownLeft; } else if (diff21.y < 0) { temp.GetComponent <SpriteRenderer>().sprite = diff32.x > 0 ? pack.UpRight : pack.UpLeft; } else if (diff21.x > 0) { temp.GetComponent <SpriteRenderer>().sprite = diff32.y > 0 ? pack.UpLeft : pack.DownLeft; } else if (diff21.x < 0) { temp.GetComponent <SpriteRenderer>().sprite = diff32.y < 0 ? pack.DownRight : pack.UpRight; } } for (int v = 1; v < steps; v++) { serialized = new SerializedObject(this[coords + v * step]); property = serialized.FindProperty("type"); property.intValue = (int)TileType.ROAD; serialized.ApplyModifiedProperties(); temp = new GameObject("road"); temp.transform.parent = spriteGroup.transform; temp.AddComponent <SpriteRenderer>().sortingOrder = -1; temp.transform.position = ToVector(coords + v * step); if (diff.x == 0) { temp.GetComponent <SpriteRenderer>().sprite = pack.vertical; } else { temp.GetComponent <SpriteRenderer>().sprite = pack.horizontal; } } temp = new GameObject("road"); temp.transform.parent = spriteGroup.transform; temp.AddComponent <SpriteRenderer>().sortingOrder = -1; if (i + 1 == road.WayPoints.Count) { temp.transform.position = ToVector(coords + steps * step); if (diff.x == 0) { temp.GetComponent <SpriteRenderer>().sprite = pack.vertical; } else { temp.GetComponent <SpriteRenderer>().sprite = pack.horizontal; } } } }