Author Topic: How to skip items on UICenterOnChild  (Read 6335 times)

singh029

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 28
    • View Profile
How to skip items on UICenterOnChild
« on: March 22, 2015, 08:55:47 PM »
Hi, Im trying to figure out a way to skip items in a scrollview if the item is not unlocked.

The UICenterOnChild currently centers on every element when scrolling.
But id like it to skip centering items that arent unlocked and center on the next unlocked item.

When i run the folowing code, it doesnt do anything. Im pretty sure i have to utilize CenterOn(transform). But im unsure how.

  1.  
  2. public static List<bool> ItemLock;
  3.         public static string curItem;
  4.         public List<UISprite> itemList;
  5.         public UICenterOnChild items;
  6.         private int selecteditem;
  7.  
  8. void Update ()
  9.         {
  10.                 Invoke ("ItemSelection", .01F);
  11. }
  12.  
  13. void ItemSelection()
  14.         {
  15.                 switch (items.centeredObject.tag)
  16.                 {
  17.                 case "Apples":
  18.                 {
  19.                         selecteditem= 1;
  20.                         if (ItemLock[1] == true)
  21.                         {
  22.                                 curItem = "Apples";
  23.                         }
  24.                         else{
  25.                                 items.CenterOn(itemList[NextUnlockedItem()].transform);
  26.                         }
  27.                         break;
  28.                 }
  29.                 case "Bananas":
  30.                 {
  31.                         selecteditem= 2;
  32.                         if (ItemLock[2] == true)
  33.                         {
  34.                                 curItem = "Bananas";
  35.                         }
  36.                         else{
  37.                                 items.CenterOn(itemList[NextUnlockedItem()].transform);
  38.                         }
  39.                         break;
  40.                 }
  41.         case "RocketLauncher":
  42.                 {
  43.                         selecteditem= 3;
  44.                         if (ItemLock[3] == true)
  45.                         {
  46.                                 curItem = "Rocket";
  47.                         }
  48.                         else{
  49.                                 items.CenterOn(itemList[NextUnlockedItem()].transform);
  50.                         }
  51.                         break;
  52.                 }
  53.        }
  54. }
  55.  
  56.  
  57.         int NextUnlockedItem()
  58.         {
  59.                 int nextavailable = 0;
  60.                 while(ItemLock[selecteditem] == false)
  61.         {
  62.                         nextavailable = selecteditem;
  63. nextavailable++;
  64.                 }
  65.                 return nextavailable;
  66.         }
  67.  


Please save me!
thank you!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to skip items on UICenterOnChild
« Reply #1 on: March 23, 2015, 09:45:03 AM »
Write your own UICenterOnChild-like script that will ignore elements accordingly. You will want to modify the Recenter() function that determines the closest valid child (line 115).

singh029

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: How to skip items on UICenterOnChild
« Reply #2 on: March 25, 2015, 05:13:30 AM »
Thank you so much! i finally got it!
I made copies of all the scripts referenced and then made the following changes.
Only thing i changed is whats in red.

for (int i = 0, imax = trans.childCount, ii = 0; i < imax; ++i)
      {
         Transform t = trans.GetChild(i);
         if (!t.gameObject.activeInHierarchy) continue;
         float sqrDist = Vector3.SqrMagnitude(t.position - pickingPoint);
         
         if (sqrDist < min && t.GetComponent<UISprite>().color != Color.grey)
         {
            min = sqrDist;
            closest = t;
            index = i;
            ignoredIndex = ii;
         }
         ++ii;
      }

Also in UICenterOnClick i changed this as well

if (center.enabled && transform.GetComponent<UISprite>().color != Color.grey)