unity战斗卡牌视频教程 第十二课 - 滑动列表的优化讲解及使用

前言

这一节课讲了滑动列表的优化。子物体的循环利用。动态复位。飞入效果等等。如下

11

视频长度:32分钟

视频下载地址

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

代码

 

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

public class SceneFriends : SceneBase
{
    #region 界面加载相关
    protected override void OnInitFront()
    {
        base.OnInitFront();
        _type = SceneType.SceneFriends;
    }
    protected override void OnInitSkinFront()
    {
        base.OnInitSkinFront();
        SetMainSkinPath("Game/Friends/SceneFriends");
    }
    protected override void OnInitDone()
    {
        base.OnInitDone();
        InitData();

    }
    protected override void OnDestroyDone()
    {
        base.OnDestroyDone();

    }
    protected override void OnDestroyFront()
    {
        base.OnDestroyFront();
        
    }
    protected override void OnClick(GameObject target)
    {
        base.OnClick(target);
        ButtonClick(target);
    }

    public override void OnInit(params object[] sceneArgs)
    {
        base.OnInit(sceneArgs);
    }
    public override void OnShowed()
    {
        base.OnShowed();

    }
    #endregion

    #region 数据定义
    private GameObject mItem;
    private GameObject mBtnFriends;
    private GameObject mBtnInfo;
    private GameObject mBtnFind;
    private GameObject mNowClickButon;
    private UIInput mInputFriendID;
    private UILabel mContent;
    /// <summary> 好友列表 </summary>
    private List<FriendsServerData> mFriendList;
    private int mNowType;
    private UILabel mNoticeLabel;
    private UILabel mId;
    private UILabel mFriednsNumsLabel;
    private UserModel mUserModel;
    /// <summary> 当前点击的按钮 </summary>
    private GameObject mNowClickSendButton;
    /// <summary> 滑动列表控制器 </summary>
    private SUIWrapContent mSUIWrapContent;
    #endregion

    #region UI逻辑

    void InitData()
    {
        LogicMgr.GetInstance().GetLogic<LogicCreateGeneric>().CreateGenericHead(skinTransform, Vector3.zero);
        mUserModel = ModelMgr.GetInstance().GetModel<UserModel>();
        mFriednsNumsLabel = skinTransform.Find("Back/Top/FriendNums").GetComponent<UILabel>();
        mId = skinTransform.Find("Back/Top/ID").GetComponent<UILabel>();
        mItem = skinTransform.Find("PanelMove/Items/Item").gameObject;
        mBtnFriends = skinTransform.Find("Back/BtnFriends").gameObject;
        mBtnInfo = skinTransform.Find("Back/BtnInfo").gameObject;
        mBtnFind = skinTransform.Find("Back/BtnFind").gameObject;
        mInputFriendID = skinTransform.Find("Back/InputFriendID").GetComponent<UIInput>();
        mContent = skinTransform.Find("Back/Content").GetComponent<UILabel>();
        mNoticeLabel = skinTransform.Find("Back/Notice/Sprite/Label").GetComponent<UILabel>();
        mFriednsNumsLabel.text = "0 / 100";

        ButtonClick(mBtnFriends);
    }

    #endregion

    #region 点击事件

    void ButtonClick(GameObject click)
    {
        if(click.name.Equals("BtnFriends"))
        {
            SwitchButtonType(click,1);
        }
        else if (click.name.Equals("BtnInfo"))
        {
            SwitchButtonType(click,2);
        }
        else if (click.name.Equals("BtnFind"))
        {
            SwitchButtonType(click,3);
        }
        else if (click.name.Equals("BtnRemove"))
        {
            int index = int.Parse(click.transform.parent.name);
            FriendsServerData data = mFriendList[index];
            if(data != null)
            {
                ActionParam ap = new ActionParam();
                ap["friendID"] = data.friendID;
                Net.Instance.Send((int)ActionType.RemoveFriend, RemoveFriendRetutn, ap);
                mNowClickSendButton = click;
            }
        }
        else if (click.name.Equals("BtnAddFirend"))
        {
            int index = int.Parse(click.transform.parent.name);
            FriendsServerData data = mFriendList[index];
            if (data != null)
            {
                ActionParam ap = new ActionParam();
                ap["friendID"] = data.friendID;
                ap["friendName"] = data.friendID;
                Net.Instance.Send((int)ActionType.AddFriend, AddFriendReturn, ap);
                mNowClickSendButton = click;
            }
        }
    }

