C# 如何在我想升级的地方添加升级系统是我的健康问题?

C# 如何在我想升级的地方添加升级系统是我的健康问题?,c#,android,unity3d,C#,Android,Unity3d,我是新来的有人能帮我吗 例如,如果你想升级,我的健康栏的价值是100,它将花费大约10个硬币,如果我升级我的健康栏,价值将变为105,健康栏的价格也会上涨。我该怎么做?我搜索了很多,但没有看到这种升级。 这是我的健康酒吧 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HealthManager : MonoBeha

我是新来的有人能帮我吗

例如,如果你想升级,我的健康栏的价值是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;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类HealthManager:MonoBehavior{
公共int maxPlayerHealth;
公众健康;
//文本;
公共健康酒吧;
公共场所被破坏;
公共游戏对象CareExplosionEffect;
//用于初始化
无效开始(){
//text=GetComponent();
healthBar=GetComponent();
playerHealth=maxPlayerHealth;
isDead=false;
}
//每帧调用一次更新
无效更新(){
if(playerHealth maxPlayerHealth){
playerHealth=maxPlayerHealth;
}
healthBar.value=玩家健康;
}
公共静态无效触点(内部损坏)
{
playerHealth-=损伤性关节炎;
}
公共卫生()
{
playerHealth=maxPlayerHealth;
}
}

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

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

对于硬币/黄金处理:MoneyManager.cs,无论何时花钱或赚钱,都要经过这里

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

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

int UpgradeHealth(int price)
{
    return price*2; //assuming 1 coin buys 2 health.
}
然后,您可以在不同的脚本之间创建引用,使它们协同工作

这可以通过将字段设置为公共字段,或者将[SerializeField]标记添加到私有字段,然后在inspector中将脚本拖入来实现

它也可以通过更复杂的代码来完成

例如,如果您只需要一个MoneyManager,您可以为MoneyManager创建一个静态变量,其中包含一个MoneyManager,如下所示

private static MoneyManager _Instance;

void Awake(){
_Instance = this;
}
然后将该代码放在MoneyManager中,然后可以通过以下方式从其他脚本调用它:

MoneyManager._Instance.YourMethod();
因此,只需让不同的脚本只处理它们自己分配的任务,将它们连接起来,让它们一起工作即可

编辑:

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

你可以在这里了解更多


我有shop、money、shield(加电)脚本,但出于某种原因,我的钱只显示在我的1级场景中,而不是在shop场景中,我无法购买我放在shop中的盾牌。你需要显示一些代码,以便我和其他人有机会帮助你。最好换个新职位。我把它贴在这里,希望你能帮助我,先生