Author Topic: How to set UIWrapContent defalut position ?  (Read 5789 times)

tunied

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
How to set UIWrapContent defalut position ?
« on: July 28, 2015, 02:46:34 AM »
Hi sorry to bother you.

currently i want use UIWrapContent to make an list (300+ item, and all item have same width&height).

follow the example, i have successful make the list display&scroll. but each time the list is start from 0 (realIndex).

how do i set the default display index?

e.g.  make item realIndex = 120 show in the middle of the scrollview.

if the scrollview can display 3 item . so ,when i click play button in Unity Editor. should display 121,120,119.

for now it display 1,0,-1.


ps: my setting for the UIWrapContent  is

Item Height : 200
Range Limit 0 to 300
Cull Content = true;

ScrollView: 
Offset x=0 y=0
Center x=0 y=0
Size x=200 y=600
Softness x=4 y=4

Thanks









ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to set UIWrapContent defalut position ?
« Reply #1 on: July 28, 2015, 06:37:28 PM »
You get to choose the minIndex and maxIndex. You also get to choose the position. If you want it to start in a specific place, do just that -- change the wrap content's transform.localPosition.

tunied

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: How to set UIWrapContent defalut position ?
« Reply #2 on: July 28, 2015, 09:35:08 PM »
Hi

i try this code:

  1. public class MyWrapContentExt : MonoBehaviour
  2. {
  3.         public UIWrapContent warpContent;
  4.  
  5.         void Start ()
  6.         {
  7.                 warpContent.onInitializeItem += OnInitializeItem;
  8.                 warpContent.minIndex = 0;
  9.                 warpContent.maxIndex = 300;
  10.                 transform.localPosition = new Vector3 (0, -1500f, 0);
  11.                 //transform.localPosition = new Vector3 (0, 1500f, 0);
  12.         }
  13.        
  14.         private void OnInitializeItem (GameObject go, int wrapIndex, int realIndex)
  15.         {
  16.                 UILabel labelTF = go.transform.FindChild ("ItemIndexTF").GetComponent<UILabel> ();
  17.                 labelTF.text = "" + realIndex;
  18.         }
  19. }
  20.  

both 1500f or -1500f  are not show any thing in the screen.



Some Detail:

my scroll view size is : 200x600
so i create 5 item under the wrap content (200x5 = 1000 > 600)
the item is only an 200px square no scale with an label on it.

my hierarchy look like:

UI Root
   |
   |--Camera
   |--Scroll View
             |
             |--Unlimited List (Attach UI Wrap Content Script & MyWrapContentExt Script)
                      |
                      |--Item 0
                      |--Item 1
                      |--Item 2
                      |--Item 3
                      |--Item 4



i also try put

  1. warpContent.onInitializeItem += OnInitializeItem;
  2. warpContent.minIndex = 0;
  3. warpContent.maxIndex = 300;
  4. transform.localPosition = new Vector3 (0, -1500f, 0);
  5.  

