5.常规实现角色面板

5.MVC框架-不使用MVC框架-角色面板


5.1 知识点

创建角色面板脚本,继承 Monobehaviour,挂载到面板预制体上

public class RolePanel : MonoBehaviour
{
}

定义控件变量外部关联

//找控件
public Text txtLev;
public Text txtHp;
public Text txtAtk;
public Text txtDef;
public Text txtCrit;
public Text txtMiss;
public Text txtLuck;

public Button btnClose;
public Button btnLevUp;

对控件添加事件监听

void Start()
{
    //监听事件
    btnClose.onClick.AddListener(ClickClose);
    btnLevUp.onClick.AddListener(ClickLevUp);
}

public void ClickClose()
{
    Debug.Log("关闭");

    HideMe();
}

public void ClickLevUp()
{
    Debug.Log("升级");

}

更新面板信息

//更新面板
public void UpdateInfo()
{
    txtLev.text = "LV." + PlayerPrefs.GetInt("PlayerLev", 1);
    txtHp.text = PlayerPrefs.GetInt("PlayerHp", 100).ToString();
    txtAtk.text = PlayerPrefs.GetInt("PlayerAtk", 20).ToString();
    txtDef.text = PlayerPrefs.GetInt("PlayerDef", 10).ToString();
    txtCrit.text = PlayerPrefs.GetInt("PlayerCrit", 20).ToString();
    txtMiss.text = PlayerPrefs.GetInt("PlayerMiss", 10).ToString();
    txtLuck.text = PlayerPrefs.GetInt("PlayerLuck", 40).ToString();
}

面板设置成单例,显示隐藏面板

//显隐
private static RolePanel panel;

public static void ShowMe()
{
    if (panel == null)
    {
        //实例化面板对象
        GameObject res = Resources.Load<GameObject>("BaseFramework/UI/UGUI/Normal/RolePanel");
        GameObject obj = Instantiate(res);
        //设置它的父对象 为 Canvas
        obj.transform.SetParent(GameObject.Find("Canvas").transform, false);

        panel = obj.GetComponent<RolePanel>();
    }
    //如果是隐藏的形式hide 在这要显示
    panel.gameObject.SetActive(true);
    //显示完面板 更新该面板的信息
    panel.UpdateInfo();
}

public static void HideMe()
{
    if (panel != null)
    {
        //方式一 直接删
        //Destroy(panel.gameObject);
        //panel = null;
        //方式二 设置可见为隐藏
        panel.gameObject.SetActive(false);
    }
}

点击角色升级按钮具体升级逻辑

public void ClickLevUp()
{
    Debug.Log("升级");

    //角色升级具体逻辑
    //升级其实就是 数据的更新
    //的数据是存在本地 
    //获取本地数据
    int lev = PlayerPrefs.GetInt("PlayerLev", 1);
    int hp = PlayerPrefs.GetInt("PlayerHp", 100);
    int atk = PlayerPrefs.GetInt("PlayerAtk", 20);
    int def = PlayerPrefs.GetInt("PlayerDef", 10);
    int crit = PlayerPrefs.GetInt("PlayerCrit", 20);
    int miss = PlayerPrefs.GetInt("PlayerMiss", 10);
    int luck = PlayerPrefs.GetInt("PlayerLuck", 40);
    //然后通过一定的升级规则去改变它 
    lev += 1;
    hp += lev;
    atk += lev;
    def += lev;
    crit += lev;
    miss += lev;
    luck += lev;
    //存起来
    PlayerPrefs.SetInt("PlayerLev", lev);
    PlayerPrefs.SetInt("PlayerHp", hp);
    PlayerPrefs.SetInt("PlayerAtk", atk);
    PlayerPrefs.SetInt("PlayerDef", def);
    PlayerPrefs.SetInt("PlayerCrit", crit);
    PlayerPrefs.SetInt("PlayerMiss", miss);
    PlayerPrefs.SetInt("PlayerLuck", luck);

    //同步更新面板上的数据
    UpdateInfo();

    //更新主面板的内容
    MainPanel.Panel.UpdateInfo();
}

主面板点击角色按钮添加打开角色面板监听

