Hello. I am somewhat new to Unity. I play around with it search high and low for snippet that solve parts of the problems i have. I have a sprite-sheet or a tilemap of images in side one image.
I want to create an Array of Limited amount of Sprite so I imagine it goes something like this ::
public Sprite spriteSheet;
public Sprite[] gridOutSprites = new Sprite[ Limit_Of_Sprite_From_Sheet]
Than I need a SpriteRender so I initialize it
private SpriteRenderer spriteRenderer;
void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
if (spriteRenderer.sprite == null)
spriteRenderer.sprite = gridOutSprites[selectedIndexedSpriteFromSheet];
}
That would load the sprite thru the renderer.
I looking to initialize a bounding box like a Rect so i can
if(Rect.Contains(SelectedIndex))
The reason I want to have a Rect is to indentify the bounds of the 2d object. I am still learning and would like an experts opinion. I am a bit slow but I catch on eventually.
Here is what I tried to do it is not finished.
{
public GameObject slotPrefab;
public int collectionLimit;
public Sprite emptySlot;
public Sprite adminCrystal;
private SpriteRenderer imgRender;
void Start()
{
imgRender = GetComponent<SpriteRenderer>();
if (imgRender.sprite == null)
{
imgRender.sprite = emptySlot;
}
Populate();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
changeInventoryIcon();
}
}
void changeInventoryIcon()
{
if (imgRender.sprite == emptySlot)
{
imgRender.sprite = adminCrystal;
}
else
{
imgRender.sprite = emptySlot;
}
}
void Populate()
{
GameObject[] slotObject = new GameObject[collectionLimit];
Rect[] objRect = new Rect[collectionLimit];
int index = 0;
while (index != slotObject.Length)
{
slotObject[index] = (GameObject)Instantiate(slotPrefab, transform);
objRect[index] = new Rect();
objRect[index].position = new Vector2(slotObject[index].transform.position.x, slotObject[index].transform.position.y);
objRect[index].width = 64;
objRect[index].height = 64;
index++;
}
}
I need to load them into ScrollView with a limit of 4 columns. I do with the Inspector and the Icon Size is 64x64px a square of 2. I kinda broke my code and my head is swimming.
I could just slice and dice the sprites from the sprite sheet and initialize each sprite individually. But that is not the question.