游戏一般都需要玩家信息系统,那么我们应该如何搭建玩家信息系统,接下来我将展示一种简单的方法,完整代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UILabel;
public enum HeroType
{
Swordman,
Magician,
Common
}
/// <summary>
/// 玩家状态信息栏
/// </summary>
public class PlayerStatus : MonoBehaviour
{
public HeroType heroType;
public int level = 1;//等级 100+level*50
public string namePlayer = "NicolePotter";
public int hp = 100;//HP MAX
public int mp = 100;//MP MAX
public float hp_remain = 100;
public float mp_remain = 100;
public float exp = 0;//目前获得的经验
// public int coin = 200;//玩家金币数量
public float attack = 20;
public int attack_plus = 0;
public float def = 20;
public int def_plus = 0;
public float speed = 20;
public int speed_plus = 0;
//剩余点数
public int point_remain = 0;
//特效
public GameObject effect;
private void Start()
{
GetExp(0);
namePlayer = PlayerPrefs.GetString("name");
}
//让外部调用获取金币的方法
//public void GetCoint(int count)
//{
// coin += count;
//}
//获取治疗
public void GetDrug(int hp,int mp)
{
hp_remain+= hp;
mp_remain+= mp;
if(hp_remain>this.hp)
hp_remain=this.hp;
if(mp_remain>this.mp)
mp_remain=this.mp;
HeadStatusUI._instance.UpdateShow();
}
//获取点数
public bool GetPoint(int point=1)
{
if(point_remain>=point)
{
point_remain -= point;
return true;
}
return false;
}
//获取经验
public void GetExp(int exp)
{
this.exp += exp;
int total_exp = 100 + level * 50;
while(this.exp>=total_exp)
{
//TODO 升级
//播放升级动画
//播放特效
GameObject.Instantiate(effect, this.transform.position, Quaternion.identity);
this.level++;
HeadStatusUI._instance.UpdateShow();
point_remain += this.level * 5;
this.exp -= total_exp;
total_exp = 100 + level * 50;
}
ExpBar._instance.SetValue(this.exp/total_exp);
}
public bool TakeMp(int count)
{
if(mp_remain>=count)
{
mp_remain-=count;
HeadStatusUI._instance.UpdateShow();
return true;
}
else
{
return false;
}
}
}
这个类中包含玩家的一些技能和经验值加成,包括血量,速度,魔法值等等。(几乎是非常完整了),每次触发只需要在其中调用方法即可。
以上代码有不明白的地方欢迎私聊问我,我很高兴为您解答,如果对您有帮助就点个赞支持一下吧!