GameObject SaveCurrentGameObject()
        {
            string targetFolder = EditorUtility.OpenFolderPanel("Save Floortile", lastFolder, "");

            targetFolder = SpriteTileGenerator.EnsureAssetDataPath(targetFolder);
            lastFolder   = targetFolder;
            //SpriteTileGenerator.PrefabFromSprite(floorSprite, targetFolder);
            var  mat = SpriteTileGenerator.MaterialFromTex(targetSprite.texture, targetFolder);
            Mesh mesh;

            switch (tileType)
            {
            case TileType.Block:
                mesh = CreateBlockMesh(targetSprite, additionalSprite_top);
                break;

            case TileType.ThinWall:
                mesh = SpriteTileGenerator.CreateMeshFromSprite(targetSprite);
                TransformMesh(mesh, Quaternion.Euler(270f, 90f, 90f), Vector3.up * 0.5f);
                break;

            case TileType.Slope:
                mesh = CreateSlopeMesh(targetSprite, additionalSprite_side, additionalSprite_top, height);
                break;

            default:
                mesh = SpriteTileGenerator.CreateMeshFromSprite(targetSprite);
                break;
            }
            return(SaveGameobject(mesh, mat, targetFolder, tileName));
        }
        void CreatePreviewObject(Sprite sprite, TileType type)
        {
            if (previewGO)
            {
                DestroyImmediate(previewGO);
            }
            if (GraphicsSettings.renderPipelineAsset)
            {
                previewMat = new Material(GraphicsSettings.renderPipelineAsset.defaultMaterial.shader);
            }
            else
            {
                previewMat = new Material(Shader.Find("Standard"));
            }
            previewMat.mainTexture = sprite.texture;
            previewMat.EnableKeyword("_ALPHATEST_ON");
            Mesh previewMesh;

            switch (type)
            {
            case TileType.Floor:
                previewMesh = SpriteTileGenerator.CreateMeshFromSprite(sprite);
                break;

            case TileType.ThinWall:
                previewMesh = SpriteTileGenerator.CreateMeshFromSprite(sprite);
                TransformMesh(previewMesh, Quaternion.Euler(270f, 90f, 90f), Vector3.up * 0.5f);
                break;

            case TileType.Block:
                previewMesh = CreateBlockMesh(sprite, additionalSprite_top);
                break;

            case TileType.Slope:
                previewMesh = CreateSlopeMesh(sprite, additionalSprite_side, additionalSprite_top, height);
                break;

            case TileType.Extrude:
                previewMesh = CreateExtrudeMesh(sprite, height);
                break;

            default:
                previewMesh = SpriteTileGenerator.CreateMeshFromSprite(sprite);
                break;
            }
            previewGO = new GameObject(tileName, typeof(MeshFilter), typeof(MeshRenderer));
            previewGO.GetComponent <MeshFilter>().mesh       = previewMesh;
            previewGO.GetComponent <MeshRenderer>().material = previewMat;
            previewGO.hideFlags = HideFlags.HideAndDontSave;
            //return previewGO;
        }
