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 /
  • Help Room /
avatar image
0
Question by ethentianknight · Jan 02, 2017 at 08:42 PM · c#guilayout

Dynamically ordering a vertical layout group

I'm attempting to display an ordered list to the user of personnel inside a sector (labeled trisect). This is a vertical layout group, and for the rows I'm using gameobject prefabs that when disabled are added to a pool. That pool holds on to disabled game objects until I need another one of that type, as to keep the scene uncluttered and save some memory.

The pooling is causing the sort to not work. If I disable the pool, it will sort people in barracks by their STR as intended. Without, it appears to have no sorting effect whatsoever.

I am attempting to sort via setsiblingindex(int i) or setLastSibling(). This works initially, but when the pooled items are reused, the order is off. I've checked in debug to make sure that the soldier list sorts properly and even checked what values were sent in setsiblingindex(i). What it revealed is that sibling index 0 has the most strength, but when I looked at the list, it showed that item nearly halfway down the list.

Pool code:

 public GameObject AddPersonnelRow(Soldier sol)
     {
         GameObject personnelRow;
         if (_inactivePersonnelRows.Count < 1)
         {
             personnelRow = GameObject.Instantiate(personnelRowPrefab);
             personnelRow.transform.SetParent(personnelGrid);
             
         }
         else
         {
             personnelRow = _inactivePersonnelRows[0];
             personnelRow.SetActive(true);
             personnelRow.GetComponentInChildren<Toggle>().isOn = false;
             _inactivePersonnelRows.RemoveAt(0);
         }
         return personnelRow;
     }
 
     public void RemovePersonnelRow(GameObject pRow)
     {
         _inactivePersonnelRows.Add(pRow);
         pRow.SetActive(false);
         pRow.transform.SetAsLastSibling();
     }


Sort code:

 private void Sort()
         {
             List<TrisectRow> soldiers;
 
             switch (_trisect.trisect.station.Subtype)
             {
                 case Subtypes.Barracks:
                     soldiers = _trisectPersonnel
                         .OrderByDescending(x => x.soldier.strength).ToList();
                     break;
                 case Subtypes.Research:
                     soldiers = _trisectPersonnel
                         .OrderByDescending(x => x.soldier.mind).ToList();
                     break;
                 default:
                     soldiers = _trisectPersonnel
                         .OrderByDescending(x => x.soldier.level)
                         .ThenByDescending(x => x.soldier.hp)
                         .ThenByDescending(x => x.soldier.strength).ToList();
                     break;
             }
 
             for (int i = 0; i < soldiers.Count(); i++)
             {
                 soldiers[i].mono.transform.SetAsLastSibling();
             }
         }

TrisectRow class:

 public class TrisectRow
     {
         public GameObject mono { get; set; }
         public Soldier soldier { get; set; }
         public TrisectRow(GameObject mono, Soldier soldier)
         {
             this.mono = mono;
             this.soldier = soldier;
         }
     }

alt text

alt text

debugresults.png (27.9 kB)
display.png (10.4 kB)
Comment
Add comment · Show 6
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 TBruce · Jan 02, 2017 at 08:47 PM 1
Share

Can you display the TrisectRow class?

avatar image ethentianknight TBruce · Jan 02, 2017 at 08:51 PM 0
Share

@TBruce Added it. Soldier just has base stats, the mono object is just the unity row to display.

avatar image TBruce ethentianknight · Jan 02, 2017 at 09:38 PM 0
Share

I get your sort but I do not understand what you are doing here

 for (int i = 0; i < soldiers.Count(); i++)
 {
     soldiers[i].mono.transform.SetAsLastSibling();
 }

Also, how/where/when is AddPersonnelRow called and you have

 if (_inactivePersonnelRows.Count < 1)

which leads one to believe that _inactivePersonnelRows.Count will never be greater than zero gain if ever set to true.

Forgive me for the questions but I am trying to wrap my head around what you are doing with the amount of code provided.

Edit: I just refreshed an I see your added images, could you explain what is wrong/right and what it should be (and which sort method you used)?

Show more comments
Show more comments

1 Reply

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

Answer by TBruce · Jan 03, 2017 at 05:41 AM

Not sure what you are going for here but I am going to assume that you are displaying the separate rows on the game screen. If this is the case then you need to set the Y position for each of the rows. Something like the following [will differ slightly depending on your implementation] (this should be done after your switch/sort statement)

 for (int i = 0; i < soldiers.Count(); i++)
 {
     Vector3 position = soldiers[i].mono.transform.position;
     position.y = soldierPrefab.transform.position.y + (i * rowOffset);
     soldiers[i].mono.transform.position = position;
 }

transform.SetAsLastSibling() and transform.SetSiblingIndex() are only setting the transforms in the hierarchy, not on the screen.

If I understand what you want to do is that you want to sort your list then display each of the list objects on screen in their sorted order.

Your switch statement already sorts the TrisectRow list correctly, you just need to change the position of the objects according to their index.

In the sample code I listed above I placed my prefab location at the top. I the calculated the offset from one row to the next and placed it in my test example.

I have included two Unity packages, one uses the Unity UI and the other uses TextMesh that shows slightly modified versions of your script (the main purpose was just to show the sorting).


trisecttest.zip (10.2 kB)
trisecttestui.zip (12.9 kB)
Comment
Add comment · Show 2 · 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 TBruce · Jan 04, 2017 at 02:25 AM 0
Share

Hi @ethentianknight, Did you find this answer helpful?

avatar image ethentianknight TBruce · Jan 06, 2017 at 08:30 PM 0
Share

Yes, this worked for my setup. Thank you for the help!

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

306 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 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 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 change GUI label size 1 Answer

Loading Game Crash 1 Answer

Multiplayer Hiding Layer just for Local Player 0 Answers

Create a Prefab directly inside a GameObject (not SetParent or transform.parent) 0 Answers

EditorGUILayout.ObjectField and array 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