更新变量GUI

时间:2017-12-13 09:53:40

标签: c# unity5 unity3d-gui

我正在拍摄太空射击游戏,在左下角,我在GUI中显示了我的统计数据。 onGUI我认为只在游戏开始时调用,因为当我在脚本中更改变量时,它应该更改GUI,因为它从该脚本中获取数字!

private void OnGUI()
{
    Vector2 targetPos = Vector2.zero;
    ////draw Armor
    //VitalStat armor = GetStat(StatType.Armor) as VitalStat;
    //VitalStat health = GetStat(StatType.Health) as VitalStat;
    //VitalStat shield = GetStat(StatType.Shield) as VitalStat;
    //VitalStat energy = GetStat(StatType.Energy) as VitalStat;

    if (GetComponent<Ship>() != null)
    {
        targetPos = new Vector2(95, Screen.height - 65);

        int row = 0;
        if (Armor.CurrentValue > 0)
        {
            GUIUtil.DrawBar(targetPos, 150, 15, row, Armor.CurrentValue, Armor.Value, "Armor");
        }
        else
        //draw Hp
        {
            GUIUtil.DrawBar(targetPos, 150, 15, row, Health.CurrentValue, Health.Value, "Health");
        }
        //draw Shield
        if (Shield.Value > 0)
        {
            row++;
            GUIUtil.DrawBar(targetPos, 150, 15, row, Shield.CurrentValue, Shield.Value, "Shield");
        }

        //draw energy
        if (Energy.Value > 0)
        {
            row++;
            GUIUtil.DrawBar(targetPos, 150, 15, row, Energy.CurrentValue, Energy.Value, "Energy");
        }
    }
    else if (GetComponent<Enemy>() != null)// && _hitTimer > Time.time
    {
        targetPos = Camera.main.WorldToScreenPoint(transform.position);

        int row = 0;
        //draw Shield
        if (Shield.CurrentValue > 0)
        {
            GUIUtil.DrawBar(targetPos, row, Shield.CurrentValue, Shield.Value, "Shield");
        }
        else if (Armor.CurrentValue > 0)
        {
            GUIUtil.DrawBar(targetPos, row, Armor.CurrentValue, Armor.Value, "Armor");
        }
        else
        //draw Hp
        {
            GUIUtil.DrawBar(targetPos, row, Health.CurrentValue, Health.Value, "Health");
        }

        //draw energy
        if (Energy.Value > 0)
        {
            row++;
            GUIUtil.DrawBar(targetPos, row, Energy.CurrentValue, Energy.Value, "Energy");
        }
    }
}

这是我绘制GUI的地方。

正如你所看到它得到了Armor.Currentvalue,当我改变那个数字时,GUI没有改变,有人知道为什么或如何解决它?

更新

这是跟踪健康状况的统计数据。

public VitalStat Health = new VitalStat() { Type = VitalStatType.Health };

VitalStat有这个:

[HideInInspector]
    public float CurrentValue;
    [HideInInspector]
    public bool ShouldRegenerate = true;
    public VitalStatType Type;
    public StatType RegenerationRate;
    public StatType RegenerationStartDelay;


    private Stat _rate;
    private Stat _delay;
    private float _regenDelayTimer = 0;

这是计算当前值的方式:

if (ShouldRegenerate && Time.time > _regenDelayTimer && _rate != null)
            {
                CurrentValue = Mathf.Min(Value, CurrentValue + (_rate.Value * Time.deltaTime));
            }

这是值:

protected float _value;
    public virtual float Value { get { return _value; } }

该值由basevalue设置:

    public virtual void Init(StatManager statMngr)
        {
            statManager = statMngr;

            _value = BaseValue;
            CalculateStatValue();
        }

在这里设置basevalue,我改变了我的健康输入:

if (!string.IsNullOrEmpty(val))
                    {
                        try
                        {
                            float intVal = Convert.ToSingle(val);
                            target.BaseValue = intVal;

                            //Safe to Database!
                        }
                        catch (Exception e)
                        {
                            Debug.Log("Invalid value " + e.Message);
                        }
                    }

我希望这能澄清一下

0 个答案:

没有答案