Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(196评价)
价格: 4039.00元
在黑暗之光的背包系统里的一个问题
sdewqazxcds发起了问答2017-07-21
6
回复
1291
浏览

老师对背包的做法是一个一个慢慢放上去,如图

但我觉得这样不好布局,所以用了动态加载,即添加了UIGrid

因为是动态加载,所以不能够用静态对象,所以我又加了个脚本MyGrid.cs

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

public class MyGrid : MonoBehaviour
{
    //自制Grid排序
    public GameObject uiGrid;

    private List<GameObject> GridItem = new List<GameObject>();
    private int itemCount = 20;

    void Awake ()
    {
        for (int i = 0; i < itemCount; i++)
        {
            GameObject _gridItem = NGUITools.AddChild(uiGrid, (GameObject)(Resources.Load("Inventory-Item-grid")));
            /*Resources.Load:使用这种方式加载资源,
             * 首先需要下Asset目录下创建一个名为Resources的文件夹,这个命名是U3D规定的方式,然后把资源文件放进去,
                当然也可以在Resources中再创建子文件夹,当然在代码加载时需要添加相应的资源路径,
             */

            _gridItem.name = "Inventory-Item-grid-" + i;
            GridItem.Add(_gridItem);
        }
    }

    public List<GameObject> GetGridItemList
    {
        get { return GridItem; }
    }
    
}

 

但是问题就来了,在45课时中,老师在Inventory.cs(背包信息)里面写了个 GetId 方法,里面遍历了itemGridList(背包方格信息),即inventory-item-grid-0x下挂载的脚本InventoryItemGrid.cs的个数,但是我用的是动态加载(用上面的脚本),所以print( itemGridList.Count ) = 0,所以没法遍历itemGridList,然后后面的步骤都无法运行,怎么办呢?

 

附:Inventory.cs

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Random = UnityEngine.Random;

public class Inventory : MonoBehaviour
{
    //管理背包信息
    public static Inventory _instance;
    public MyGrid myGrid;

    private TweenPosition tween;
    private int cointCount = 1000;//金币数量
    private List<InventoryItemGrid> itemGridList = new List<InventoryItemGrid>();//背包方格信息

 

    public UILabel coinNumberLabel;
    public GameObject InventoryItem;


    void Awake()
    {
        _instance = this;
        tween = this.GetComponent<TweenPosition>();
    }

    void Start()
    {
        //foreach (GameObject child in myGrid.GetGridItemList)
        //{
        //    print(child.gameObject.name);//获取方格名称
        //}
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.X))
        {
            GetId(Random.Range(1001,1004));
        }
    }

    //拾取到id的物品,并添加到物品栏里面
    //处理拾取物品的功能
    public void GetId(int id)
    {
        //第一步是查找在所有的物品中是否存在该物品
            //第二步如果存在,让num +1
            //第三步 不存在,查找空的方格,然后把新创建的InventoryItem放到这个空的方格里面
        InventoryItemGrid grid = null;
        foreach (InventoryItemGrid temp in itemGridList)//第一步
        {
            if (temp.id == id)
            {
                grid = temp;
                break;
            }
        }
        if (grid != null) //第二步
        {
            grid.PlusNumber(id);
        }
        else 
        {
            foreach (InventoryItemGrid temp in itemGridList)
            {
                if (temp.id == 0)//格子id为0,说明这是空的格子,把物品放到那个格子里
                {
                    grid = temp;
                    break;
                }
            }
            if (grid != null)//第三步
            {
                GameObject itemGo = NGUITools.AddChild(grid.gameObject, InventoryItem);//在grid(格子)加入子对象InventoryItem(物品)
                /*static public GameObject AddChild ( GameObject parent, GameObject prefab)
                 * 从预设生成物体并作为子物体添加给指定物体。
                 * 这个十分有用,尤其是动态生成场景或者菜单时,
                 * 添加子物体后需要自己处理子物体的localpostion和localrotation等,
                 * 这个方法把这些全部集成了,而且返回添加后的子物体,进一步操作也很方便啊
                 */
                itemGo.transform.localPosition = Vector3.zero;//itemGo为添加到格子里的物品
                grid.SetId(id);
                print(12);
            }
        }
    }

    public void Show()//显示
    {
        tween.PlayForward();//顺序播放
    }

    public void Hide()//隐藏
    {
        tween.PlayReverse();//倒序播放
    }
}

 

 

所有回复
  • siki 2017-07-21

    GridItem这里不是所有格子的list集合吗,可以遍历这个

    你可以给每一个格子身上添加一个脚本保存自身的id

    • sdewqazxcds 2017-07-21

      GridItem可以获取每个格子的 id 和 name,但不知道怎样发送到每个格子(Inventory-Item-grid)的脚本上(InventoryItemGrid.cs)

      (0) 回复
    • sdewqazxcds 2017-07-21

      回复 @ sdewqazxcds: 搞错了,GridItem只能get到name,id还不行

      (0) 回复
    还有-3条回复,点击查看
    你还没有登录,请先登录注册
  • siki 2017-07-21

    还有你说的静态对象是啥

    • sdewqazxcds 2017-07-21

      可能我的表述有问题,不是静态对象,是在Hierarchy面板下存在的对象

      (0) 回复
    还有-4条回复,点击查看
    你还没有登录,请先登录注册
  • sdewqazxcds 2017-07-21

    原来GridItem已经获取了所有方格对象的列表,只要 GetComponent<InventoryItemGrid> 就能获取InventoryItemGrid对象了,已解决 

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!