[Unity] UGUI背包系统(初级)

查看:1756 |回复:9 | 2016-6-4 08:07:37

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 大西几 于 2021-2-23 20:53 编辑

本帖最后由 仅为年时 于 2016-6-2 14:05 编辑


效果图放上(不断点击键盘G随机获取物品):
135149ec6apubmf4z0gz5y.jpg


此篇是接着上篇背包前奏来写,拖拽和射线检测部分就不再多说,包括后面的修改加上了交换、回初始位置等功能都是在那个基础上的扩展,各有所需各自想要的扩展也就不同,大家可以根据上一篇来自己扩展。
该篇主要来介绍一下背包的拾取功能
1、创建一个image用来作为背景,命名为Beibao,比如说如下图:
135135sotezopxht45pdxb.png.thumb.jpg


2、在Beibao下添加一个空对象,命名为Grid,并为其添加Gridlayout Group组件,调整大小,符合背景。
3、在Grid下创建一个image,命名为UCell,将其做成prefab,然后做满整个Grid,当然,数量自己控制,如下图:

4、为UCell做一个子对象,命名为UItem,UItem下包含一个text,也做成prefab。当然,大小要自己设置好,至少不能比UCell大。
5、准备几个图片资源,创建一个Resources文件夹,放到下面,内容自由发挥吧,比如下图:

6、开始写脚本:
创建一个C#脚本,命名为UPickup,关键代码如下:

[C#] [color=rgb(51, 102, 153) !important]纯文本查看 [color=rgb(51, 102, 153) !important]复制代码
[color=white !important]
[color=white !important]?


01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

using UnityEngine;

using System.Collections;

using UnityEngine.UI;


public class UPickup : MonoBehaviour {


        public GameObject[] cells;

        public GameObject instantiate;

        GameObject item;


        AudioClip Musics;

        Image[] Images;

        Image Imagesingle;


        Text index;

        int IndexInt = 0;

        string IndexStr = "";

        int RandomM = 0;

        string RandomStrInt = "";

        string RandomStr = "";


        // Use this for initialization

        void Start () {


        }


        // Update is called once per frame

        void Update () {


                if (Input.GetKeyDown (KeyCode.G)) {

                        Pickup ();

                }


        }


        public void Pickup(){


                bool isFind = false;


                RandomM = Random.Range (0,6);

                RandomStrInt = RandomM.ToString ();

                RandomStr = "Pictures/" + RandomStrInt;//路径

                print (RandomStr);


                item = Instantiate (instantiate, transform.position, transform.rotation) as GameObject;//获取预制物体

                Imagesingle = item.transform.GetComponent<Image>();                                                                           //获取预制物体的image组件

                Imagesingle.overrideSprite = Resources.Load (RandomStr, typeof(Sprite))as Sprite;           //动态加载image组件的sprite


                for (int i = 0; i < cells.Length; i++) {

                        if (cells .transform.childCount > 0) {//判断当前格子是否有物体

                                //如果有,并且一样的

                                if (Imagesingle.overrideSprite.name == cells .transform.GetChild (0).transform.GetComponent<Image>().overrideSprite.name) {

                                        //判断的是image加载图片的名字

                                        isFind = true;


                                        index = cells .transform.GetChild (0).transform.GetChild(0).GetComponent<Text>();

                                        IndexInt = int.Parse(index.text);

                                        IndexInt += 1;

                                        IndexStr = IndexInt.ToString();

                                        index.text = IndexStr;

                                        Destroy (item);


                                }

                        }

                }

                if (isFind == false) {

                        for (int i = 0; i < cells.Length; i++) {

                                if (cells .transform.childCount == 0) {

                                        //当前没有物体,则添加

                                        item.transform.SetParent(cells.transform);

                                        item.transform.localPosition = Vector3.zero;

                                        break;

                                }

                        }

                }

        }


}







解释一下,该函数实现的功能有动态随机加载UItem图片、判断当前格子是否有物品,有就加数目,没有就添加新物品,逻辑结构应该还算清晰。
7、把脚本挂载在Grid上,并配好,如下图:

8、添加两个Tag分别为UCell和UItem

将UCell组件的Tag设为UCell,将UItem的Tag设为UItem。用作代码中作判断。
到此差不多做好了,拖拽不用多少,看过上一篇的应该都明白,有什么问题尽管问。
[size=0.83em]背包1.gif (241.4 KB, 下载次数: 0)
下载附件  [url=]保存到相册[/url]
[color=rgb(153, 153, 153) !important]前天 14:00 上传




评分

参与人数 1活跃度 +1 展开 理由
成林 + 1 注意哦.转载帖子.记得先复制到TXT文本中.在从文本里面复制出来.这样才不会出现

查看全部评分

2016-6-4 08:07:37  
 赞 赞 1

使用道具 登录

9个回答,把该问题分享到群,邀请大神一起回答。
2#
ganxiefenxiang
回复 收起回复
2016-6-16 08:45:25   回复
 赞 赞 1

使用道具 登录

3#
谢谢楼主分享
回复 收起回复
2016-6-21 11:44:50   回复
 赞 赞 1

使用道具 登录

4#
感谢楼主分享!!
回复 收起回复
2016-6-24 19:30:35   回复
 赞 赞 1

使用道具 登录

5#
我来贴个原网址:  http://www.manew.com/thread-90065-1-1.html
(程序代码可以复制,也有原码Unity资源文件)
*原文在游戏蛮牛,「只支持在游戏蛮牛原创首发,不支持转载」,虽然这样说,但还是看到好几个地方转载{:1_140:}
回复 收起回复
2016-9-5 19:28:45   回复
 赞 赞 1

使用道具 登录

6#

看看神马东东
回复 收起回复
2016-9-16 18:24:08   回复
 赞 赞 1

使用道具 登录

7#

顶顶顶顶顶顶顶顶顶顶顶顶
回复 收起回复
2016-9-22 16:51:14   回复
 赞 赞 1

使用道具 登录

8#
这转发都不尊重别人啊
回复 收起回复
2016-10-10 08:19:22   回复
 赞 赞 1

使用道具 登录

9#
代码不好复制!! 看不明白是啥
回复 收起回复
2018-6-28 11:48:02   回复
 赞 赞 1

使用道具 登录

10#
好东西
回复 收起回复
2018-8-1 11:58:16   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表