Beispiel #3
0
        override public void OnInspectorGUI()
        {
            var t = target as TileAtlas;

            if (GUILayout.Button("Create Dictionary From List"))
            {
                t.GetDictionaryFromList();
            }
            DrawDefaultInspector();
            if (GUILayout.Button("Add all GameObjects from Folder"))
            {
                string path     = EditorUtility.OpenFolderPanel("Folder to copy Gameobjects from", "Assets/", "");
                string homePath = AssetDatabase.GetAssetPath(t);
                homePath = homePath.Substring(0, homePath.Length - 6); // .as set
                if (!AssetDatabase.IsValidFolder(homePath))
                {
                    AssetDatabase.CreateFolder(homePath.Substring(0, homePath.Length - t.name.Length - 1), t.name);
                    AssetDatabase.Refresh();
                }
                foreach (var go in GetAtPath <GameObject>(path, false))
                {
                    t.tiles.AddRange(TileObject.CreateTileObjectsAt(homePath, go));
                    EditorUtility.SetDirty(t);
                }
            }
            if (GUILayout.Button("Add all GameObjects from Subfolders"))
            {
                string path     = EditorUtility.OpenFolderPanel("Folder to copy Gameobjects from", "Assets/", "");
                string homePath = AssetDatabase.GetAssetPath(t);
                homePath = homePath.Substring(0, homePath.Length - 6); // .as set
                if (!AssetDatabase.IsValidFolder(homePath))
                {
                    AssetDatabase.CreateFolder(homePath.Substring(0, homePath.Length - t.name.Length - 1), t.name);
                    AssetDatabase.Refresh();
                }
                foreach (var go in GetAtSubFolders <GameObject>(path, false))
                {
                    t.tiles.AddRange(TileObject.CreateTileObjectsAt(homePath, go));
                    EditorUtility.SetDirty(t);
                }
            }
            if (GUILayout.Button("Create Tiles from Spritesheet"))
            {
                string path = EditorUtility.OpenFilePanelWithFilters("Spritesheet", "Assets", new string[] { "PNG", "png", "JPG", "jpg", "JPEG", "jpeg" });
                t.tiles.AddRange(SpriteTileGenerator.GenerateFromSpriteSheet(path, false));
                EditorUtility.SetDirty(t);
            }
            EditorGUILayout.HelpBox("Drag Tiles, Prefabs or Sprites here (not implemented yet)", MessageType.Info);
        }
        static Mesh CreateSlopeMesh(Sprite sprite, Sprite sideSprite, Sprite backSprite, float height)
        {
            //
            Mesh main = SpriteTileGenerator.CreateMeshFromSprite(sprite);

            Vector3[] verts = main.vertices;
            for (int i = 0; i <= 1; i++)
            {
                verts[i] += Vector3.up * height;
            }
            main.SetVertices(verts);
            main.RecalculateNormals();
            main.RecalculateTangents();

            if (sideSprite)
            {
                Mesh[] partials = new Mesh[4]; // two quads, two tris
                partials[0] = main;
                partials[1] = CreatePartialQuad(backSprite ?? sideSprite, height);
                TransformMesh(partials[1],
                              Quaternion.Euler(270f, 90f, 90f),
                              new Vector3(0f, 0.5f, 0.5f));

                partials[2] = CreatePartialTris(sideSprite, height);
                TransformMesh(partials[2],
                              Quaternion.Euler(90f, 90f, 0f),
                              new Vector3(0.5f, 0.5f, 0f));

                partials[3] = CreatePartialTris(sideSprite, height);
                TransformMesh(partials[3],
                              Quaternion.Euler(90f, 90f, 0f),
                              new Vector3(-0.5f, 0.5f, 0f));

                partials[3].triangles = new int[] { partials[3].triangles[2], partials[3].triangles[1], partials[3].triangles[0] };

                return(MeshCombine.CombineMeshes(partials));
            }
            else
            {
                return(main);
            }
            //return CreatePartialQuad(sprite, height);
        }
 static Mesh CreateBlockMesh(Sprite sprite, Sprite topSprite)
 {
     Mesh[] partials = new Mesh[5];
     for (int i = 0; i <= 3; i++)
     {
         partials[i] = SpriteTileGenerator.CreateMeshFromSprite(sprite);
         TransformMesh(partials[i],
                       Quaternion.Euler(270f, 90f + 90f * i, 90f),
                       Vector3.up * 0.5f
                       + Quaternion.Euler(0f, 90f * i, 0f).normalized *Vector3.forward * 0.5f);
     }
     if (topSprite)
     {
         partials[4] = SpriteTileGenerator.CreateMeshFromSprite(topSprite);
         TransformMesh(partials[4],
                       Quaternion.Euler(0f, 0f, 0f),
                       Vector3.up * 1f);
     }
     return(MeshCombine.CombineMeshes(partials));
 }