Does a point exist in units of the type of recycling unit

I found an article that says recycling and reusing variables is good practice in unity. So I accepted it. But one thing is unclear: is this applicable to value type variables (integers, vectors)?

is there a point i using this:

int x; Vector3 v; void functionCalledVeryOften(){ x=SomeCalculation(); v=SomeCalc(); //do something with x and v } 

instead of this:

 void functionCalledVeryOften(){ int x=SomeCalculation(); Vector3 v=SomeCalc(); //do something with x and v } 
+5
source share
2 answers

It depends on what you want to do with this object.

Let's take your first example, say we want to access the x and v variables from the second functionCalledEveryOnceSoOften() function. This function does not need any overloads to pass the variables and can just directly access the variables in the class instance.

In the second example, if we want to do the same. We would need to call functionCalledEveryOnceSoOften(int, vector3) since the function would not have direct access to the variables.

In unity, it often happens that functions will need to use the same values ​​as another function, although they may not always be called in a chain. To take this into account in your second example, we must add if expressions to our function to filter this out.

In your first example, however, we could use these variables without a problem. This is one of the reasons why this is often recommended.

In accordance with the performance in the second example, the variable is stored on the stack, unlike the heap, because it is defined within the method, which will be destroyed after the method finishes. Therefore, using a variable memory is not really a concern. There may be small overhead to re-create and destroy, but this should be negligible.

In the first example, you save the variable on the heap, since it is defined within the class, it will be destroyed only with the class and create an instance on it. This means that memory can be used for longer periods of time, but there will be no overhead to create / destroy a variable. It is also usually negligible.

All together, if you do not create thousands of these objects, you quickly get access to variables, you most likely will not notice a big difference in performance.

The biggest difference will most likely be the way code is written. Better or worse.

+2
source

Does it make sense in units like disposal units

Yes, some data types are not all.

Does this apply to value type variables (integers, vectors)?

No.

It depends on the type of variable.

This means that it does not apply to int , double , float , bool , Vector3 and Vector2 and other similar data types. It doesn't even apply to string , because already, string cannot be reused in C #. strings are immutable.

In fact, using int from a local variable, say in a while is faster than using an int declared as global.

* Examples of when you should declare a variable once and reuse it, or in your own words, recycle or reuse variables in Unity *.

Arrays

If the function contains an array and this function is often called.

 void functionCalledVeryOften() { float[] playerLives = new float[5]; //This is bad because it allocates memory each time it is called for (int i = 0; i < playerLives.Length; i++) { playerLives[i] = UnityEngine.Random.Range(0f,5f); } } 

This allocates memory every time and can be solved by creating a global array and initializing it outside the function once. You can create a simple function that flushes data in an array to 0.

 float[] playerLives = new float[5]; void functionCalledVeryOften() { for (int i = 0; i < playerLives.Length; i++) { playerLives[i] = UnityEngine.Random.Range(0f,5f); } } 

Creating new objects :

Creating new objects requires resources and can cause problems on mobile devices. It depends on how often you do it.

The code below creates a GameObject (bullet), then attaches a Rigidbody to it, and then removes it. This happens every frame when a space is held and finally destroys the mark 10 seconds later.

 void functionCalledVeryOften() { if (Input.GetKey(KeyCode.Space)) { //Create new Bullet each time GameObject myObject = new GameObject("bullet"); Rigidbody bullet = myObject.AddComponent<Rigidbody>() as Rigidbody; //Shoot Bullet bullet.velocity = transform.forward * 50; Destroy(myObject); } } 

The code is invalid because it allocates memory every time a new GameObject is created and when a GameObject is destroyed, it also starts the garbage collector. This can slow down and cause hiccups in your game.

The solution to the above code is to combine objects. You can learn more about it here: Unity Object Combination Tutorial

An example of a simple fix for this with a global variable:

 List<GameObject> reUsableBullets; int toUseIndex = 0; void Start() { intitOnce(); } //Call this function once to create bullets void intitOnce() { reUsableBullets = new List<GameObject>(); //Create 20 bullets then store the reference to a global variable for re-usal for (int i = 0; i < 20; i++) { reUsableBullets[i] = new GameObject("bullet"); reUsableBullets[i].AddComponent<Rigidbody>(); reUsableBullets[i].SetActive(false); } } void functionCalledVeryOften() { if (Input.GetKey(KeyCode.Space)) { //Re-use old bullet reUsableBullets[toUseIndex].SetActive(true); Rigidbody tempRgb = reUsableBullets[toUseIndex].GetComponent<Rigidbody>(); tempRgb.velocity = transform.forward * 50; toUseIndex++; //reset counter if (toUseIndex == reUsableBullets.Count - 1) { toUseIndex = 0; } } } 

So, basically you create an object inside the function before the game starts, and then save the link in a global variable. Then you reuse the object that you created in the function, since its reference is stored in a global variable.

Instantiate

The Instantiate function is used to create a copy of the collection. The code below will instantiate the bullet, and then shoots at each frame until the space is held and finally destroys it 10 seconds later.

 public GameObject bulletPrefab; void functionCalledVeryOften() { if (Input.GetKey(KeyCode.Space)) { //Create new Bullet each time Rigidbody bullet = Instantiate(bulletPrefab, new Vector3(0, 0, 0), Quaternion.identity) as Rigidbody; //Shoot Bullet bullet.velocity = transform.forward * 50; Destroy(myObject,10f); } } 

The code is invalid because it allocates memory depending on how many components are attached to the bullet assembly and how many child GameObject are under it. The solution also uses a combination of objects. Create a GameObject in a function, save the link in a global variable, and then reuse it. The solution is the same with the solution above.

In conclusion, the sample code does not apply to your question.

You can learn more about memory management in Unity here .

+5
source

Source: https://habr.com/ru/post/1258787/


All Articles