Esempio n. 1
0
        private void OnGUI()
        {
            m_OrgTexture      = EditorGUILayout.ObjectField(m_OrgTexture, typeof(Texture2D), false) as Texture2D;
            m_AnimationFrames = EditorGUILayout.IntField("Animation Frames", m_AnimationFrames);
            m_AnimationSpeed  = EditorGUILayout.FloatField("Animation Speed", m_AnimationSpeed);
            m_Type            = (EAutoTileType)EditorGUILayout.EnumPopup("AutoTile Type", m_Type);

            if (m_OrgTexture != null)
            {
                // precheck texture size is valid!
                if (!IsValid())
                {
                    EditorGUILayout.HelpBox("Texture size is not match!", MessageType.Error);
                    GUI.enabled = false;
                }

                if (GUILayout.Button("MakeAutoTile"))
                {
                    TextureImporter imp;
                    var             org_path = AssetDatabase.GetAssetPath(m_OrgTexture);

                    // make sure the texture is readable!
                    if (!m_OrgTexture.isReadable)
                    {
                        imp            = TextureImporter.GetAtPath(org_path) as TextureImporter;
                        imp.isReadable = true;
                        imp.SaveAndReimport();
                    }

                    Size cell_size;
                    if (m_Type == EAutoTileType.RPG_MAKER_XP)
                    {
                        cell_size = new Size(32);
                    }
                    else
                    {
                        cell_size = new Size(48);
                    }

                    var org_tex_region = new Texture2DRegion(m_OrgTexture);
                    var new_tex_region = new Texture2DRegion(cell_size.Scale(8 * m_AnimationFrames, 6));
                    FillAutoTileAnimations(org_tex_region, new_tex_region, cell_size, m_AnimationFrames, m_Type);

                    // save the reordered texture and reload it
                    var path = Path.Combine(Path.GetDirectoryName(org_path), Path.GetFileNameWithoutExtension(org_path) + "_Split.png");
                    path = AssetDatabase.GenerateUniqueAssetPath(path);
                    File.WriteAllBytes(path, new_tex_region.texture.EncodeToPNG());
                    AssetDatabase.ImportAsset(path);

                    // calc all sprite meta data
                    var sheet           = new SpriteMetaData[XP_AUTOTILE_MAP.Count * m_AnimationFrames];
                    var frame_cell_size = new Size((int)new_tex_region.rect.width / m_AnimationFrames, (int)new_tex_region.rect.height);

                    for (var i = 0; i < m_AnimationFrames; ++i)
                    {
                        var sub_rect = new_tex_region.rect.SubRectFromIdx(i, frame_cell_size);
                        for (var j = 0; j < XP_AUTOTILE_MAP.Count; ++j)
                        {
                            var meta = new SpriteMetaData
                            {
                                name      = string.Format("tile_{0}_{1}", i, j),
                                rect      = sub_rect.SubRectFromIdx(j, cell_size),
                                border    = Vector4.zero,
                                alignment = (int)SpriteAlignment.Center,
                                pivot     = new Vector2(0.5f, 0.5f)
                            };
                            sheet[i * XP_AUTOTILE_MAP.Count + j] = meta;
                        }
                    }

                    // reimport the texture to generate all sprites
                    imp                     = TextureImporter.GetAtPath(path) as TextureImporter;
                    imp.textureType         = TextureImporterType.Sprite;
                    imp.spritePixelsPerUnit = cell_size.width;
                    imp.spriteImportMode    = SpriteImportMode.Multiple;
                    imp.mipmapEnabled       = false;
                    imp.filterMode          = FilterMode.Point;
                    imp.spritesheet         = sheet;
                    imp.SaveAndReimport();

                    // get all the sprites
                    var objs = AssetDatabase.LoadAllAssetsAtPath(path);
                    var sprs = new List <Sprite>();
                    foreach (var obj in objs)
                    {
                        if (obj is Sprite)
                        {
                            sprs.Add(obj as Sprite);
                        }
                    }

                    // generate the auto tile asset
                    var auto_tile = ScriptableObject.CreateInstance <AutoTile>();

                    // use the last sprite as the default, to show in the palette.
                    auto_tile.m_DefaultSprite = sprs.GetSpriteByIdx(0, 47);

                    auto_tile.m_TilingRules = new List <AutoTile.TilingRule>(XP_RULE_MAP.Count);
                    for (var i = 0; i < XP_RULE_MAP.Count; ++i)
                    {
                        var rule = new AutoTile.TilingRule();
                        rule.m_Neighbors = XP_RULE_MAP[i];
                        rule.m_Sprites   = sprs.GetSpritesByIdx(m_AnimationFrames, i);
                        if (m_AnimationFrames > 1)
                        {
                            rule.m_AnimationSpeed = m_AnimationSpeed;
                            rule.m_Output         = AutoTile.TilingRule.OutputSprite.Animation;
                        }
                        else
                        {
                            rule.m_Output = AutoTile.TilingRule.OutputSprite.Single;
                        }
                        auto_tile.m_TilingRules.Add(rule);
                    }

                    var auto_tile_path = Path.Combine(Path.GetDirectoryName(org_path), Path.GetFileNameWithoutExtension(org_path) + "_RuleTile.asset");
                    auto_tile_path = AssetDatabase.GenerateUniqueAssetPath(auto_tile_path);

                    AssetDatabase.CreateAsset(auto_tile, auto_tile_path);
                }

                GUI.enabled = true;

                //if (GUILayout.Button("Test A2")) {
                //    MakeAutoTile_A2(@"E:\RPGMakerMV\Project1\img\tilesets\Inside_A2.png");
                //}
            }
        }
