Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created December 8, 2021 23:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtdekker/bf28bc127e00e0341981cc7d67323c65 to your computer and use it in GitHub Desktop.
Save kurtdekker/bf28bc127e00e0341981cc7d67323c65 to your computer and use it in GitHub Desktop.
Editor script to make a grid of sprites, aka, poor man's tilemap
using UnityEngine;
using UnityEditor;
// @kurtdekker
// cheap and cheerful grid-of-sprites maker
// this is an editor-time application
// it assumes you will further manipulate this grid and DIRTY AND SAVE THE SCENE
// be sure to drop this into an Editor folder!
public class MakeGridOfSprites : EditorWindow
{
public int across = 15;
public int down = 10;
public Sprite sprite;
public Vector2 spacing = new Vector2( 1, 1);
[MenuItem("GameObject/Create Sprite Grid")]
static void Init()
{
MakeGridOfSprites window = (MakeGridOfSprites)EditorWindow.GetWindow(typeof(MakeGridOfSprites));
window.Show();
}
void OnGUI()
{
GUILayout.Label("Make a grid of sprites in scene - @kurtdekker", EditorStyles.boldLabel);
across = EditorGUILayout.IntField("Tiles across", across);
down = EditorGUILayout.IntField("Tiles across", down);
sprite = (Sprite)EditorGUILayout.ObjectField( sprite, typeof( Sprite), true);
spacing = EditorGUILayout.Vector2Field( "Spacing", spacing);
if (GUILayout.Button( "Build the grid!!"))
{
var parent = new GameObject( "MakeGridOfSprites-Parent").transform;
for (int j = 0; j < down; j++)
{
for (int i = 0; i < across; i++)
{
var pos = new Vector2( i * spacing.x, j * spacing.y);
// center it
pos -= new Vector2( across - 1, down - 1) * 0.5f;
string tilename = System.String.Format( "Tile ({0},{1})", i, j);
var tile = new GameObject( tilename);
tile.transform.SetParent( parent);
tile.transform.position = pos;
var spriteRenderer = tile.AddComponent<SpriteRenderer>();
spriteRenderer.sprite = sprite;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment