new ngui ScrollView03

ÇÁ·Î±×·¥ÀûÀ¸·Î ½ºÅ©·Ñºä¿¡ ItemÀ» Ãß°¡ Çغ¸ÀÚ.
»ç¿ë ¹æ¹ýÀº ´ÙÀ½°ú °°´Ù.

1. ¸Þ´º > NGUI > Create > 2D UI »ý¼º

2. ¸Þ´º > NGUI > Create > Scroll View
¶Ç´Â  Add Component > NGUI > Scroll View ÄÄÆ÷³ÍÆ®¸¦ Ãß°¡ÇÑ´Ù.

UI Root ÇÏÀ§¿¡ Scroll View ¿ÀºêÁ§Æ®¸¦ »ý¼ºÇÑ´Ù.
ScrollView¿¡ UIScrollView ÄÄÆ÷³ÍÆ®°¡ Ãß°¡µÈ´Ù.



3. Scroll View ¿ÀºêÁ§Æ® UIPanel¿¡¼­ Size Á¶Á¤ÇÑ´Ù.
°¡·Î ¹æÇâÀ¸·Î ½ºÅ©·Ñ Çϱâ À§ÇØ Movement´Â Horizontal·Î Á¤ÇÑ´Ù.

½ºÅ©·ÑºäÀÇ CellÀÇ Å©±â°¡ 128À̱⠶§¹®¿¡ Y »çÀÌÁ 128·Î ¼³Á¤Çß´Ù.




4. ¸Þ´º > NGUI  > Create > Grid ¸¦ »ý¼ºÇÑ´Ù.  ¼¿ÀÇ Å©±â´Â 128 X 128·Î ¼³Á¤ÇÑ´Ù.
Scroll View ÇÏÀ§¿¡ Grid ¿ÀºêÁ§Æ®¸¦ »ý¼ºÇÑ´Ù.





5. ¸Þ´º > NGUI > Create > WidgetÀ» Ãß°¡ÇÑ´Ù. Size¸¦ 128 X 128·Î ¼³ÇÑ´Ù.
Grid ÇÏÀ§¿¡ Widget ¿ÀºêÁ§Æ®¸¦ »ý¼ºÇÑ´Ù.

¿ÀºêÁ§Æ® À̸§À» Continer¿¡¼­ ItemÀ¸·Î ¼öÁ¤ÇÑ´Ù.



6.  Item¿¡¼­ Add Component > NGUI > Interaction > Drag Scroll View¸¦ Ãß°¡ÇÑ´Ù.



7. Item¿¡¼­ ¸Þ´º > NGUI > Attach > Collider¸¦ Ãß°¡ÇÑ´Ù.
Box ColliderÀÇ Å©±â´Â ÀÚµ¿À¸·Î 128 * 128 ¼³Á¤µÈ´Ù.



8. ¸Þ´º > NGUI > Create > Sprite ¸¦ Ãß°¡ÇÑ´Ù.
Size´Â  Cell Size¿Í ÀÏÄ¡ ½ÃŲ´Ù. ¿©±â¼­´Â 128 X 128ÀÌ´Ù.

µðÆúÆ® °ªÀ¸·Î blue ½ºÇÁ¶óÀÌÆ®¸¦ ¼±ÅÃÇÑ´Ù.


9. ¸Þ´º > NGUI > Create > Label À» Ãß°¡ÇÑ´Ù.

µðÆúÆ® °ªÀ¸·Î blue¸¦ ÀÔ·ÂÇÑ´Ù.



10. ÇÁ¸®ÆÕÀ» ¸¸µé±â À§ÇØ ItemÀ» Project ºä·Î µå·¡±× ÇÑ´Ù.
Hierarchy ºäÀÇ ItemÀº Á¦°ÅÇÑ´Ù.



11. ¾ÆÀÌÅÛÀ» Ãß°¡ÇÏ´Â ½ºÅ©¸³Æ®¸¦ Grid ³ëµåÀÇ ÄÄÆ÷³ÍÆ®·Î Ãß°¡ÇÑ´Ù.

  1. ScrollItem.cs¸¦ ¸¸µé¾î Grid ³ëµåÀÇ ÄÄÆ÷³ÍÆ®·Î Ãß°¡ÇÑ´Ù.
  2. ¾ÆÀÌÅÛÀÇ ÇÁ¸®ÆÕÀ» prefabItem¿¡ µå·¡±× ÇÑ´Ù.
  3. »óÀ§³ëµå ScrollVIew¸¦ µå·¡±× ÇÑ´Ù.




RepositionÀ¸·Î ¾ÆÀÌÅÛµéÀÌ °ãÄ¡Áö ¾Êµµ·Ï ÇÑ´Ù.

GetComponent<UIGrid>().Reposition()

scrollView º¯¼ö´Â ½ºÅ©·ÑÀÇ À§Ä¡¸¦ °¡Àå ¿ÞÂÊÀ̳ª À§¿¡ À§Ä¡ ½ÃÅ°±â À§ÇØ ÇÊ¿äÇÏ´Ù.

scrollView.GetComponent<UIScrollView>().ResetPosition();

