Esempio n. 1
0
    public bool EqualsTile(TileBase tile)
    {
        if (tile == null || !(tile is Map2DTile))
        {
            return(false);
        }
        Map2DTile other = (Map2DTile)tile;

        return(sprite == other.sprite);
    }
Esempio n. 2
0
    public static void PopulatePalette(string assetPath)
    {
        bool dirty = false;

        GameObject  paletteObject = AssetDatabase.LoadAssetAtPath <GameObject>(assetPath);
        GridPalette settings      = null;
        Tilemap     map           = paletteObject.GetComponentInChildren <Tilemap>();

        foreach (Object obj in AssetDatabase.LoadAllAssetsAtPath(assetPath))
        {
            if (obj is GridPalette)
            {
                settings = (GridPalette)obj;
            }
        }
        if (settings == null)
        {
            settings            = ScriptableObject.CreateInstance <GridPalette>();
            settings.name       = EditorUtils.NameFromPath(assetPath);
            settings.cellSizing = GridPalette.CellSizing.Automatic;
            AssetDatabase.AddObjectToAsset(settings, paletteObject);
            dirty = true;
        }

        string    name           = EditorUtils.NameFromPath(assetPath);
        string    localDirectory = EditorUtils.LocalDirectoryFromPath(assetPath);
        string    pngDirectory   = localDirectory.Substring(0, localDirectory.LastIndexOf('/'));
        string    tileDirectory  = pngDirectory + "/Tiles/" + name;
        string    texturePath    = pngDirectory + "/" + name + ".png";
        Texture2D tex            = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePath);

        for (int y = 0; y < tex.height / Map.TileSizePx; y += 1)
        {
            for (int x = 0; x < tex.width / Map.TileSizePx; x += 1)
            {
                Vector3Int pos          = new Vector3Int(x, (tex.height / Map.TileSizePx - y - 1), 0);
                TileBase   existingTile = map.GetTile <TileBase>(pos);

                string    tileName = NameForTile(name, x, y);
                Map2DTile newTile  = AssetDatabase.LoadAssetAtPath <Map2DTile>(tileDirectory + "/" + tileName + ".asset");

                if (!newTile.EqualsTile(existingTile))
                {
                    dirty = true;
                    map.SetTile(pos, newTile);
                }
            }
        }

        if (dirty)
        {
            AssetDatabase.SaveAssets();
        }
    }
Esempio n. 3
0
    public override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height)
    {
        Map2DTile targetTile = (Map2DTile)target;

        if (targetTile == null || targetTile.sprite == null)
        {
            return(null);
        }
        else
        {
            Editor editor = CreateEditor(targetTile.sprite);
            //string localDir = EditorUtils.LocalDirectoryFromPath(assetPath);
            //string parentDir = localDir.Substring(0, localDir.LastIndexOf('/'));
            //string name = EditorUtils.NameFromPath(assetPath);
            //int lastIndex = name.LastIndexOf('[');
            //string parentName = lastIndex > 0 ? name.Substring(0, lastIndex) : name;
            //string path = parentDir + "/" + parentName + ".png";

            return(editor.RenderStaticPreview(assetPath, subAssets, width, height));
        }
    }
Esempio n. 4
0
    public static void CreateTiles(string assetPath)
    {
        string localDirectory = EditorUtils.LocalDirectoryFromPath(assetPath);
        string name           = EditorUtils.NameFromPath(assetPath);
        string genericPath    = localDirectory + "/Tiles";
        string tilesDirPath   = genericPath + "/" + name;

        if (!AssetDatabase.IsValidFolder(genericPath))
        {
            AssetDatabase.CreateFolder(localDirectory, "Tiles");
        }
        if (!AssetDatabase.IsValidFolder(tilesDirPath))
        {
            AssetDatabase.CreateFolder(genericPath, name);
        }

        Object[] spriteObjects = AssetDatabase.LoadAllAssetRepresentationsAtPath(assetPath);
        foreach (Object spriteObject in spriteObjects)
        {
            if (!spriteObject.GetType().IsAssignableFrom(typeof(Sprite)))
            {
                continue;
            }
            Sprite    sprite     = (Sprite)spriteObject;
            string    spritePath = tilesDirPath + "/" + sprite.name + ".asset";
            Map2DTile tile       = AssetDatabase.LoadAssetAtPath <Map2DTile>(spritePath);
            if (tile == null)
            {
                tile = ScriptableObject.CreateInstance <Map2DTile>();
                AssetDatabase.CreateAsset(tile, spritePath);
            }
            tile.sprite       = sprite;
            tile.name         = sprite.name;
            tile.color        = Color.white;
            tile.colliderType = Tile.ColliderType.Sprite;
            tile.transform    = Matrix4x4.identity;
        }
    }