void SaveAsTile(GameObject go) { var builder = FindObjectOfType <LevelBuilder>(); if (!builder) { Debug.LogWarning("Create a level builder in scene."); return; } if (!builder.tileSet) { Debug.LogWarning("Add a tileatlas to builder."); return; } var newTile = TileObject.CreateNewTileFileFromPrefabs(go); builder.tileSet.tiles.Add(newTile); builder.tileSet.GetDictionaryFromList(); }
private void OnGUI() { if (!CheckForRequirements()) { return; } EditorGUILayout.BeginHorizontal(); painting = GUILayout.Toggle(painting, "painting", "Button"); EditorGUILayout.LabelField(new GUIContent(layerIndex + ":" + paletteIndex), GUILayout.Width(30)); EditorGUILayout.EndHorizontal(); brushRotation = EditorGUILayout.Vector3Field("Brush Direction", brushRotation); //if (painting) { //if (layerIndex < -1 || layerIndex >= t.tileSet.layers.Count) return; layerIndex = Mathf.Clamp(layerIndex, -1, t.tileSet.layers.Count - 1); paletteIndex = Mathf.Clamp(paletteIndex, 0, layerIndex == -1 ? (t.tileSet.tiles.Count - 1) : (t.tileSet.layers[layerIndex].layerObjects.Count - 1)); paletteIndex = Mathf.Max(0, paletteIndex); if (layerIndex == -1) { selectedTileGuid = t.tileSet.tiles[paletteIndex].guid; } else if (t.tileSet.layers[layerIndex]?.layerObjects.Count > 0) { selectedTileGuid = t.tileSet.layers[layerIndex]?.layerObjects[paletteIndex]?.guid; } if (!string.IsNullOrEmpty(selectedTileGuid)) { t.tileSet.TileFromGuid.TryGetValue(selectedTileGuid, out selectedTile); } if (!selectedTile) { selectedTile = t.tileSet.tiles[0]; selectedTileGuid = selectedTile.guid; } } EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("Grid Visibility "); rasterVisibility = (RasterVisibility)EditorGUILayout.EnumPopup(rasterVisibility); rasterColor = EditorGUILayout.ColorField(rasterColor); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); targetHeigth = EditorGUILayout.IntField("Heigth: ", targetHeigth); // activeLayer = EditorGUILayout.IntField("Layer:", activeLayer); EditorGUILayout.EndHorizontal(); Event e = Event.current; if (e.type == EventType.DragUpdated || e.type == EventType.DragPerform) { Rect myRect = GUILayoutUtility.GetRect(100, 40, GUILayout.ExpandWidth(true), GUILayout.ExpandHeight(true)); GUI.Box(myRect, "Drag and Drop Prefabs to this Box!"); if (myRect.Contains(e.mousePosition)) { if (e.type == EventType.DragUpdated) { DragAndDrop.visualMode = DragAndDropVisualMode.Move; } if (e.type == EventType.DragPerform) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; //Debug.Log(DragAndDrop.objectReferences.Length); for (int i = 0; i < DragAndDrop.objectReferences.Length; i++) { var draggedTile = DragAndDrop.objectReferences[i] as TileObject; if (!draggedTile) { var draggedGameObject = DragAndDrop.objectReferences[i] as GameObject; draggedTile = TileObject.CreateNewTileFileFromPrefabs(false, draggedGameObject); AssetDatabase.AddObjectToAsset(draggedTile, t.tileSet); } t.tileSet.tiles.Add(draggedTile); if (layerIndex != -1) { t.tileSet.layers[layerIndex].layerObjects.Add(draggedTile); } } t.tileSet.GetDictionaryFromList(); DragAndDrop.AcceptDrag(); e.Use(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(t.tileSet)); } } } DrawPalette(t.tileSet, Event.current); if (GUILayout.Button("Select Atlas")) { Selection.activeObject = t.tileSet; } if (GUILayout.Button("Clear Level")) { t.ClearLevel(); } }
public static TileObject TileFromSprite(Sprite sprite) { var go = PrefabFromSprite(sprite); return(TileObject.CreateNewTileFileFromPrefabs(go)); }
public static List <TileObject> GenerateFromSpriteSheet(string path, bool isRelativePath) { string filePath = isRelativePath ? path : ("Assets" + path.Substring(Application.dataPath.Length)); List <TileObject> export = new List <TileObject>(); var tex = AssetDatabase.LoadAssetAtPath <Texture2D>(filePath); string folderPath = filePath.Substring(0, filePath.LastIndexOf("/") + 1) + tex.name; EnsureFolders(filePath, tex.name); Material mat = MaterialFromTex(tex, folderPath); Sprite[] sprites = AssetDatabase.LoadAllAssetsAtPath(filePath).OfType <Sprite>().ToArray(); //Mesh mesh = new Mesh(); //mesh.subMeshCount = sprites.Length; //List<Vector3> allVerts = new List<Vector3>(); //List<Vector2> allUVs = new List<Vector2>(); //List<ContainerForSubMesh> submeshes = new List<ContainerForSubMesh>(); for (int index = 0; index < sprites.Length; index++) { var sprite = sprites[index]; Debug.Log("Creating " + sprite.name); //foreach (var vert in sprite.vertices) // Debug.Log("vert: (" + vert.x.ToString("0.00") + ";" + vert.y.ToString("0.00") + ")"); //foreach (var uv in sprite.uv) // Debug.Log("uv: (" + uv.x.ToString("0.00") + ";" + uv.y.ToString("0.00") + ")"); //return; Vector3[] verts = new Vector3[sprite.vertices.Length]; Vector2[] uvs = new Vector2[sprite.uv.Length]; for (int i = 0; i < verts.Length; i++) { verts[i] = new Vector3(sprite.vertices[i].x, 0f, sprite.vertices[i].y); uvs[i] = sprite.uv[i]; } int[] faces = new int[sprite.triangles.Length]; for (int i = 0; i < faces.Length; i++) { //Debug.Log(index + " : face " + sprite.triangles[i]); faces[i] = sprite.triangles[i];// + vertOffset; } Mesh mesh = new Mesh(); mesh.name = sprite.name; mesh.SetVertices(verts); mesh.SetUVs(0, uvs); mesh.SetTriangles(faces, 0); AssetDatabase.CreateAsset(mesh, folderPath + "/meshes/" + sprite.name + ".asset"); AssetDatabase.SaveAssets(); GameObject meshHolder = new GameObject(); var filter = meshHolder.AddComponent <MeshFilter>(); filter.sharedMesh = AssetDatabase.LoadAssetAtPath <Mesh>(folderPath + "/meshes/" + sprite.name + ".asset"); var rend = meshHolder.AddComponent <MeshRenderer>(); rend.sharedMaterial = mat; var go = PrefabUtility.SaveAsPrefabAsset(meshHolder, folderPath + "/prefabs/" + sprite.name + ".prefab"); export.Add(TileObject.CreateNewTileFileFromPrefabs(go)); GameObject.DestroyImmediate(meshHolder); //submeshes.Add(new ContainerForSubMesh(verts, uvs, faces)); //allVerts.AddRange(verts); //allUVs.AddRange(uvs); } //mesh.SetVertices(allVerts); //mesh.SetUVs(0, allUVs); //for (int i = 0; i < submeshes.Count; i++) //{ // mesh.SetTriangles(submeshes[i].faces, i); // //submeshes[i].DrawDebugMesh(3f); //} //string savePath = filePath.Substring(0, filePath.LastIndexOf("/") + 1) + tex.name + "-mesh.asset"; //AssetDatabase.CreateAsset(mesh, savePath); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); return(export); }