Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Feb 07, 2021 at 11:11 AM by DiasPTW for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DiasPTW · Feb 06, 2021 at 01:55 PM · listgrid based gamecards

How do I swap gameObjects on a list by clicking in the UI?

I'm making a game where the player moves a certain piece by rearranging "movement cards" in order to create the path he wants the piece to take to reach the end goal. The problem is, I don't know how to do the rearranging part.

Currently, I have a UI element that has a fixed amount of Images that change sprite according to the list information and are given a random order and I'd like to know how I could make the player rearrange said pieces by clicking or dragging on the images and swapping them with the other images [example in the image]. (Swap those two pieces) alt text Here's the code I have right now, and I honestly have no idea where to go from here. I'm very new to programming and I've been teaching myself only through experimenting so please don't go too hard on me :P

 void DarPeçasNecessarias() //Gives the exact amount of necessary pieces to finish the level
     {
         if (direçao.x > 0)
         {
             numRight = (int) direçao.x;
             for (int i = 0; i < numRight; i++)
             {
                 Peçass.Add("Right");
             }
             if (direçao.y > 0)
             {
                 numUp = (int) direçao.y;
                 for (int i = numRight; i < numRight + numUp; i++)
                 {
                     Peçass.Add("Up");
                 }
             }
             else if (direçao.y < 0)
             {
                 numDwn = (int) - direçao.y;
                 for (int i = numRight; i < numRight + numDwn; i++)
                 {
                     Peçass.Add("Down");
                 }
             }
             else { numDwn = 0; numUp = 0; }
 
         }
         else if (direçao.x < 0)
         {
             numLft = (int) - direçao.x;
             for (int i = 0; i < numLft; i++)
             {
                 Peçass.Add("Left");
             }
             if (direçao.y > 0)
             {
                 numUp = (int) direçao.y;
                 for (int i = numLft; i < numLft + numUp; i++)
                 {
                     Peçass.Add("Up");
                 }
             }
             else if (direçao.y < 0)
             {
                 numDwn = (int) - direçao.y;
                 for (int i = numLft; i < numLft + numDwn; i++)
                 {
                     Peçass.Add("Down");
                 }
             }
             else { numDwn = 0; numUp = 0; }
         }
         else { numLft = 0; numRight = 0; }
         //-----------------
         numPeçasNecessarias = numRight + numLft + numUp + numDwn;
         DarPeçasExtra();
     }
 
     void DarPeçasExtra() //Gives extra pieces
     {
         for (int i = numPeçasNecessarias; i < NumPeças; i++)
         {
             int nrg = Random.Range(1,5);
             //Debug.Log(nrg);
             if (nrg == 1)
             {
                 Peçass.Add("Up");
                 numUp++;
             }
             else if (nrg == 2)
             {
                 Peçass.Add("Down");
                 numDwn++;
             }
             else if (nrg == 3)
             {
                 Peçass.Add("Left");
                 numLft++;
             }
             else
             {
                 Peçass.Add("Right");
                 numRight++;
             }
         }
 
         ShuffleList();
     }
 
     void ShuffleList() //Shuffles the List
     {
         string Temp = "Temp";
         for (int i = 0; i < Peçass.Count; i++)
         {
             int rnd = Random.Range(0, Peçass.Count);
             Temp = Peçass[rnd];
             Peçass[rnd] = Peçass[i];
             Peçass[i] = Temp;
         }
         FillImages(); 
     }
 
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.P))
         {
             canMove = !canMove;
         }
 
         if (canMove)
         {
             if (!isRunning)
             {
                 StartCoroutine(AplicarMovimento());
             }
             
         }else { StopCoroutine(AplicarMovimento()); isRunning = false; }
 
         AbrirHotbar();
         FillImages();
     }
 
     void AbrirHotbar()//Opens the "hotbar" where all the movement cards are located
     {  
         if (Input.GetMouseButtonDown(0))
         {
             Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
             RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);
 
             if (hit.transform.gameObject.CompareTag("TilePeça") && !Hotbar.activeSelf)
             {
                 Hotbar.SetActive(true);
             }
             else if (hit.transform.gameObject.CompareTag("TilePeça") && Hotbar.activeSelf)
             {
                 Hotbar.SetActive(false);
             }
         }
     } 
 
     void FillImages() // Changes the sprite of the images in the Hotbar
     {
         for (int i = 0; i < Peçass.Count; i++)
         {
             Transform botao = Hotbar.transform.GetChild(i);
             
             if (Peçass[i] == "Up")
             {
                 botao.GetComponent<Image>().sprite = Up;
             }else if (Peçass[i] == "Down")
             {
                 botao.GetComponent<Image>().sprite = Down;
             }
             else if (Peçass[i] == "Left")
             {
                 botao.GetComponent<Image>().sprite = Left;
             }
             else if (Peçass[i] == "Right")
             {
                 botao.GetComponent<Image>().sprite = Right;
             }
         }
     }


help.png (55.9 kB)
Comment
Add comment · Show 1
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 sath · Feb 06, 2021 at 02:53 PM 0
Share

You can use the Re-orderable List from Unity-UI-Extensions

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by AbandonedCrypt · Feb 06, 2021 at 03:03 PM

Get the list index of both the source object and a temp variable, then swap them by index.

 List<Object> list = new List<Object>();
 int target, source; //index of target and source object
 Object tmp; //temporary store the target object
 
 void SwapObjects()
 {
     tmp = list[target];
     list[target] = list[source];
     list[source] = tmp;
 }

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 DiasPTW · Feb 06, 2021 at 10:10 PM 0
Share

Thank you so much!

Follow this Question

Answers Answers and Comments

115 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

Related Questions

A node in a childnode? 1 Answer

Voting System Using List (C#) 1 Answer

Limit the size/capacity of a list 3 Answers

How do I swap two gameObjects order in a list? 2 Answers

can somebody help for the algorithm or code how implement this crossword math? 1 Answer


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