unity战斗卡牌视频教程 第八课 – 主界面Scene 及 玩家信息Panel

前言

本节课。我们主要讲了以下几点内容:

1.Panel的建立。及PanelBase的使用。

2.属性变更事件

3.数据模型的定义 UserModel

4.点击事件委托

5.等等。。

时长:72分钟

效果预览。

2

1

下集预告

好友。聊天。邮件,商店。背包制作。

教程下载地址

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

本课脚本

玩家数据模型 UserModel.cs

using UnityEngine;
using System.Collections;

public class UserModel : BaseModel
{
    /// <summary> 玩家昵称 </summary>
    public string name
    {
        set;
        get;
    }
    /// <summary> 玩家性别 </summary>
    public int sex
    {
        get;
        set;
    }

    #region 金币

    private int mGold;
    /// <summary> 玩家金币 </summary>
    public int gold
    {
        get
        {
            return mGold;
        }
        set
        { 
            if(mGold == value)
            {
                return;
            }
            if(value <= 0)
            {
                mGold = 0;
            }

            ValueUpdateEventArgs ve = new ValueUpdateEventArgs();
            ve.key = "gold";
            ve.oldValue = mGold;
            ve.newValue = value;
            mGold = value;
            DispatchValueUpdateEvent(ve);
        }
    }
    #endregion

    #region 元宝
    private int mIngot;
    /// <summary> 元宝 </summary>
    public int ingot
    {
        get
        {
            return mIngot;
        }
        set
        {
            if (mIngot == value)
            {
                return;
            }
            if (value <= 0)
            {
                mIngot = 0;
            }

            ValueUpdateEventArgs ve = new ValueUpdateEventArgs();
            ve.key = "ingot";
            ve.oldValue = mIngot;
            ve.newValue = value;
            mIngot = value;
            DispatchValueUpdateEvent(ve);
        }
    }
    #endregion

    #region 经验
    private int mExp;
    /// <summary> 经验 </summary>
    public int exp
    {
        get
        {
            return mExp;
        }
        set
        {
            if (mExp == value)
            {
                return;
            }
            if (value <= 0)
            {
                mExp = 0;
            }

            ValueUpdateEventArgs ve = new ValueUpdateEventArgs();
            ve.key = "exp";
            ve.oldValue = mExp;
            ve.newValue = value;
            mExp = value;
            DispatchValueUpdateEvent(ve);
        }
    }
    #endregion

    #region 等级
    private int mLv;
    /// <summary> 等级 </summary>
    public int lv
    {
        get
        {
            return mLv;
        }
        set
        {
            if (mLv == value)
            {
                return;
            }
            if (value <= 0)
            {
                mLv = 0;
            }

            ValueUpdateEventArgs ve = new ValueUpdateEventArgs();
            ve.key = "lv";
            ve.oldValue = mLv;
            ve.newValue = value;
            mLv = value;
            DispatchValueUpdateEvent(ve);
        }
    }
    #endregion

    #region 体力
    private int mAction;
    /// <summary> 等级 </summary>
    public int action
    {
        get
        {
            return mAction;
        }
        set
        {
            if (mAction == value)
            {
                return;
            }
            if (value <= 0)
            {
                mLv = 0;
            }

            ValueUpdateEventArgs ve = new ValueUpdateEventArgs();
            ve.key = "action";
            ve.oldValue = mAction;
            ve.newValue = value;
            mAction = value;
            DispatchValueUpdateEvent(ve);
        }
    }
    #endregion
}

主界面 SceneMain.cs

using UnityEngine;
using System.Collections;

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

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

    }
    protected override void OnDestroyFront()
    {
        base.OnDestroyFront();
        mUserModel.ValueUpdateEvent -= OnValueUpdateEventArgs;
        mUserModel = null;
    }
    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 UISprite mHeadIcon;
    private UILabel mNameLabel;
    private UILabel mVipLabel;
    private UILabel mActionLabel;
    private UILabel mGoldLabel;
    private UILabel mIngotLabel;
    private UILabel mZhanDouLiLabel;
    private UILabel mLvLabel;
    private UserModel mUserModel;
    #endregion

    #region 界面初始化
    void InitData()
    {
        mUserModel = ModelMgr.GetInstance().GetModel<UserModel>();
        mUserModel.ValueUpdateEvent += OnValueUpdateEventArgs;
        mHeadIcon = skinTransform.Find("HeadInfo/Head/HeadIcon").GetComponent<UISprite>();
        mNameLabel = skinTransform.Find("HeadInfo/NameLabel").GetComponent<UILabel>();
        mVipLabel = skinTransform.Find("HeadInfo/VipLabel").GetComponent<UILabel>();
        mActionLabel = skinTransform.Find("HeadInfo/ActionSprite/Label").GetComponent<UILabel>();
        mGoldLabel = skinTransform.Find("HeadInfo/GoldSprite/Label").GetComponent<UILabel>();
        mIngotLabel = skinTransform.Find("HeadInfo/IngotSprite/Label").GetComponent<UILabel>();
        mZhanDouLiLabel = skinTransform.Find("HeadInfo/ZhanDouLi").GetComponent<UILabel>();
        mLvLabel = skinTransform.Find("HeadInfo/LvSprite/Label").GetComponent<UILabel>();

        UIEventListener listener = UIEventListener.Get(mHeadIcon.transform.parent.gameObject);
        listener.onClick = ButtonClick;

        mNameLabel.text = mUserModel.name;
        mLvLabel.text = mUserModel.lv.ToString();
        mActionLabel.text = string.Concat(mUserModel.action.ToString(), "/ 100");
        mGoldLabel.text = mUserModel.gold.ToString();
        mIngotLabel.text = mUserModel.ingot.ToString();
    }

    #endregion

    #region 属性变更事件

    void OnValueUpdateEventArgs(object obj, ValueUpdateEventArgs v)
    {
        switch(v.key)
        {
            case "gold":
                ChangeGold((int)v.newValue, (int)v.oldValue);
                break;
        }
    }

    void ChangeGold(int newNums,int oldNums)
    {
        mGoldLabel.text = newNums.ToString();
    }
    #endregion

    #region 按钮点击事件

    void ButtonClick(GameObject click)
    {
        if(click.name.Equals("Head"))
        {
            PanelMgr.GetInstance().ShowPanel(PanelName.PanelUserInfo);
        }
    }
    #endregion
}

玩家信息 PanelUserInfo.cs

using UnityEngine;
using System.Collections;

public class PanelUserInfo : PanelBase
{
    #region 界面加载相关
    protected override void OnInitFront()
    {
        base.OnInitFront();
        _type = PanelName.PanelUserInfo;//面板名称
        _maskStyle = PanelMgr.PanelMaskSytle.BlackAlpha;//面板打开遮罩类型
        _showStyle = PanelMgr.PanelShowStyle.CenterScaleBigNomal;//面板打开方式
    }
    protected override void OnInitSkinFront()
    {
        base.OnInitSkinFront();
        SetMainSkinPath("Game/Main/PanelUserInfo");
    }
    protected override void OnInitDone()
    {
        base.OnInitDone();
    }
    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 ui逻辑


    void ButtonClick(GameObject click)
    {
        Close();
    }
    #endregion
}

 

 

 

 

本文链接:

https://bobsong.cn/922.html
1 + 6 =
1 评论
    3dChrome 69Windows 10
    3月19日 回复

    大神,你在优酷的教程挂掉了,请问有其他补档吗?