Utils.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using UnityEngine;
  2. using System.IO;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. //-----------------------------------------------------------------------------
  6. // Copyright 2012-2017 RenderHeads Ltd. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. namespace RenderHeads.Media.AVProMovieCapture
  9. {
  10. public class Utils
  11. {
  12. /// <summary>
  13. /// The "main" camera isn't necessarily the one the gets rendered last to screen,
  14. /// so we sort all cameras by depth and find the one with no render target
  15. /// </summary>
  16. public static Camera GetUltimateRenderCamera()
  17. {
  18. Camera result = Camera.main;
  19. {
  20. // Iterate all enabled cameras
  21. float highestDepth = float.MinValue;
  22. Camera[] enabledCameras = Camera.allCameras;
  23. for (int cameraIndex = 0; cameraIndex < enabledCameras.Length; cameraIndex++)
  24. {
  25. Camera camera = enabledCameras[cameraIndex];
  26. // Ignore null cameras
  27. if (camera != null)
  28. {
  29. // Ignore cameras that are hidden or have a targetTexture
  30. bool isHidden = (camera.hideFlags & HideFlags.HideInHierarchy) == HideFlags.HideInHierarchy;
  31. if (!isHidden && camera.targetTexture == null)
  32. {
  33. // Ignore cameras that render nothing
  34. if (camera.pixelRect.width > 0f && camera.pixelHeight > 0f)
  35. {
  36. // Keep the one with highest depth
  37. // TODO: handle the case where camera depths are equal - which one is first then?
  38. if (camera.depth >= highestDepth)
  39. {
  40. highestDepth = camera.depth;
  41. result = camera;
  42. }
  43. }
  44. }
  45. }
  46. }
  47. }
  48. return result;
  49. }
  50. public static bool HasContributingCameras(Camera parentCamera)
  51. {
  52. bool result = true;
  53. // If the camera doesn't clear the target completely then it may have other contributing cameras
  54. if (parentCamera.rect == new Rect(0f, 0f, 1f, 1f))
  55. {
  56. if (parentCamera.clearFlags == CameraClearFlags.Skybox ||
  57. parentCamera.clearFlags == CameraClearFlags.Color)
  58. {
  59. result = false;
  60. }
  61. }
  62. return result;
  63. }
  64. /// <summary>
  65. /// Returns a list of cameras sorted in render order from first to last that contribute to the rendering to parentCamera
  66. /// </summary>
  67. public static Camera[] FindContributingCameras(Camera parentCamera)
  68. {
  69. System.Collections.Generic.List<Camera> validCameras = new System.Collections.Generic.List<Camera>(8);
  70. {
  71. // Iterate all enabled cameras
  72. Camera[] enabledCameras = Camera.allCameras;
  73. for (int cameraIndex = 0; cameraIndex < enabledCameras.Length; cameraIndex++)
  74. {
  75. Camera camera = enabledCameras[cameraIndex];
  76. // Ignore null cameras and camera that is the parent
  77. if (camera != null && camera != parentCamera)
  78. {
  79. // Only allow cameras with depth less or equal to parent camera
  80. // TODO: handle the case where camera depths are equal - which one is first then?
  81. if (camera.depth <= parentCamera.depth)
  82. {
  83. // Ignore cameras that are hidden or have a targetTexture that doesn't match the parent
  84. bool isHidden = (camera.hideFlags & HideFlags.HideInHierarchy) == HideFlags.HideInHierarchy;
  85. if (!isHidden && camera.targetTexture == parentCamera.targetTexture)
  86. {
  87. // Ignore cameras that render nothing
  88. if (camera.pixelRect.width > 0 && camera.pixelHeight > 0)
  89. {
  90. validCameras.Add(camera);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. if (validCameras.Count > 1)
  98. {
  99. // Sort by depth (render order)
  100. validCameras.Sort(delegate (Camera a, Camera b)
  101. {
  102. if (a.depth < b.depth)
  103. {
  104. return -1;
  105. }
  106. else if (a.depth > b.depth)
  107. {
  108. return 1;
  109. }
  110. return 0;
  111. });
  112. // Starting from the last camera to render, find the first one that clears the screen completely
  113. for (int i = (validCameras.Count - 1); i >= 0; i--)
  114. {
  115. if (validCameras[i].rect == new Rect(0f, 0f, 1f, 1f))
  116. {
  117. if (validCameras[i].clearFlags == CameraClearFlags.Skybox ||
  118. validCameras[i].clearFlags == CameraClearFlags.Color)
  119. {
  120. // Remove all cameras before this
  121. validCameras.RemoveRange(0, i);
  122. break;
  123. }
  124. }
  125. }
  126. }
  127. return validCameras.ToArray();
  128. }
  129. public static bool ShowInExplorer(string itemPath)
  130. {
  131. bool result = false;
  132. #if !UNITY_WEBPLAYER
  133. itemPath = Path.GetFullPath(itemPath.Replace(@"/", @"\")); // explorer doesn't like front slashes
  134. if (File.Exists(itemPath))
  135. {
  136. Process.Start("explorer.exe", "/select," + itemPath);
  137. result = true;
  138. }
  139. #endif
  140. return result;
  141. }
  142. public static bool OpenInDefaultApp(string itemPath)
  143. {
  144. bool result = false;
  145. itemPath = Path.GetFullPath(itemPath.Replace(@"/", @"\"));
  146. if (File.Exists(itemPath))
  147. {
  148. UnityEngine.Application.OpenURL(itemPath);
  149. result = true;
  150. }
  151. return result;
  152. }
  153. public static long GetFileSize(string filename)
  154. {
  155. #if UNITY_WEBPLAYER
  156. return 0;
  157. #else
  158. System.IO.FileInfo fi = new System.IO.FileInfo(filename);
  159. return fi.Length;
  160. #endif
  161. }
  162. #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
  163. [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  164. [return: MarshalAs(UnmanagedType.Bool)]
  165. private static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
  166. out ulong lpFreeBytesAvailable,
  167. out ulong lpTotalNumberOfBytes,
  168. out ulong lpTotalNumberOfFreeBytes);
  169. public static bool DriveFreeBytes(string folderName, out ulong freespace)
  170. {
  171. freespace = 0;
  172. if (string.IsNullOrEmpty(folderName))
  173. {
  174. throw new System.ArgumentNullException("folderName");
  175. }
  176. if (!folderName.EndsWith("\\"))
  177. {
  178. folderName += '\\';
  179. }
  180. ulong free = 0, dummy1 = 0, dummy2 = 0;
  181. if (GetDiskFreeSpaceEx(folderName, out free, out dummy1, out dummy2))
  182. {
  183. freespace = free;
  184. return true;
  185. }
  186. else
  187. {
  188. return false;
  189. }
  190. }
  191. #endif
  192. }
  193. }