Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Bleedblue1125 · Oct 11, 2017 at 01:03 AM · unity 5spritesselectiononmousedown

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!alt text

untitled.png (404 B)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bleedblue1125 · Oct 21, 2017 at 02:06 AM 0
Share

@TBART82 Fantastic! Thank you so much and sorry for taking a long time to respond and accept your answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

146 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges