- Home /
How do I select a group full of sprites, between two points?
Hi everyone. Currently, what I am trying to do is select a group full of sprites by clicking on one point first, which is the green block. Then a mouse down on the red point as the second point. After that the red, green, and all the grey points are selected in-game and changed. All I am going to do is change the image they are rendering. What I am really having trouble on is finding how I get green, red, and the ones in a square selected so I can change them. Thank you in advance!
Answer by TBART82 · Oct 11, 2017 at 11:47 AM
Hello. I have come up with a solution for your question. Just follow the steps provided and you will successfully be able to get a list of sprites within two points which you select!
Step 1. Add a tag called "Tile" to each of your tiles.
Step 2. Create a C# script called "TileData" and add that to each of your tiles as well. Place the following code into that script:
using UnityEngine;
using UnityEngine.EventSystems;
public class TileData : MonoBehaviour, IPointerClickHandler {
// This is an EventSystem function which will detect a click on UI.
// This only works for the UI element it's attached to. This is why
// you add it to each of your tiles.
public void OnPointerClick(PointerEventData eventData)
{
// Accessing the TileManager script. We will make this in the next step.
TileManager tm = GameObject.Find("GameMaster").GetComponent<TileManager>();
// Accessing the 'AddSprite' function of the 'TileManager' to add the selected object.
tm.AddTile(eventData.pointerPress.gameObject);
}
}
Step 3. Create an empty gameobject in the hierarchy. Name it "GameMaster".
Step 4. Create a C# script called "TileManager" and attach it to the "GameMaster" object. Place the following code into that script.
using System.Collections.Generic;
using UnityEngine;
public class TileManager : MonoBehaviour {
// A list of the two selected tiles
public List<GameObject> selected;
// A list of all the tiles
public List<GameObject> allTiles;
// A list of the tiles in-between the two selection points
public List<GameObject> betweenTiles;
private void Start()
{
// Creates an array of every tile and adds it to the 'allTiles' array.
GameObject[] tile = GameObject.FindGameObjectsWithTag("Tile");
for (int i = 0; i < tile.Length; i++)
{
allTiles.Add(tile[i]);
}
}
// This function that adds the tiles within the selection
// points to the 'betweenTiles' list.
public void AddTile(GameObject tile)
{
selected.Add(tile);
// When the selected points is = 2
if (selected.Count == 2)
{
// Calculate the minimum coordinate
Vector2 minCoord = new Vector2(Mathf.Min(selected[0].transform.localPosition.x, selected[1].transform.localPosition.x), Mathf.Min(selected[0].transform.localPosition.y, selected[1].transform.localPosition.y));
// Calculate the maximum coordinate
Vector2 maxCoord = new Vector2(Mathf.Max(selected[0].transform.localPosition.x, selected[1].transform.localPosition.x), Mathf.Max(selected[0].transform.localPosition.y, selected[1].transform.localPosition.y));
// Loop through each tile in 'allTiles' and see if the tile is = or
// within the minCoord and maxCoord.
for (int i = 0; i < allTiles.Count; i++)
{
if (allTiles[i].transform.localPosition.x >= minCoord.x && allTiles[i].transform.localPosition.x <= maxCoord.x && allTiles[i].transform.localPosition.y >= minCoord.y && allTiles[i].transform.localPosition.y <= maxCoord.y)
{
// Adds to the 'betweenTiles' list
betweenTiles.Add(allTiles[i]);
}
}
}
}
}
Completed. Once this step is finished, you should be able to run the game, select two points, and everything within that square area will be added to a list where you can do what you want with each tile. I'm sorry I can't fully explain what the code does, but there is quite a lot of comments to assist understanding. If you need any other help, be sure to let me know. Thanks,
TBART82!
@TBART82 Fantastic! Thank you so much and sorry for taking a long time to respond and accept your answer.
Your answer
Follow this Question
Related Questions
how to check presence of sprite on a UI button? 1 Answer
Can't Get .enabled to Work for Children Sprites 2 Answers
SpriteRenderer is gone when I change in script. 1 Answer
OnMouseDown with Tags 1 Answer
How to Get Next One On The Que? 0 Answers