    void SwitchButtonType(GameObject click,int type)
    {
        mNoticeLabel.transform.parent.parent.gameObject.SetActive(false);
        GameObject sprite = click.transform.Find("Sprite").gameObject;
        BoxCollider box = click.GetComponent<BoxCollider>();
        sprite.SetActive(true);
        box.enabled = false;

        if (mNowClickButon != null)
        {
            sprite = mNowClickButon.transform.Find("Sprite").gameObject;
            box = mNowClickButon.GetComponent<BoxCollider>();
            sprite.SetActive(false);
            box.enabled = true;
        }
        mNowClickButon = click;
        mNowType = type;
        if(type == 3)
        {
            mContent.gameObject.SetActive(false);
            mInputFriendID.gameObject.SetActive(true);
            Net.Instance.Send((int)ActionType.IntroduceFriend, AddFriendListReturn, null);
        }
        else if(type == 2)
        {
            mContent.gameObject.SetActive(true);
            mInputFriendID.gameObject.SetActive(false);
            mContent.text = "这是显示想要加你为好友的玩家!";
            //Net.Instance.Send((int)ActionType.FriendList, FriendListReturn, null);
        }
        else
        {
            mContent.gameObject.SetActive(true);
            mInputFriendID.gameObject.SetActive(false);
            mContent.text = "结交好友,互赠体力,还可获得成就奖励呢!";
            Net.Instance.Send((int)ActionType.FriendList, FriendListReturn, null);
            
        }

    }
    #endregion

    #region 其他逻辑

    void ShowItemList()
    {
        mSUIWrapContent = mItem.transform.parent.GetComponent<SUIWrapContent>();
        mSUIWrapContent.maxNum = mFriendList.Count;
        mSUIWrapContent.first = 0;
        mSUIWrapContent.updateItemCallback = InitItem;
        mSUIWrapContent.clickCallback = ButtonClick;
        mSUIWrapContent.wrapContent = true;
        mSUIWrapContent.Reset(true);
    }

    void InitItem(GameObject item,int index)
    {
        FriendsServerData data = mFriendList[index];
        FriendsItem friendsItem = item.GetComponent<FriendsItem>();
        friendsItem.InitData(data, mNowType);
    }

    #endregion

    #region 服务器返回
    /// <summary>
    /// 获取好友列表服务器返回
    /// </summary>
    /// <param name="action"></param>
    void FriendListReturn(ActionResult action)
    {
        if (action == null) return;
        mFriendList = action.Get<List<FriendsServerData>>("list");
        ShowItemList();
        mFriednsNumsLabel.text = mFriendList.Count.ToString() + " / 100";
    }
    /// <summary>
    /// 可添加为好友的推荐列表
    /// </summary>
    /// <param name="action"></param>
    void AddFriendListReturn(ActionResult action)
    {
        if (action == null) return;
        mFriendList = action.Get<List<FriendsServerData>>("list");
        ShowItemList();
    }

    /// <summary>
    /// 添加好友返回
    /// </summary>
    /// <param name="action"></param>
    void AddFriendReturn(ActionResult action)
    {
        if (action == null) return;
        int isSuccess = action.Get<int>("isSuccess");
        if (isSuccess == 1)
        {
            LogicMgr.GetInstance().GetLogic<LogicTips>().AddTips("添加好友成功!");
            int index = int.Parse(mNowClickSendButton.transform.parent.name);
            mFriendList.RemoveAt(index);
            mSUIWrapContent.RemoveItem(mNowClickSendButton.transform.parent.gameObject);
            mSUIWrapContent.RefreshList();
        }
        else
        {
            LogicMgr.GetInstance().GetLogic<LogicTips>().AddTips("添加好友失败!");
        }
    }
    /// <summary>
    /// 删除好友返回
    /// </summary>
    /// <param name="action"></param>
    void RemoveFriendRetutn(ActionResult action)
    {
        if (action == null) return;
        int isSuccess = action.Get<int>("isSuccess");
        if (isSuccess == 1)
        {
            LogicMgr.GetInstance().GetLogic<LogicTips>().AddTips("删除好友成功!");
            int index = int.Parse(mNowClickSendButton.transform.parent.name);

            mFriendList.RemoveAt(index);

        }
        else
        {
            LogicMgr.GetInstance().GetLogic<LogicTips>().AddTips("删除好友失败!");
        }
    }
    #endregion
}

 

 

 

本文链接:

https://www.bobsong.net/1053.html
1 + 1 =
3 评论
    2015年07月23日 回复

    这节课wrap.updateItemCallback委托调用void InitItem(GameObject item, int index)函数的时候参数并没有赋值,GameObject是哪个元素呢,index又在哪取的值呢?而且通过FriendsServerData data = mFriendList[index];也没有Instantiate就把游戏对象实例化出来了吗。看不懂,求教

      2015年07月24日 回复

      @公子邢 这个。在wrap这个tools脚本里面有的。创建的时候。肯定能知道自己是哪个游戏对象。而且他是第几个创建出来的啊

    2015年09月16日 回复

    这节课的工程代码,百度网盘,打开没找到相对应的脚本呀.请更新一下项目工程吧.