Unity 制作滚动物品界面

Unity 制作滚动物品界面:

_}ZK809[6R39(S86V1(2JJY

第一种方式: (panel的方式实现)

    1. 创建一个GameObject(A),添加UIPanel和UIScrollView
           2. 物品放入A中,添加Drag Scroll View 和 box Colliders(一般是添加一个Grid,物体放入Grid,把Grid放入A)
   

注意事项:

     1. 动态加载的物品坐标不正确,使用Grid组件Reposititon(),进行更新

代码实现:

using UnityEngine;

using System.Collections;

using System.Collections.Generic;



public class RootPanel : MonoBehaviour {



    public UIScrollView view;

    public GameObject items;

    public GameObject Grid;

    public List<GameObject> itemList = new List<GameObject>();



    public void Start() 

    {

        foreach (var i in itemList)

        {

            Grid.GetComponent<UIGrid>().AddChild(i.transform);

        }



        view.Scroll(1f);

    }



    public void NextPage() 

    {

        view.MoveRelative(new Vector3(200, 0, 0));

        view.RestrictWithinBounds(false, true, false);

    }

    public void PreviousPage()

    {

        view.MoveRelative(new Vector3(-200, 0, 0));

        view.RestrictWithinBounds(false, true, false);

    }



    //向前面滚动

    public void Next() 

    {



        view.Scroll(1f);

    }



    //向后面滚动

    public void Previous() 

    {

        view.Scroll(-1f);

    }



    //添加

    public void Add() 

    {

       

       var clone = (GameObject)Instantiate(items,Vector3.zero,Quaternion.identity);

        clone.transform.parent = Grid.transform;

        clone.transform.localScale = new Vector3(1, 1, 1);

        itemList.Add(clone);

        

        Grid.GetComponent<UIGrid>().AddChild(clone.transform);

        Grid.GetComponent<UIGrid>().Reposition();

    }



    //删除

    int index = 0;

    public void Remove() 

    {

        var grid =  Grid.GetComponent<UIGrid>();

        Destroy(itemList[index].gameObject);

        index++;



        grid.repositionNow = true;

        

    }



}

 

 

源代码: http://yunpan.cn/cyP9DQ22JpYJk  提取码 7131

你可能感兴趣的:(unity)