unity战斗卡牌视频教程 第二课 - 滑动列表及克隆删除对象以及代码添加脚本【补录】

前言

本节课为补录的第二课。主要讲了以下内容

1.动态创建子物体

2.按钮事件及克隆删除

3.滑动列表及UIPanel的使用

1

 

交流群号:162541429

 

教程下载地址

系列教程目录汇总:http://www.bobsong.net/886.html

 代码如下

using UnityEngine;
using System.Collections;
using System;

public class SceneMail : MonoBehaviour
{

    private GameObject mItem;
    void Start()
    {
        mItem = transform.Find("PanelMove/Items/Item").gameObject;
        ShowMove();
    }

    /// <summary>
    /// 显示滑动列表
    /// </summary>
    void ShowMove()
    {
        for (int i = 0; i < 50; i++)
        {
            GameObject item = Instantiate(mItem) as GameObject;
            item.transform.parent = mItem.transform.parent;
            item.SetActive(true);
            item.transform.localEulerAngles = Vector3.zero;
            item.transform.localScale = Vector3.one;
            item.transform.localPosition = new Vector3(0, 170 - (i * 120), 0);
            item.name = i.ToString();
            UIDragScrollView dragScroll = item.AddComponent<UIDragScrollView>();
            BoxCollider[] boxArr = item.GetComponentsInChildren<BoxCollider>();
            foreach(BoxCollider box in boxArr)
            {
                if (box.gameObject.name.StartsWith("Btn"))
                {
                    UIEventListener listener = UIEventListener.Get(box.gameObject);
                    listener.onClick = ButtonClick;
                }
            }
            InitItem(item, i);
        }
    }
    /// <summary>
    /// item初始化
    /// </summary>
    /// <param name="item"></param>
    /// <param name="index"></param>
    void InitItem(GameObject item,int index)
    {
        UILabel title = item.transform.Find("Title").GetComponent<UILabel>();
        UILabel time = item.transform.Find("Time").GetComponent<UILabel>();
        UISprite icon = item.transform.Find("Head/Icon").GetComponent<UISprite>();
        title.text = "王麻子给你的来信" + index.ToString();
        time.text = DateTime.Now.ToString();
        icon.spriteName = (3050 + index).ToString();
    }
    /// <summary>
    /// 按钮点击事件
    /// </summary>
    /// <param name="click"></param>
    void ButtonClick(GameObject click)
    {
        if (click.name.Equals("BtnRemove"))
        {
            Debug.Log("正则删除Item = " + click.transform.parent.gameObject.name);
            Destroy(click.transform.parent.gameObject);
        }
        
    }
}

 

 

 

 

 

 

 

本文链接:

https://bobsong.cn/1002.html
1 + 2 =
快来做第一个评论的人吧~