ArrayHelper.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using UnityEngine;
  3. #pragma warning disable 1591 // undocumented XML code warning
  4. static public class ArrayHelper
  5. {
  6. #if !UNITY_FLASH || UNITY_EDITOR
  7. static public T AddArrayElement<T>( ref T[] array ) where T : new()
  8. {
  9. return AddArrayElement<T>( ref array, new T() );
  10. }
  11. static public T AddArrayElement<T>( ref T[] array, T elToAdd )
  12. {
  13. if ( array == null )
  14. {
  15. array = new T[ 1 ];
  16. array[ 0 ] = elToAdd;
  17. return elToAdd;
  18. }
  19. var newArray = new T[ array.Length + 1 ];
  20. array.CopyTo( newArray, 0 );
  21. newArray[ array.Length ] = elToAdd;
  22. array = newArray;
  23. return elToAdd;
  24. }
  25. static public void DeleteArrayElement<T>( ref T[] array, int index )
  26. {
  27. if ( index >= array.Length || index < 0 )
  28. {
  29. Debug.LogWarning( "invalid index in DeleteArrayElement: " + index );
  30. return;
  31. }
  32. var newArray = new T[ array.Length - 1 ];
  33. int i;
  34. for ( i = 0; i < index; i++ )
  35. {
  36. newArray[ i ] = array[ i ];
  37. }
  38. for ( i = index + 1; i < array.Length; i++ )
  39. {
  40. newArray[ i - 1 ] = array[ i ];
  41. }
  42. array = newArray;
  43. }
  44. #endif
  45. }