Insert ÇÔ¼ö¿¡¼­ GetChildObj ÇÔ¼ö·Î "Sprite"¿Í "Label" ³ëµå¸¦ ±¸ÇÑ´Ù.
À̹ÌÁö¿Í ÅؽºÆ®¸¦ ¼öÁ¤ÇÑ´Ù.

//ScrollItem.cs
uusing UnityEngine;
using System.Collections;

public class ScrollItem : MonoBehaviour {
    //ScrollView prefab GameObject
    public GameObject prefabItem;
    public GameObject scrollView;

    bool runMakeItem = false;

    void Update()
    {
        if (runMakeItem == false)
        {
            MakeItem ();
            runMakeItem = true;
        }
    }

    void MakeItem()
    {
        Insert("blue", "blue");
        Insert ("gray", "gray");
        Insert ("Green", "green");
        Insert ("orang", "orange");
        Insert ("red", "red");
        Insert ("yellow", "yellow");

        //itemÀÌ °ãÄ¡Áö ¾Êµµ·Ï À§Ä¡¸¦ °è»êÇÑ´Ù.
        GetComponent<UIGrid>().Reposition();
        //item ½ÃÀÛ À§Ä¡¸¦ topÀ̳ª Á¦ÀÏ ¿ÞÂÊÀ¸·Î Á¤¿­ÇÑ´Ù.
        //scrollView.GetComponentInChildren< UIScrollView >().ResetPosition();
        scrollView.GetComponent<UIScrollView>().ResetPosition();
    }

    void  Insert(string imgName, string textName)
    {
        GameObject obj = Instantiate(prefabItem, new Vector3(0f, 0f, 0f), Quaternion.identity) as GameObject;
        obj.transform.parent = this.transform;
        obj.transform.localScale = new Vector3(1f, 1f, 1f);

        UISprite sprite = GetChildObj (obj, "Sprite").GetComponent<UISprite>();
        UILabel label = GetChildObj (obj, "Label").GetComponent<UILabel>();

        sprite.spriteName = imgName;
        label.text = textName;
    }

    //ÀÚ½Ä ¿ÀºêÁ§Æ® ¸®½ºÆ® ¾ò¾î¿À±â
    GameObject GetChildObj( GameObject source, string strName  )
    {
        Transform[] AllData = source.GetComponentsInChildren< Transform >();
        GameObject target = null;
        foreach( Transform Obj in AllData )
        {
            if( Obj.name == strName )
            {
                target = Obj.gameObject;
                break;
            }
        }
       
        return target;
    }
}

12. Item ÇÁ¸®ÆÕ¿¡ UIButton°ú ItemClick.cs ½ºÅ©¸³Æ®¸¦ ÄÄÆ÷³ÍÆ®·Î Ãß°¡ÇÑ´Ù.

UIButtton ÄÄÆ÷³ÍÆ® Ãß°¡ :  NGUI > Interaction > Button Ãß°¡

ItemClick.cs¸¦ Item ÄÄÆ÷³ÍÆ®·Î Ãß°¡ÇÑ´Ù.

Inspector ºä¿¡¼­ ItemClickÀ» UIButtoÀÇ OnClick - Notify·Î µå·¡±× ÇÑ´Ù.



ÄÄÆù³ÍÆ®·Î Ãß°¡ÇÑ ItemClickÀ» OnClickÀÇ Notify·Î µå·¡±×ÇÏ°í ³ª¸é ´ÙÀ½°ú °°ÀÌ µÈ´Ù.




//ItemClick.cs
using UnityEngine;
using System.Collections;

public class ItemClick : MonoBehaviour {
    int index;

    void Start() {
        index = 0;
       
        foreach (Transform child in this.transform.parent)
        {
            if (child == transform) {
                break;
            }
            index++;
        }
       
    }
   
    void OnClick()
    {
        UILabel label = transform.Find ("Label").GetComponent<UILabel>();
        Debug.Log (index + "¹ø ° ¾ÆÀÌÅÛ Å¬¸¯µÊ" + " color: " + label.text);
    }
}

Sart( )¿¡¼­ ÀÚÁ÷ÀÇ °¹¼ö¸¦ index·Î ¼³Á¤ÇÑ´Ù.
UIButtonÀÇ Å¬¸¯ À̺¥Æ®¸¦ ó¸®Çϱâ À§ÇØ OnClick( ) ÇÔ¼ö¿¡¼­ ItemÀÇ Label ¹®ÀÚ¿­À» Ãâ·ÂÇÕ´Ï´Ù.

14. ½ÇÇà




¼Ò½º :    ItemClick.cs                ScrollItem.cs

PS)
ItemÀÌ ScrollºäÀÇ Æгκ¸´Ù ÀÛÀº °æ¿ì ½ºÅ©·Ñ(µå·¡±×)ÀÌ ¾ÈµÇ¾î¾ß ÇÑ´Ù.

ÀÌ·± °æ¿ì, ½ºÅ©·ÑÀ» ¸·À»·Á¸é ´ÙÀ½°ú °°ÀÌ "Cancel Drag If Fits"¸¦ üũ ÇϸéµÈ´Ù.