in Awake() or Update() functino , but none of them are working :(

Thanks




 

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to set UIWrapContent defalut position ?
« Reply #3 on: July 31, 2015, 09:25:16 PM »
I was talking of changing localPosition of actual items... You change it on something else entirely, possibly the parent?

tunied

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: How to set UIWrapContent defalut position ?
« Reply #4 on: August 03, 2015, 04:27:46 AM »
Hi Aren

Still can't make it work :(

is this possible you can take a look at my demo?

thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to set UIWrapContent defalut position ?
« Reply #5 on: August 05, 2015, 08:33:36 AM »
1. You never set the "Items" list so there was nothing to iterate through.
2. You forgot to inform the scroll view that you've changed the position of stuff within and that it should update its own.
3. You should have set "onInitializeItem" in the Awake() function so that your items would get initialized properly when you hit Play.

Other than that your script works fine.
  1. using UnityEngine;
  2.  
  3. public class MyWrapContentExt : MonoBehaviour
  4. {
  5.         public UIWrapContent warpContent;
  6.         public GameObject[] items;
  7.        
  8.         void Awake ()
  9.         {
  10.                 warpContent.onInitializeItem += OnInitializeItem;
  11.         }
  12.  
  13.         void Start ()
  14.         {
  15.                 for (int index = 0; index < items.Length; index++)
  16.                 {
  17.                         GameObject item = items[index];
  18.                         item.transform.localPosition = new Vector3(0, 2000f + index * 200f, 0);
  19.                 }
  20.                 GetComponentInParent<UIScrollView>().ResetPosition();
  21.                 warpContent.WrapContent(true);
  22.         }
  23.        
  24.         private void OnInitializeItem (GameObject go, int wrapIndex, int realIndex)
  25.         {
  26.                 UILabel labelTF = go.transform.FindChild ("ItemIndexTF").GetComponent<UILabel> ();
  27.                 labelTF.text = "" + realIndex;
  28.         }
  29. }
  30.  
You may need to modify UIWrapContent.WarpContent function to add the boolean parameter I added. Without it your items won't be updated properly (but they will be in the right place and visible). The modified UIWrapContent function is as follows:
  1.         /// <summary>
  2.         /// Wrap all content, repositioning all children as needed.
  3.         /// </summary>
  4.  
  5.         public virtual void WrapContent (bool forceUpdate = false)
  6.         {
  7.                 float extents = itemSize * mChildren.Count * 0.5f;
  8.                 Vector3[] corners = mPanel.worldCorners;
  9.                
  10.                 for (int i = 0; i < 4; ++i)
  11.                 {
  12.                         Vector3 v = corners[i];
  13.                         v = mTrans.InverseTransformPoint(v);
  14.                         corners[i] = v;
  15.                 }
  16.                
  17.                 Vector3 center = Vector3.Lerp(corners[0], corners[2], 0.5f);
  18.                 bool allWithinRange = true;
  19.                 float ext2 = extents * 2f;
  20.  
  21.                 if (mHorizontal)
  22.                 {
  23.                         float min = corners[0].x - itemSize;
  24.                         float max = corners[2].x + itemSize;
  25.  
  26.                         for (int i = 0, imax = mChildren.Count; i < imax; ++i)
  27.                         {
  28.                                 Transform t = mChildren[i];
  29.                                 float distance = t.localPosition.x - center.x;
  30.  
  31.                                 if (distance < -extents)
  32.                                 {
  33.                                         Vector3 pos = t.localPosition;
  34.                                         pos.x += ext2;
  35.                                         distance = pos.x - center.x;
  36.                                         int realIndex = Mathf.RoundToInt(pos.x / itemSize);
  37.  
  38.                                         if (minIndex == maxIndex || (minIndex <= realIndex && realIndex <= maxIndex))
  39.                                         {
  40.                                                 t.localPosition = pos;
  41.                                                 UpdateItem(t, i);
  42.                                         }
  43.                                         else
  44.                                         {
  45.                                                 allWithinRange = false;
  46.                                                 if (forceUpdate) UpdateItem(t, i);
  47.                                         }
  48.                                 }
  49.                                 else if (distance > extents)
  50.                                 {
  51.                                         Vector3 pos = t.localPosition;
  52.                                         pos.x -= ext2;
  53.                                         distance = pos.x - center.x;
  54.                                         int realIndex = Mathf.RoundToInt(pos.x / itemSize);
  55.  
  56.                                         if (minIndex == maxIndex || (minIndex <= realIndex && realIndex <= maxIndex))
  57.                                         {
  58.                                                 t.localPosition = pos;
  59.                                                 UpdateItem(t, i);
  60.                                         }
  61.                                         else
  62.                                         {
  63.                                                 allWithinRange = false;
  64.                                                 if (forceUpdate) UpdateItem(t, i);
  65.                                         }
  66.                                 }
  67.                                 else if (mFirstTime || forceUpdate) UpdateItem(t, i);
  68.  
  69.                                 if (cullContent)
  70.                                 {
  71.                                         distance += mPanel.clipOffset.x - mTrans.localPosition.x;
  72.                                         if (!UICamera.IsPressed(t.gameObject))
  73.                                                 NGUITools.SetActive(t.gameObject, (distance > min && distance < max), false);
  74.                                 }
  75.                         }
  76.                 }
  77.                 else
  78.                 {
  79.                         float min = corners[0].y - itemSize;
  80.                         float max = corners[2].y + itemSize;
  81.  
  82.                         for (int i = 0, imax = mChildren.Count; i < imax; ++i)
  83.                         {
  84.                                 Transform t = mChildren[i];
  85.                                 float distance = t.localPosition.y - center.y;
  86.  
  87.                                 if (distance < -extents)
  88.                                 {
  89.                                         Vector3 pos = t.localPosition;
  90.                                         pos.y += ext2;
  91.                                         distance = pos.y - center.y;
  92.                                         int realIndex = Mathf.RoundToInt(pos.y / itemSize);
  93.  
  94.                                         if (minIndex == maxIndex || (minIndex <= realIndex && realIndex <= maxIndex))
  95.                                         {
  96.                                                 t.localPosition = pos;
  97.                                                 UpdateItem(t, i);
  98.                                         }
  99.                                         else
  100.                                         {
  101.                                                 allWithinRange = false;
  102.                                                 if (forceUpdate) UpdateItem(t, i);
  103.                                         }
  104.                                 }
  105.                                 else if (distance > extents)
  106.                                 {
  107.                                         Vector3 pos = t.localPosition;
  108.                                         pos.y -= ext2;
  109.                                         distance = pos.y - center.y;
  110.                                         int realIndex = Mathf.RoundToInt(pos.y / itemSize);
  111.  
  112.                                         if (minIndex == maxIndex || (minIndex <= realIndex && realIndex <= maxIndex))
  113.                                         {
  114.                                                 t.localPosition = pos;
  115.                                                 UpdateItem(t, i);
  116.                                         }
  117.                                         else
  118.                                         {
  119.                                                 allWithinRange = false;
  120.                                                 if (forceUpdate) UpdateItem(t, i);
  121.                                         }
  122.                                 }
  123.                                 else if (mFirstTime || forceUpdate) UpdateItem(t, i);
  124.  
  125.                                 if (cullContent)
  126.                                 {
  127.                                         distance += mPanel.clipOffset.y - mTrans.localPosition.y;
  128.                                         if (!UICamera.IsPressed(t.gameObject))
  129.                                                 NGUITools.SetActive(t.gameObject, (distance > min && distance < max), false);
  130.                                 }
  131.                         }
  132.                 }
  133.                 mScroll.restrictWithinPanel = !allWithinRange;
  134.         }