Esempio n. 2
0
        private void MakeAutoTile_A2(string img_path)
        {
            var txt_path = Path.Combine(Path.GetDirectoryName(img_path), Path.GetFileNameWithoutExtension(img_path) + ".txt");
            var names    = File.ReadAllLines(txt_path).Select(s => s.Split('|')[0]).ToArray();

            var rows      = 4;
            var cols      = 8;
            var cell_size = new Size(48);

            var org_tex = new Texture2D(2, 2);

            org_tex.LoadImage(File.ReadAllBytes(img_path));

            var org_tex_region = new Texture2DRegion(org_tex);
            var new_tex_region = new Texture2DRegion(cell_size.Scale(8 * cols, 6 * rows));

            for (var i = 0; i < rows * cols; ++i)
            {
                FillAutoTileTexture(org_tex_region.SubRegionFromIdx(i, cell_size.Scale(2, 3)),
                                    new_tex_region.SubRegionFromIdx(i, cell_size.Scale(8, 6)), cell_size, EAutoTileType.RPG_MAKER_MV);
            }

            // save the reordered texture and reload it
            var base_path = AssetDatabase.GUIDToAssetPath(AssetDatabase.CreateFolder("Assets", Path.GetFileNameWithoutExtension(img_path)));

            var path = Path.Combine(base_path, Path.GetFileNameWithoutExtension(img_path) + "_Split.png");

            path = AssetDatabase.GenerateUniqueAssetPath(path);
            File.WriteAllBytes(path, new_tex_region.texture.EncodeToPNG());
            AssetDatabase.ImportAsset(path);

            var sheet = new SpriteMetaData[rows * cols * XP_AUTOTILE_MAP.Count];

            for (var i = 0; i < rows * cols; ++i)
            {
                var sub_rect = new_tex_region.rect.SubRectFromIdx(i, cell_size.Scale(8, 6));
                for (var j = 0; j < XP_AUTOTILE_MAP.Count; ++j)
                {
                    var meta = new SpriteMetaData
                    {
                        name      = string.Format("{0}_{1}", names[i], j),
                        rect      = sub_rect.SubRectFromIdx(j, cell_size),
                        border    = Vector4.zero,
                        alignment = (int)SpriteAlignment.Center,
                        pivot     = new Vector2(0.5f, 0.5f)
                    };
                    sheet[i * XP_AUTOTILE_MAP.Count + j] = meta;
                }
            }

            // reimport the texture to generate all sprites
            var imp = TextureImporter.GetAtPath(path) as TextureImporter;

            imp.textureType         = TextureImporterType.Sprite;
            imp.spritePixelsPerUnit = cell_size.width;
            imp.spriteImportMode    = SpriteImportMode.Multiple;
            imp.mipmapEnabled       = false;
            imp.filterMode          = FilterMode.Point;
            imp.spritesheet         = sheet;
            imp.SaveAndReimport();

            // get all the sprites
            var objs = AssetDatabase.LoadAllAssetsAtPath(path);
            var sprs = new List <Sprite>();

            foreach (var obj in objs)
            {
                if (obj is Sprite)
                {
                    sprs.Add(obj as Sprite);
                }
            }

            // generate the auto tile assets
            var tiles = new List <AutoTile>();

            for (var i = 0; i < rows * cols; ++i)
            {
                var auto_tile = ScriptableObject.CreateInstance <AutoTile>();

                // use the last sprite as the default, to show in the palette.
                auto_tile.m_DefaultSprite = sprs.GetSpriteByName(string.Format("{0}_{1}", names[i], 47));

                auto_tile.m_TilingRules = new List <AutoTile.TilingRule>(XP_RULE_MAP.Count);
                for (var j = 0; j < XP_RULE_MAP.Count; ++j)
                {
                    var rule = new AutoTile.TilingRule();
                    rule.m_Neighbors = XP_RULE_MAP[j];
                    rule.m_Sprites   = new Sprite[] { sprs.GetSpriteByName(string.Format("{0}_{1}", names[i], j)) };
                    rule.m_Output    = AutoTile.TilingRule.OutputSprite.Single;
                    auto_tile.m_TilingRules.Add(rule);
                }

                var auto_tile_path = Path.Combine(base_path, names[i] + "_RuleTile.asset");
                auto_tile_path = AssetDatabase.GenerateUniqueAssetPath(auto_tile_path);

                AssetDatabase.CreateAsset(auto_tile, auto_tile_path);
                tiles.Add(auto_tile);
            }

            var palette = Utility.CreateNewPalette(base_path, Path.GetFileNameWithoutExtension(img_path));

            for (var i = 0; i < rows * cols; ++i)
            {
                var x = i % cols;
                var y = Mathf.FloorToInt(i / cols);
                palette.SetTile(new Vector3Int(x, y, 0), tiles[i]);
                EditorUtility.SetDirty(tiles[i]);
            }
        }