I want to create a block-based puzzle game that generates blocks as they are required, however I cannot seem to find any good information on how to do something like this using Unity's new sprite system.
Ideally the following would occur:
- Generate a random number to determine the type of block
- Create a sprite object of correct block type
- Display block on screen
I currently have the following:
using UnityEngine;
using System.Collections;
public class Block : MonoBehaviour {
public SpriteRenderer blockSprite;
int row;
int column;
// Use this for initialization
void Start ()
{
row = 0;
column = 0;
}
// Update is called once per frame
void Update ()
{
// gravity check
}
public void GenerateNewBlock()
{
int randNumber = Random.Range(0, System.Enum.GetNames(typeof(BlockType)).Length);
Sprite sprite = new Sprite();
blockSprite = gameObject.GetComponent<SpriteRenderer>();
switch (randNumber)
{
case 0:
sprite = Sprite.Create(AssetsLoader.Instance.BlockSprites[BlockType.Water], new Rect (0, 0, 100, 100), new Vector2(0, 0), 100.0f);
break;
case 1:
sprite = Sprite.Create(AssetsLoader.Instance.BlockSprites[BlockType.Fire], new Rect (0, 0, 100, 100), new Vector2(0, 0), 100.0f);
break;
}
}
}
public enum BlockType
{
Water,
Fire
}
And an assets loader Singleton class to load in the textures once into memory
using UnityEngine;
using System.Collections.Generic;
public class AssetsLoader : MonoBehaviour
{
// instance variable to prevent multiple instances of this class
private static AssetsLoader instance;
// variables we want to retain state
public Dictionary<BlockType, Texture2D> BlockSprites;
// empty private constructor
private AssetsLoader() {}
// Property that holds the instance
public static AssetsLoader Instance
{
get
{
if (instance == null)
{
instance = new AssetsLoader();
}
return instance;
}
}
// Use this for initialization
void Start ()
{
BlockSprites = new Dictionary<BlockType, Sprite>();
Sprite newSprite;
newSprite = Resources.Load("Sprites/water block") as Texture2D;
BlockSprites.Add(BlockType.Water, newSprite);
newSprite = Resources.Load("Sprites/fire block") as Texture2D;
BlockSprites.Add(BlockType.Fire, newSprite);
}
}
Can someone please help me understand what needs to be done to create block objects in my scene. Ideally I would only use Sprite
objects but I think I might need to load the assets in as Texture
s in order to get them to display.