Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using UnityEngine;
- public class Runner : MonoBehaviour
- {
- public Vector3 StartPosition = new Vector3 ( 10, 0, 0 );
- public Vector3 EndPosition = new Vector3 ( -10, 0, 0 );
- // Each Unity unit is considered a metre.
- public float MetresPerSecond;
- [Tooltip("The Prefab without a RigidBody")]
- public GameObject TransformPrefab;
- [Tooltip ( "The Prefab with a RigidBody" )]
- public GameObject RigidBodyPrefab;
- public int [ ] NumberToInstantiate = new int [ ] { 10_000, 20_000, 30_000 };
- private List<GameObject> TransformObjects;
- private List<Rigidbody> RigidBodyObjects;
- private float timer = 0;
- private float distanceToTravel;
- private float velocityReciprocal;
- private bool started = false;
- private bool finished = false;
- private int frameCount = 0;
- private int frameCountFixed = 0;
- private bool fixedFinished;
- private float startTime;
- Stopwatch stopwatch = new Stopwatch ( );
- private int currentlyTransformTest = 1;
- private int currentInstantiationIndex = 0;
- private string [ ] testName = new string[] { "RigidBody", "Transform" };
- private float xPosition;
- void Start()
- {
- distanceToTravel = ( StartPosition - EndPosition ).magnitude;
- velocityReciprocal = 1 / ( distanceToTravel / MetresPerSecond );
- Setup ( );
- }
- void Setup ( )
- {
- started = finished = fixedFinished = false;
- timer = 0;
- // Instantiate both of the lists of objects.
- TransformObjects = new List<GameObject> ( NumberToInstantiate [ currentInstantiationIndex ] );
- RigidBodyObjects = new List<Rigidbody> ( NumberToInstantiate [ currentInstantiationIndex ] );
- for ( int i = 0; i < NumberToInstantiate [ currentInstantiationIndex ]; ++i )
- {
- TransformObjects.Add ( Instantiate ( TransformPrefab, StartPosition, Quaternion.identity ) );
- RigidBodyObjects.Add ( Instantiate ( RigidBodyPrefab, StartPosition, Quaternion.identity ).GetComponent<Rigidbody> ( ) );
- RigidBodyObjects [ i ].velocity = ( EndPosition - StartPosition ) * velocityReciprocal;
- RigidBodyObjects [ i ].gameObject.SetActive ( false );
- }
- }
- void Reset ( )
- {
- started = finished = fixedFinished = false;
- timer = 0;
- // Flip the test.
- currentlyTransformTest = 1 - currentlyTransformTest;
- if ( currentlyTransformTest == 1 )
- {
- // Destroy the objects in both lists.
- for ( int i = 0; i < NumberToInstantiate [ currentInstantiationIndex ]; ++i )
- {
- Destroy ( TransformObjects [ i ] );
- Destroy ( RigidBodyObjects [ i ].gameObject );
- }
- // Increase the currentInstantiationIndex
- currentInstantiationIndex++;
- if ( currentInstantiationIndex >= NumberToInstantiate.Length ) currentInstantiationIndex = 0;
- // Now Setup the lists again.
- Setup ( );
- return;
- }
- else
- {
- for ( int i = 0; i < NumberToInstantiate [ currentInstantiationIndex ]; ++i )
- RigidBodyObjects [ i ].gameObject.SetActive ( true );
- }
- }
- void Update()
- {
- if ( finished ) return;
- if ( !started )
- {
- startTime = Time.time;
- stopwatch.Restart ( );
- stopwatch.Start ( );
- started = true;
- frameCount = frameCountFixed = 0;
- }
- frameCount++;
- timer += (Time.deltaTime * velocityReciprocal);
- if ( currentlyTransformTest == 1 )
- {
- for ( int i = 0; i < NumberToInstantiate [ currentInstantiationIndex ]; ++i )
- TransformObjects [ i ].transform.localPosition = Vector3.Lerp ( StartPosition, EndPosition, timer );
- xPosition = TransformObjects [ 0 ].transform.localPosition.x;
- }
- else
- xPosition = RigidBodyObjects [ 0 ].transform.localPosition.x;
- // We can cheat here, because we know that the object is moving right to left, along the x axis.
- if ( xPosition <= EndPosition.x )
- {
- stopwatch.Stop ( );
- finished = true;
- UnityEngine.Debug.Log ( $"Finished running using {testName[currentlyTransformTest]} test. {NumberToInstantiate [ currentInstantiationIndex ]} Objects.\n" +
- $"Elapsed Time : { Time.time - startTime }/{ stopwatch.Elapsed.TotalSeconds } seconds (Unity/StopWatch)." +
- $" Frames : {frameCount}. Frame Rate : { frameCount / ( Time.time - startTime ) }/{ frameCount / stopwatch.Elapsed.TotalSeconds }" );
- Reset ( );
- }
- }
- private void FixedUpdate ( )
- {
- if ( finished || !started || fixedFinished ) return;
- frameCountFixed++;
- if ( currentlyTransformTest == 0 )
- xPosition = RigidBodyObjects [ 0 ].transform.localPosition.x;
- else
- xPosition = TransformObjects [ 0 ].transform.localPosition.x;
- if ( xPosition <= EndPosition.x )
- {
- fixedFinished = true;
- UnityEngine.Debug.Log ( $"FixedUpdate {testName [ currentlyTransformTest ]} crossed the line at { Time.time - startTime }/{ stopwatch.Elapsed.TotalSeconds }" +
- $" seconds (Unity/StopWatch)!\nFrame {frameCount} and Fixed Frame {frameCountFixed}." );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment