unity战斗卡牌视频教程 第十一课 - 添加好友及删除好友和通用顶部栏添加

前言

这一课主要讲的好友添加。删除。及通用顶部栏添加。效果图如下

1

 

视频长度:50分钟

视频下载地址

列教程目录汇总: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 List<GameObject> mNowMoveListItem;
    private int mNowType;
    private UILabel mNoticeLabel;
    private UILabel mId;
    private UILabel mFriednsNumsLabel;
    private UserModel mUserModel;
    /// <summary> 当前点击的按钮 </summary>
    private GameObject mNowClickSendButton;
    #endregion

    #region UI逻辑

    void InitData()
    {
        LogicMgr.GetInstance().GetLogic<LogicCreateGeneric>().CreateGenericHead(skinTransform, Vector3.zero);
        mUserModel = ModelMgr.GetInstance().GetModel<UserModel>();
        mNowMoveListItem = new List<GameObject>();
        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>();

        //mId.text = mUserModel.i

        mFriednsNumsLabel.text = "0 / 100";

        //mNowClickButon = mBtnFriends;
        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 = "这是显示想要加你为好友的玩家!";
            DestroyMoveList();
            //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 服务器返回
    /// <summary>
    /// 获取好友列表服务器返回
    /// </summary>
    /// <param name="action"></param>
    void FriendListReturn(ActionResult action)
    {
        if (action == null) return;
        mFriendList = action.Get<List<FriendsServerData>>("list");
        DestroyMoveList();
        ShowItemList(mFriendList, 1);
        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");
        DestroyMoveList();
        ShowItemList(mFriendList, 3);
    }

    /// <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);
            mNowMoveListItem.Remove(mNowClickSendButton.transform.parent.gameObject);
            Destroy(mNowClickSendButton.transform.parent.gameObject);
            mFriendList.RemoveAt(index);
            RefreshMoveList();
        }
        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);
            mNowMoveListItem.Remove(mNowClickSendButton.transform.parent.gameObject);
            Destroy(mNowClickSendButton.transform.parent.gameObject);
            mFriendList.RemoveAt(index);
            RefreshMoveList();
        }
        else
        {
            LogicMgr.GetInstance().GetLogic<LogicTips>().AddTips("删除好友失败!");
        }
    }
    #endregion

    #region 其他逻辑

    void ShowItemList(List<FriendsServerData> list,int type)
    {
        int index = 0;
        if(list.Count <= 0)
        {
            mNoticeLabel.transform.parent.parent.gameObject.SetActive(true);
            mNoticeLabel.text = "没有数据记录";
        }
        foreach(FriendsServerData data in list)
        {
            GameObject item = Instantiate(mItem) as GameObject;
            item.name = index.ToString();
            item.transform.parent = mItem.transform.parent;
            item.transform.localEulerAngles = Vector3.zero;
            item.transform.localScale = Vector3.one;
            item.transform.localPosition = new Vector3(0, 165 - (index * 110), 0);
            FriendsItem friendsItem = item.GetComponent<FriendsItem>();
            friendsItem.InitData(data, type);
            item.SetActive(true);
            index++;
            mNowMoveListItem.Add(item);
            BoxCollider[] boxArr = item.GetComponentsInChildren<BoxCollider>();
            foreach(BoxCollider box in boxArr)
            {
                UIEventListener listener = UIEventListener.Get(box.gameObject);
                listener.onClick = ButtonClick;
            }
        }
    }


    /// <summary> 清空之前列表item </summary>
    void DestroyMoveList()
    {
        foreach(GameObject obj in mNowMoveListItem)
        {
            Destroy(obj);
        }
        mNowMoveListItem.Clear();
    }


    void RefreshMoveList()
    {
        int index = 0;
        foreach(GameObject item in mNowMoveListItem)
        {
            item.transform.localPosition = new Vector3(0, 165 - (index * 110), 0);
            index++;
        }
    }
    #endregion
}

 

本文链接:

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