void Start()
{
    //添加事件
    //btnRole.onClick.AddListener(ClickBtnRole);

    btnRole.onClick.AddListener(() =>
    {
        //打开角色面板的逻辑
        Debug.Log("按钮点击");
        //显示角色面板
        RolePanel.ShowMe();
    });
}

private void ClickBtnRole()
{
    //打开角色面板的逻辑
    //显示角色面板
    RolePanel.ShowMe();
}

5.2 知识点代码

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

//1.创建脚本
public class RolePanel : MonoBehaviour
{
    //2.找控件
    public Text txtLev;
    public Text txtHp;
    public Text txtAtk;
    public Text txtDef;
    public Text txtCrit;
    public Text txtMiss;
    public Text txtLuck;

    public Button btnClose;
    public Button btnLevUp;


    //4.显隐
    private static RolePanel panel;

    public static void ShowMe()
    {
        if (panel == null)
        {
            //实例化面板对象
            GameObject res = Resources.Load<GameObject>("BaseFramework/UI/UGUI/Normal/RolePanel");
            GameObject obj = Instantiate(res);
            //设置它的父对象 为Canvas
            obj.transform.SetParent(GameObject.Find("Canvas").transform, false);

            panel = obj.GetComponent<RolePanel>();
        }
        //如果是隐藏的形式hide 在这要显示
        panel.gameObject.SetActive(true);
        //显示完面板 更新该面板的信息
        panel.UpdateInfo();
    }

    public static void HideMe()
    {
        if (panel != null)
        {
            //方式一 直接删
            //Destroy(panel.gameObject);
            //panel = null;
            //方式二 设置可见为隐藏
            panel.gameObject.SetActive(false);
        }
    }




    void Start()
    {
        //3.监听事件
        btnClose.onClick.AddListener(ClickClose);
        btnLevUp.onClick.AddListener(ClickLevUp);
    }

    public void ClickClose()
    {
        Debug.Log("关闭");

        HideMe();
    }

    public void ClickLevUp()
    {
        Debug.Log("升级");

        //6.角色升级具体逻辑
        //升级其实就是 数据的更新
        //的数据是存在本地 
        //获取本地数据
        int lev = PlayerPrefs.GetInt("PlayerLev", 1);
        int hp = PlayerPrefs.GetInt("PlayerHp", 100);
        int atk = PlayerPrefs.GetInt("PlayerAtk", 20);
        int def = PlayerPrefs.GetInt("PlayerDef", 10);
        int crit = PlayerPrefs.GetInt("PlayerCrit", 20);
        int miss = PlayerPrefs.GetInt("PlayerMiss", 10);
        int luck = PlayerPrefs.GetInt("PlayerLuck", 40);
        //然后通过一定的升级规则去改变它 
        lev += 1;
        hp += lev;
        atk += lev;
        def += lev;
        crit += lev;
        miss += lev;
        luck += lev;
        //存起来
        PlayerPrefs.SetInt("PlayerLev", lev);
        PlayerPrefs.SetInt("PlayerHp", hp);
        PlayerPrefs.SetInt("PlayerAtk", atk);
        PlayerPrefs.SetInt("PlayerDef", def);
        PlayerPrefs.SetInt("PlayerCrit", crit);
        PlayerPrefs.SetInt("PlayerMiss", miss);
        PlayerPrefs.SetInt("PlayerLuck", luck);

        //同步更新面板上的数据
        UpdateInfo();

        //更新主面板的内容
        MainPanel.Panel.UpdateInfo();
    }

    //4.更新面板
    public void UpdateInfo()
    {
        txtLev.text = "LV." + PlayerPrefs.GetInt("PlayerLev", 1);
        txtHp.text = PlayerPrefs.GetInt("PlayerHp", 100).ToString();
        txtAtk.text = PlayerPrefs.GetInt("PlayerAtk", 20).ToString();
        txtDef.text = PlayerPrefs.GetInt("PlayerDef", 10).ToString();
        txtCrit.text = PlayerPrefs.GetInt("PlayerCrit", 20).ToString();
        txtMiss.text = PlayerPrefs.GetInt("PlayerMiss", 10).ToString();
        txtLuck.text = PlayerPrefs.GetInt("PlayerLuck", 40).ToString();
    }
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 785293209@qq.com

×

喜欢就点赞,疼爱就打赏