UniGifExtension.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// Extension methods class
  5. /// </summary>
  6. public static class UniGifExtension
  7. {
  8. /// <summary>
  9. /// Convert BitArray to int (Specifies the start index and bit length)
  10. /// </summary>
  11. /// <param name="startIndex">Start index</param>
  12. /// <param name="bitLength">Bit length</param>
  13. /// <returns>Converted int</returns>
  14. public static int GetNumeral(this BitArray array, int startIndex, int bitLength)
  15. {
  16. var newArray = new BitArray(bitLength);
  17. for (int i = 0; i < bitLength; i++)
  18. {
  19. if (array.Length <= startIndex + i)
  20. {
  21. newArray[i] = false;
  22. }
  23. else
  24. {
  25. bool bit = array.Get(startIndex + i);
  26. newArray[i] = bit;
  27. }
  28. }
  29. return newArray.ToNumeral();
  30. }
  31. /// <summary>
  32. /// Convert BitArray to int
  33. /// </summary>
  34. /// <returns>Converted int</returns>
  35. public static int ToNumeral(this BitArray array)
  36. {
  37. if (array == null)
  38. {
  39. Debug.LogError("array is nothing.");
  40. return 0;
  41. }
  42. if (array.Length > 32)
  43. {
  44. Debug.LogError("must be at most 32 bits long.");
  45. return 0;
  46. }
  47. var result = new int[1];
  48. array.CopyTo(result, 0);
  49. return result[0];
  50. }
  51. }