LightningSpriteSheetCreator.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // Procedural Lightning for Unity
  3. // (c) 2015 Digital Ruby, LLC
  4. // Source code may be used for personal or commercial projects.
  5. // Source code may NOT be redistributed or sold.
  6. //
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using System.Collections;
  10. namespace DigitalRuby.ThunderAndLightning
  11. {
  12. #if UNITY_EDITOR && !UNITY_WEBPLAYER
  13. /// <summary>
  14. /// Helps create lightning sprite sheets
  15. /// </summary>
  16. public class LightningSpriteSheetCreator : MonoBehaviour
  17. {
  18. /// <summary>The width of the spritesheet in pixels</summary>
  19. [Tooltip("The width of the spritesheet in pixels")]
  20. public int Width = 512;
  21. /// <summary>The height of the spritesheet in pixels</summary>
  22. [Tooltip("The height of the spritesheet in pixels")]
  23. public int Height = 512;
  24. /// <summary>The number of rows in the spritesheet</summary>
  25. [Tooltip("The number of rows in the spritesheet")]
  26. public int Rows = 5;
  27. /// <summary>The number of columns in the spritesheet</summary>
  28. [Tooltip("The number of columns in the spritesheet")]
  29. public int Columns = 5;
  30. /// <summary>Delay in between frames to export. Gives time for animations and effects to happen.</summary>
  31. [Tooltip("Delay in between frames to export. Gives time for animations and effects to happen.")]
  32. public float FrameDelay = 0.1f;
  33. /// <summary>Background color for the sprite sheet. Usually black or transparent.</summary>
  34. [Tooltip("Background color for the sprite sheet. Usually black or transparent.")]
  35. public Color BackgroundColor = Color.black;
  36. /// <summary>The full path and file name to save the saved sprite sheet to</summary>
  37. [Tooltip("The full path and file name to save the saved sprite sheet to")]
  38. public string SaveFileName;
  39. /// <summary>The label to notify that the export is working and then completed</summary>
  40. [Tooltip("The label to notify that the export is working and then completed")]
  41. public Text ExportCompleteLabel;
  42. private bool exporting;
  43. private RenderTexture renderTexture;
  44. private void Start()
  45. {
  46. }
  47. private void Update()
  48. {
  49. }
  50. private IEnumerator ExportFrame(int row, int column, float delay)
  51. {
  52. yield return WaitForSecondsLightning.WaitForSecondsLightningPooled(delay);
  53. float cellWidth = (float)Width / (float)Columns;
  54. float cellHeight = (float)Height / (float)Rows;
  55. float x = ((float)column * cellWidth) / (float)Width;
  56. float y = ((float)row * cellHeight) / (float)Height;
  57. float w = cellWidth / (float)Width;
  58. float h = cellHeight / (float)Height;
  59. Camera.main.clearFlags = CameraClearFlags.Nothing;
  60. Camera.main.targetTexture = renderTexture;
  61. Rect existingViewportRect = Camera.main.rect;
  62. Camera.main.rect = new Rect(x, 1.0f - y - h, w, h);
  63. Camera.main.Render();
  64. Camera.main.rect = existingViewportRect;
  65. Camera.main.targetTexture = null;
  66. Camera.main.clearFlags = CameraClearFlags.SolidColor;
  67. }
  68. private IEnumerator PerformExport(float delay)
  69. {
  70. yield return WaitForSecondsLightning.WaitForSecondsLightningPooled(delay);
  71. RenderTexture.active = renderTexture;
  72. Texture2D png = new Texture2D(Width, Height, TextureFormat.ARGB32, false, false);
  73. png.ReadPixels(new Rect(0, 0, Width, Height), 0, 0);
  74. RenderTexture.active = null;
  75. byte[] pngBytes = png.EncodeToPNG();
  76. if (string.IsNullOrEmpty(SaveFileName))
  77. {
  78. SaveFileName = System.IO.Path.Combine(Application.persistentDataPath, "LightningSpriteSheet.png");
  79. }
  80. System.IO.File.WriteAllBytes(SaveFileName, pngBytes);
  81. ExportCompleteLabel.text = "Done!";
  82. exporting = false;
  83. renderTexture = null;
  84. }
  85. /// <summary>
  86. /// Export button is tapped
  87. /// </summary>
  88. public void ExportTapped()
  89. {
  90. if (exporting)
  91. {
  92. return;
  93. }
  94. exporting = true;
  95. ExportCompleteLabel.text = "Processing...";
  96. renderTexture = new RenderTexture(Width, Height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Default);
  97. renderTexture.anisoLevel = 4;
  98. renderTexture.antiAliasing = 4;
  99. RenderTexture.active = renderTexture;
  100. GL.Clear(true, true, BackgroundColor, 0.0f);
  101. RenderTexture.active = null;
  102. float delay = FrameDelay * 0.5f;
  103. for (int i = 0; i < Rows; i++)
  104. {
  105. for (int j = 0; j < Columns; j++)
  106. {
  107. StartCoroutine(ExportFrame(i, j, delay));
  108. delay += FrameDelay;
  109. }
  110. }
  111. StartCoroutine(PerformExport(delay));
  112. }
  113. }
  114. #endif
  115. }