使用方法set("hp", -1);
函数说明及实现
具体使用及其实现
using System.Security.Cryptography.X509Certificates;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
public class Player : MonoBehaviour
{
public float hp;
public float attack;
public float def;
// Start is called before the first frame update
void Start()
{
initData();
}
// Update is called once per frame
void Update()
{
set("hp", -1);
Debug.Log(hp);
}
void initData(float hpc = 100, float attackc = 10, float defc = 0)
{
hp = hpc;
attack = attackc;
def = defc;
}
/// <summary>
/// 设置玩家属性
/// </summary>
/// <param name="type">属性类型</param>
/// <param name="num">数值</param>
/// <param name="flag">是否直接设置,为 false 时是在玩家原来属性基础上增减</param>
public void set(string type, float num = 0, bool flag = false)
{
FieldInfo target = GetType().GetField(type);
if (target != null){
GetType().GetField(type).SetValue(this, flag?num:(float)GetType().GetField(type).GetValue(this) + num);
}
}
}
实现效果:每一帧输出100,99,98 以此类推
|