Shift thru Selected Tile / Sprite from Sprite-Sheet

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.

There are many coordinate systems in play here. You are conflating a lot of them and perhaps not aware of some of them.

For one there’s the pixels of your Texture, imported as a Sprite. Those are just pixels. Sure, we can say 64x64.

Second when you import a Sprite object, it has a property called Pixels Per Unit, which maps how many of those pixels will by default show up in one world unit of distance in Unity.

After that you can of course scale the SpriteRenderer showing that Sprite in the world.

Sprites can also go into UI.Image components under a Canvas. Those Image components are ALWAYS scaled to some size via the RectTransform. The sprite is presented at that size.

The UI.Image components are presented by the Canvas and the Canvas decides how to map them to the screen space or pixel space of the final display via the Canvas Scaler.

And of course EVERY transform in the way can technically be scaled to non-unity value (other than (1,1,1)), although this is generally bad practice, but it does happen.

So… obviously WAAAAAAAAAY too many coordinate systems to have in play at once. Total nightmare.

Instead, back up a little bit and go work through some very basic Inventory tutorials on Youtube to see which of these coordinates systems they just “go with” or “adjust” and also how they configure stuff to show properly, to learn how to “When in Rome, act like a Roman.”

You might want to also study up on general Unity UI stuff, because that’s critical to know, otherwise everything you do will behave mysteriously all the time, as your screen shape changes, your window size changes, your monitor resolution changes, etc.

1 Like

I understand the Canvas part.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class InventoryHandler : MonoBehaviour
{
    public GameObject inventoryCanvas;
    public GameObject inventorySlot;
    public GameObject selectedItem;

    private bool isIconDrag = false;
    private RectTransform slotLocket;
    // Start is called before the first frame update
    void Start()
    {
        GameObject inventoryItem = Instantiate(inventorySlot) as GameObject;
        inventoryItem.transform.SetParent(inventoryCanvas.transform, false);
        inventoryItem.transform.position = new Vector2(100, 100);
        selectedItem = inventoryItem;
        slotLocket = (RectTransform)selectedItem.transform;
    }
    // Update is called once per frame
    void Update()
    {
       
        Rect rect = new Rect(slotLocket.rect.x, slotLocket.rect.y, slotLocket.rect.width, slotLocket.rect.height);

        if (Input.GetMouseButtonDown(0) && rect.Contains(Input.mousePosition))
        {
            isIconDrag = true;
            slotLocket = (RectTransform)selectedItem.transform;
        }
        if (Input.GetMouseButtonUp(0))
        {
            isIconDrag = false;
        }
        if (isIconDrag == true)
        {
            Vector2 screenPosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
            selectedItem.transform.position = screenPosition;
        }
      

    }
}

This is what I started to work with as an inventory system yet a the tile-map i shown is going to be the inventory icon. I hick-billy the drag and drop yet i am experience some difficulties

I can Prefab the Image and Load into as a GameObject thru Inspector.

Again, if you’re gonna start moving things to random magic numbers, you’re ALWAYS going to have trouble with the default Canvas setups. Nobody manipulates stuff like this in Unity and thinks it was a good idea and does it a second time.

And again, these coordinate systems MEAN NOTHING to each other. Like they mean NOTHING… it is like asking “How many grains of sand will it take me to eat this candy bar?” Nonsense, Grade-A nonsense.

Go bone up on the standard ways interacting with Unity UI or else you are just not going to get anything done here.

Well to a expert they see the faults. I am no expert yet kinda accept your advice.

The screen resolution is different on all system cause of all the monitor size and driver capacity in how many pixels it can hold… like 800x600px a low resolution.
If I program a object with a set size with coorderiants it will be placed depending what the software is running (game) on what machine is host the software.

But this is a learning experince and a practice. I will learn the ninja art of unity.