简体   繁体   English

避免 Unity 的全局变量。 如何以及为什么?

[英]Avoiding global variables for Unity. How and why?

I've seen multiple warnings about usage of global/public static variables for my code.我已经看到多个关于在我的代码中使用全局/公共 static 变量的警告。 I was wondering how important is it?我想知道它有多重要? How many global variables can I get away with using or should I attempt to use none at all?我可以使用多少个全局变量,或者我应该尝试不使用多少?

And if so how do you solve this issue?如果是这样,你如何解决这个问题? For example: I have a "Stamina bar" in my game.例如:我的游戏中有一个“耐力条”。 There's a double variable corresponding to it, the code for the bar itself references the variable to change its size to represent the lower stamina.有一个与之对应的双变量,条形图本身的代码引用该变量来更改其大小以表示较低的耐力。

        void Update()
        {
            if (FourEvents.CHARGE < 10)
            {
                transform.localScale = new Vector2((float)(FourEvents.CHARGE / 3.79), .76f);
            }
        }

Another gameObject refills the stamina bar through its collision:另一个游戏对象通过碰撞重新填充耐力条:

        void OnCollisionEnter2D(Collision2D collision)
        {
            if (FourEvents.CHARGE <= 9)
            {
                FourEvents.CHARGE += 1;
                Debug.Log(FourEvents.CHARGE);
            }
        }

Although there's other codes referencing this variable, I'll avoid posting them to save space.虽然还有其他代码引用了这个变量,但我会避免发布它们以节省空间。 Lastly:最后:

        public static double CHARGE;
        public int counter;
        // Start is called before the first frame update
        void Start()
        {
            CHARGE += 10;
        }

        // Update is called once per frame
        void FixedUpdate()
        {
            counter += 1;
            if (counter > 1000)
            {
                counter = 0;
            }
            if (Input.GetKey(KeyCode.Space) && counter % 10 == 0)
            {
                if (CHARGE >= 1)
                {
                    CHARGE = CHARGE - .4;
                    Debug.Log(CHARGE);
                }
                else
                {
                    CHARGE = 0;
                }
            }
        }

A solution that encapsulates the data of charge logic: Create a class which handles your charge logic封装充电逻辑数据的解决方案:创建一个处理您的充电逻辑的 class

class ChargeHandler
{
    double charge;
    int chargeRefillLimit = 9;
    double initChargeValue = 10;
    public double GetCurrentCharge()
    {
        return charge;
    }

    public void RefillCharge()
    {
        if(charge <= chargeRefillLimit)
        {
            charge += 1;
        }
    }

    public void InitCharge()
    {
        charge = initChargeValue;
    }

    public void PerformCharge()
    {
        if(charge >= 1)
        {
            charge -= 4;
        }
        else
        {
            charge = 0;
        }
    }
}

and then use it like so:然后像这样使用它:

ChargeHandler chargeHandler = new ChargeHandler();

void Start()
{
    chargeHandler.InitCharge();
}
void OnCollisionEnter2D(Collision2D collision)
{
    chargeHandler.RefillCharge();
}
void FixedUpdate()
{
    counter += 1;
        if (counter > 1000)
        {
            counter = 0;
        }
        if (Input.GetKey(KeyCode.Space) && counter % 10 == 0)
        {
             chargeHandler.PerformCharge();
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM