滚动的商店,scroll view

本文介绍了一个基于Unity的游戏商店界面实现方案,通过动态生成上下滚动视图来展示不同物品及其详细信息,包括名称、价格和数量等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

这里写图片描述

商店物品是上下滚动,点击物品,会弹出介绍它的界面,并且物品的名称,数量,单价会进行相应的变化
首先创建一个Panel,做项目的过程中发现最好以平面做基石,这样里面的东西无论深度多少,最后改一下Panel的深度就可以了
这里写图片描述
这是重点,动态生成的滚动视图将会出现在Grid下,我这个scroll view实际上是一个panel,挂上了Scroll View
这个滚动是上下滚动,所以grid中弄成了Vertical
这里写图片描述
宽和高是图片的大小,Row Limit是说一列有四个,
核心代码

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Cell{
    public int itemId = 0;//Id号
    public string iconName = string.Empty;//物品的图片
    public string name = string.Empty;//物品的名称
    public int price = 0;//价格
    public int itemCount = 0;//数量

    public Cell(int id, string icN, string na, int pri, int Count){
        itemId = id;
        iconName = icN;
        name = na;
        price = pri;
        itemCount = Count;
    }
}
public class shangdiangrid : MonoBehaviour {
    public GameObject cellPre;//物品的预设体
    public UIGrid grid;
    public GameObject itemInfo;//点击物品图片后弹出的介绍框

    List<Cell> cellConfigs = new List<Cell>(); 

    Cell tempCell = new Cell(0, string.Empty, string.Empty, 0, 0);//分别对应(ID,图片,名称,价格,数目)

    void Awake(){//前提是图集中的图片名称要跟下面对的上,"1160008[1]"这个,其他属性自己设置
        cellConfigs.Add(new Cell(1001, "1160008[1]", "环首刀",500, 10));
        cellConfigs.Add(new Cell(1002, "1160010[1]", "唐刀", 800, 20));
        cellConfigs.Add(new Cell(1003, "1160015[1]", "太极刀", 1000, 30));
        cellConfigs.Add(new Cell(1004, "1160020[1]", "云头刀", 1200, 40));
        cellConfigs.Add(new Cell(1005, "1120013[1]", "远古手镯", 500, 50));
        cellConfigs.Add(new Cell(1006, "1120014[1]", "悲鸣手镯", 800, 60));
        cellConfigs.Add(new Cell(1007, "1120015[1]", "幻影手镯", 1000, 70));
        cellConfigs.Add(new Cell(1008, "1130005[1]", "结界手镯", 1200, 80));
        cellConfigs.Add(new Cell(1009, "1130006[1]", "熊皮甲", 500, 90));
        cellConfigs.Add(new Cell(1010, "1130012[1]", "凌罗衣", 800, 100));
        cellConfigs.Add(new Cell(1011, "1130015[1]", "铁锁环甲", 1000, 110));
        cellConfigs.Add(new Cell(1012, "1140004[1]", "霜月衣", 1200, 120));
    }

    // Use this for initialization
    void Start () {
        itemInfo.SetActive (false);//介绍框在未点击时处于隐藏状态

        for (int i = 0; i < cellConfigs.Count; i++) {//遍历着实例化创建
            GameObject cell = Instantiate (cellPre) as GameObject;
            cell.transform.parent = grid.transform;//把物品一个个放到Grid的下面
            cell.transform.localScale = Vector3.one;//设定每个图片的规格都是1

            cell.transform.Find("icon").GetComponent<UISprite>().spriteName
                = cellConfigs[i].iconName;
            cell.transform.localPosition = new Vector3(0,0,0);//确保全部显示,有时候位置不对,即使产生了也不会显示
            UIEventListener.Get(cell).onClick = showItemInfo;//点击,弹出介绍框,这样不用在button那里设置了

        }

        grid.Reposition ();

    }
    //这是显示介绍框的方法
    public void showItemInfo(GameObject sprite){
        string spriteName = sprite.transform.Find ("icon").GetComponent<UISprite> ().spriteName;
        for (int i = 0; i < cellConfigs.Count; i++) {
            if(cellConfigs[i].iconName.Equals(spriteName)){
                itemInfo.SetActive(true);
                itemInfo.transform.Find("Icon").GetComponent<UISprite>().spriteName = cellConfigs[i].iconName;
                itemInfo.transform.Find("Name").GetComponent<UILabel>().text = cellConfigs[i].name;
                itemInfo.transform.Find("Price").GetComponent<UILabel>().text = cellConfigs[i].price.ToString();
                itemInfo.transform.Find("Count").GetComponent<UILabel>().text = cellConfigs[i].itemCount.ToString();

                GameObject sellButObj = GameObject.Find("Sell") ;
                UIEventListener.Get(sellButObj).onClick = sellItemFunc;

                tempCell = cellConfigs[i];

                break;
            }
        }
    }

    void sellItemFunc(GameObject obj){

        Debug.Log (obj.name);
        if (tempCell.itemCount > 0) {
            tempCell.itemCount -= 1;
            itemInfo.SetActive (false);
        }
    }


    // Update is called once per frame
    void Update () {
        itemInfo.transform.Find("Count").GetComponent<UILabel>().text = tempCell.itemCount.ToString();
    }
}

我这里把每个物体做成了预设体,
至于list,用它不要忘了加上using System.Collections.Generic;
这里写图片描述
注意图片标签的名字要跟代码里的一样,否则会找不到

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值