如何添加我要升级的升级系统是我的健康状况?

时间:2017-09-20 18:40:03

标签: c# android unity3d

我是团结的新人可以帮助我吗?

例如我的健康栏值为100如果你想要升级它将花费大约10个硬币,如果我升级我的健康栏,则值变为105并且健康栏的价格也会增加。我怎样才能做到这一点?我搜索了很多,但我没有看到那种升级。 这是我的健康栏

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

public class HealthManager : MonoBehaviour {

public int maxPlayerHealth;
public static int playerHealth;
//Text text;
public Slider healthBar;

public bool isDead;

public GameObject carExplosionEfect;


// Use this for initialization
void Start () {

    //text = GetComponent<Text> ();
    healthBar = GetComponent<Slider>();

    playerHealth = maxPlayerHealth;

    isDead = false;
}

// Update is called once per frame
void Update () {
    if (playerHealth <= 0 && !isDead) 
    {
        playerHealth = 0;

        isDead = true;

        Instantiate (carExplosionEfect, transform.position, transform.rotation);
    }

    if (playerHealth > maxPlayerHealth) {
        playerHealth = maxPlayerHealth;
    }

    healthBar.value = playerHealth;
}

public static void HurtPlayerOnContact(int damageToGive)
{
    playerHealth -= damageToGive;
}

public void FullHealth()
{
    playerHealth = maxPlayerHealth;
}
}

2 个答案:

答案 0 :(得分:1)

首先想要给你一个简单的建议,不要把UI代码和播放器数据控制放在一起。创建一个升级方法,获取你想要增加的helth量,更新变量maxPlayerHealth和playerHealth,同时更新滑块最大值。还有其他疑问再次询问。

答案 1 :(得分:0)

制作不同的脚本以保持事物的排序。

对于硬币/黄金处理:MoneyManager.cs,无论何时花钱或获得金钱,它都会经历这里。

对于统计数据:StatsManager.cs,跟踪您可能拥有的当前健康状况和其他统计数据。

对于购买升级:UpgradeShop.cs,可以访问以购买更好的统计数据,具有接收硬币/黄金并返回统计奖金的方法,例如

int UpgradeHealth(int price)
{
    return price*2; //assuming 1 coin buys 2 health.
}

然后,您可以在不同脚本之间创建引用,以使它们协同工作。

这可以通过将字段设为公用,或将[SerializeField]标记添加到私有字段,然后在检查器中拖动脚本来完成。

也可以通过更精细的代码来完成。

例如,如果您只想要一个资金经理,您可以为MoneyManager创建一个静态变量,它有一个MoneyManager,就像这样。

private static MoneyManager _Instance;

void Awake(){
_Instance = this;
}

然后将该代码放在MoneyManager中,然后可以通过说出来从其他脚本调用它。

MoneyManager._Instance.YourMethod();

因此,只需让不同的脚本只处理自己分配的任务,连接它们并让它们协同工作。

编辑:

我在我的示例中使用的Awake()方法是Unity生命周期的一部分。

您可以在此处详细了解。

https://docs.unity3d.com/Manual/ExecutionOrder.html