Unity中C#脚本的问题

时间:2015-12-15 19:32:58

标签: c# unity3d unity5

如何缩短这个?我想我可能是一个数组,但我不知道如何设置它。这是代码,当你获得一定数量的硬币时,它就是它需要做的事情:它将生命值提高10并保存健康状况,然后它会更改显示下次升级所需硬币数量的文本。

void HpUpgradelvl()

 {

     if (PlayerControl.coins >= 30)
     {
         healthPoints = healthPoints + 10;
         PlayerPrefs.SetFloat("HealthUP", healthPoints);
         if (PlayerControl.coins >= 60)
         {
             txt.SetActive(false);
             txt1.SetActive(true);
             txt2_0.SetActive(false);
             txt2_1.SetActive(true);
             healthPoints = healthPoints + 10;
             PlayerPrefs.SetFloat("HealthUP", healthPoints);
             if (PlayerControl.coins >= 100)
             {
                 txt1.SetActive(false);
                 txt2.SetActive(true);
                 txt2_1.SetActive(false);
                 txt2_2.SetActive(true);
                 healthPoints = healthPoints + 10;
                 PlayerPrefs.SetFloat("HealthUP", healthPoints);
                 if (PlayerControl.coins >= 150)
                 {
                     txt2.SetActive(false);
                     txt3.SetActive(true);
                     txt2_2.SetActive(false);
                     txt2_3.SetActive(true);
                     healthPoints = healthPoints + 10;
                     PlayerPrefs.SetFloat("HealthUP", healthPoints);
                     if (PlayerControl.coins >= 210)
                     {
                         txt3.SetActive(false);
                         txt4.SetActive(true);
                         txt2_3.SetActive(false);
                         txt2_4.SetActive(true);
                         healthPoints = healthPoints + 10;
                         PlayerPrefs.SetFloat("HealthUP", healthPoints);
                         if (PlayerControl.coins >= 280)
                         {
                             txt4.SetActive(false);
                             txt5.SetActive(true);
                             txt2_4.SetActive(false);
                             txt2_5.SetActive(true);
                             healthPoints = healthPoints + 10;
                             PlayerPrefs.SetFloat("HealthUP", healthPoints);
                             if (PlayerControl.coins >= 360)
                             {
                                 txt5.SetActive(false);
                                 txt6.SetActive(true);
                                 txt2_5.SetActive(false);
                                 txt2_6.SetActive(true);
                                 healthPoints = healthPoints + 10;
                                 PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                 if (PlayerControl.coins >= 450)
                                 {
                                     txt6.SetActive(false);
                                     txt7.SetActive(true);
                                     txt2_6.SetActive(false);
                                     txt2_7.SetActive(true);
                                     healthPoints = healthPoints + 10;
                                     PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                     if (PlayerControl.coins >= 500)
                                     {
                                         txt7.SetActive(false);
                                         txt8.SetActive(true);
                                         txt2_7.SetActive(false);
                                         txt2_8.SetActive(true);
                                         healthPoints = healthPoints + 10;
                                         PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                         if (PlayerControl.coins >= 610)
                                         {
                                             txt8.SetActive(false);
                                             txt9.SetActive(true);
                                             txt2_8.SetActive(false);
                                             txt2_9.SetActive(true);
                                             healthPoints = healthPoints + 10;
                                             PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                             if (PlayerControl.coins >= 730)
                                             {
                                                 txt9.SetActive(false);
                                                 txt10.SetActive(true);
                                                 txt2_9.SetActive(false);
                                                 txt2_10.SetActive(true);
                                                 healthPoints = healthPoints + 10;
                                                 PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                                 if (PlayerControl.coins >= 860)
                                                 {
                                                     txt10.SetActive(false);
                                                     txt11.SetActive(true);
                                                     txt2_10.SetActive(false);
                                                     txt2_11.SetActive(true);
                                                     healthPoints = healthPoints + 10;
                                                     PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                                     if (PlayerControl.coins >= 1000)
                                                     {
                                                         txt11.SetActive(false);
                                                         txtmax.SetActive(true);
                                                         imgmax.SetActive(false);
                                                         txt2_11.SetActive(false);
                                                         txtmax2.SetActive(true);
                                                         imgmax2.SetActive(false);
                                                         healthPoints = healthPoints + 10;
                                                         PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     else healthPoints = 100;
 }  

2 个答案:

答案 0 :(得分:2)

有几种不同的方法可以解决这个问题,但也许最简单的方法是从一个带有阈值的数组开始(确保它的排序最小到最高):

function isValidEmailAddress(emailAddress) {
    return /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/i.test(emailAddress);
}

然后你可以循环遍历你的数组,而不是那些可怕的嵌套var thresholds = new int[] { 30,60,100,150,210,...}; s:

if

答案 1 :(得分:0)

我意识到你设定的等级要求(至少达到450)遵循某种模式。每个级别的要求总是增加10.我认为这是管理XP要求的一种简单但有效的方法 该模式的一个优点是,它可以用二次函数表示。所以我写了一个生成并解决这种二次函数的方法 你给它提供必要的功能参数(基础,增长)和当前点(在这种情况下为黄金),它返回玩家已达到的等级。

举个例子,我在下面的常量中设置的值几乎与您首先列出的要求相匹配。他们是:

  1. 30
  2. 70
  3. 130
  4. 200
  5. 280 ...
  6. 我还使用了一个属性来保持HP与PlayerPrefs同步。您可以在此处添加支持变量,以防止始终从PlayerPrefs中读取。

    public class HpManager : MonoBehaviour
    {
        public int HealthPoints 
        { 
            get 
            { 
                PlayerPrefs.GetFloat("HealthUP")
            }
            set 
            { 
                PlayerPrefs.SetFloat("HealthUP", value); 
            } 
        }   
    
        private const uint BASE_HEALTH_POINTS = 100;            // Your HP for level 0
        private const uint HEALTH_POINTS_PER_LEVEL = 10;        // The amount the HP increase with each level
    
        private const uint BASE_LEVEL_REQUIREMENT = 30;     // The gold required to reach level 1
        private const uint PER_LEVEL_INCREASE = 10;         // The amount the level up requirement increases with every level
        private const uint MAX_LEVEL = 99;
    
    
        private void Start()
        {
            HealthPoints = BASE_HEALTH_POINTS;              // Set a base value for your HP
        }
    
        void HpUpgradelvl()
        {
            int level = GetLevel((uint)PlayerControl.coins, BASE_LEVEL_REQUIREMENT, PER_LEVEL_INCREASE);
            level = Mathf.Clamp(0, MAX_LEVEL);
    
            HealthPoints = BASE_HEALTH_POINTS + level * HEALTH_POINTS_PER_LEVEL;
        }   
    
        private int GetLevel(uint points, uint baseRequirement, uint increaseRequirement)
        {
            float a = 0.5f * increaseRequirement;
            float b = baseRequirement - a;
    
            // Convert quadratic equation "l = ax² + bx" into normalized form "0 = x² + px + q"
            float p = b / a;                                                
            float q = -points / a;                                          // Bring across and normalize
            float level = -p / 2f + Mathf.Sqrt((p * p) / 4f - q);               // Solve for positive value       
    
            return Mathf.FloorToInt(level);                                     // Return reached level
        }
    }
    

    我需要更多信息来帮助您处理文本 它看起来似乎过于复杂。我无法想象你实际上需要所有那些单独的文本对象 究竟是什么,你想在这里做什么?