Added performance optimization Unity project with mass instantiation script
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using Unity.Profiling;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public class MassInstantiation : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private InputActionProperty toggleMassInstantiation;
|
||||
[SerializeField] private InputActionProperty deleteAllObjects;
|
||||
[SerializeField] private GameObject prefab;
|
||||
[SerializeField] private int objectsToInstantiate = 100;
|
||||
[SerializeField] private bool isInstantiating;
|
||||
|
||||
private static readonly ProfilerMarker InstantiateObjectsMarker = new ProfilerMarker("InstantiateObjects");
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
toggleMassInstantiation.action.Enable();
|
||||
toggleMassInstantiation.action.performed += _ => isInstantiating = !isInstantiating;
|
||||
|
||||
deleteAllObjects.action.Enable();
|
||||
deleteAllObjects.action.performed += _ => DeleteAllObjects();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
toggleMassInstantiation.action.Disable();
|
||||
deleteAllObjects.action.Disable();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (isInstantiating) InstantiateObjects();
|
||||
}
|
||||
|
||||
private void InstantiateObjects()
|
||||
{
|
||||
InstantiateObjectsMarker.Begin();
|
||||
for (int i = 0; i < objectsToInstantiate; i++)
|
||||
{
|
||||
Instantiate(prefab, transform);
|
||||
}
|
||||
InstantiateObjectsMarker.End();
|
||||
}
|
||||
|
||||
private void DeleteAllObjects()
|
||||
{
|
||||
for (int i = transform.childCount - 1; i >= 0; i--)
|
||||
{
|
||||
Destroy(transform.GetChild(i).gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user