Files
BABA_YAGA/Assets/Scripts/Optimization/StressTestSpawner.cs
2026-03-26 20:27:19 +07:00

86 lines
2.8 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using OnlyScove.Scripts;
namespace Elbyss.Optimization
{
public class StressTestSpawner : MonoBehaviour
{
[Header("Spawn Settings")]
public GameObject prefabToSpawn;
public int spawnLimit = 1000;
public int spawnsPerFrame = 10;
public float spacing = 2.0f;
[Header("Testing Mode")]
public bool useAutoStateMachine = true;
[Header("Optimization Options")]
public bool stripHeavyComponents = false;
private List<GameObject> spawnedObjects = new List<GameObject>();
private int currentSpawnCount = 0;
private bool isSpawning = false;
private void Update()
{
if (isSpawning && currentSpawnCount < spawnLimit)
{
for (int i = 0; i < spawnsPerFrame && currentSpawnCount < spawnLimit; i++)
{
SpawnObject();
}
}
}
[ContextMenu("Start Stress Test")]
public void StartStressTest() => isSpawning = true;
[ContextMenu("Clear All")]
public void ClearAll()
{
foreach (var obj in spawnedObjects) if (obj != null) Destroy(obj);
spawnedObjects.Clear();
currentSpawnCount = 0;
isSpawning = false;
}
private void SpawnObject()
{
if (prefabToSpawn == null) return;
int rowSize = Mathf.CeilToInt(Mathf.Sqrt(spawnLimit));
float x = (currentSpawnCount % rowSize) * spacing;
float z = (currentSpawnCount / rowSize) * spacing;
Vector3 spawnPos = transform.position + new Vector3(x, 0, z);
GameObject newObj = Instantiate(prefabToSpawn, spawnPos, transform.rotation);
if (useAutoStateMachine)
{
var realInput = newObj.GetComponent<InputReader>();
if (realInput != null) realInput.enabled = false;
var originalSM = newObj.GetComponent<PlayerStateMachine>();
if (originalSM != null)
{
DestroyImmediate(originalSM);
var autoSM = newObj.AddComponent<AutoPlayerStateMachine>();
autoSM.alwaysMoveForward = true;
autoSM.alwaysRun = true;
}
}
if (stripHeavyComponents)
{
if (newObj.TryGetComponent<Animator>(out var anim)) anim.enabled = false;
if (newObj.TryGetComponent<CharacterController>(out var cc)) cc.enabled = false;
if (newObj.TryGetComponent<PlayerStateMachine>(out var sm)) sm.enabled = false;
}
spawnedObjects.Add(newObj);
currentSpawnCount++;
}
}
}