SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

443 lines
13 KiB

4 years ago
  1. using UnityEngine;
  2. using System;
  3. using System.IO;
  4. using System.Collections;
  5. public static class Capture {
  6. //the name of the screenshot
  7. private static string _Name = "screenshot";
  8. //the extention the screenshot will have
  9. private static string _Extention = ".png";
  10. //the path the screenshot will be saved in
  11. private static string _ScreenShotPath = "";
  12. /// <summary>
  13. /// the pixels per unity...used while converting a ScreenShot to a sprite
  14. /// </summary>
  15. public static int PxPerUnit = 100;
  16. //returns the fill path of a ScreenShot
  17. private static string GetFullPath(int ScreenShotNum = 0 )
  18. {
  19. return _ScreenShotPath + "/" +_Name + ScreenShotNum.ToString() + _Extention ;
  20. }
  21. /// <summary>
  22. /// Takes the ScreenShot.
  23. /// </summary>
  24. /// <param name="ScreenShotNum">ScreenShot number.</param>
  25. public static void TakeScreenShot(int ScreenShotNum = 0)
  26. {
  27. _ScreenShotPath = Application.dataPath + "/ScreenShots";
  28. if (!Directory.Exists(_ScreenShotPath))
  29. {
  30. Directory.CreateDirectory(_ScreenShotPath);
  31. }
  32. ScreenCapture.CaptureScreenshot(GetFullPath(ScreenShotNum));
  33. Debug.Log("screenshot created at " + GetFullPath(ScreenShotNum));
  34. /*
  35. !!! NOTE !!!
  36. the below is my attempt at forcing the system to wait until the file is saved before executing any more processes.
  37. however this caused a small freeze in the game.
  38. */
  39. // filePath = GetFullPath(ScreenShotNum);
  40. //
  41. // System.Threading.Thread m_Thread = null;
  42. // m_Thread = new System.Threading.Thread(FileCheck);
  43. // m_Thread.Start();
  44. }
  45. /*
  46. !!! NOTE !!!
  47. the below is my attempt at forcing the system to wait until the file is saved before executing any more processes.
  48. however this caused a small freeze in the game.
  49. */
  50. // static string filePath;
  51. // private static void FileCheck()
  52. // {
  53. // while(!File.Exists(filePath))
  54. // {
  55. // //wait
  56. // }
  57. //
  58. // //return
  59. // }
  60. /// <summary>
  61. /// checks if the ScreenShot exist.
  62. /// </summary>
  63. /// <returns><c>true</c>, if ScreenShot exist was does, <c>false</c> otherwise.</returns>
  64. /// <param name="ScreenShotNum">ScreenShot number.</param>
  65. public static bool doesScreenshotExist(int ScreenShotNum = 0)
  66. {
  67. return File.Exists(GetFullPath(ScreenShotNum));
  68. }
  69. /// <summary>
  70. /// Gets the ScreenShot as texture2d.
  71. /// </summary>
  72. /// <returns>The ScreenShot texture2d.</returns>
  73. /// <param name="ScreenShotNum">ScreenShot number.</param>
  74. public static Texture2D GetScreenShot_Texture2D(int ScreenShotNum = 0 )
  75. {
  76. if (GetFullPath(ScreenShotNum) == "")
  77. {
  78. Debug.Log("No screenshot was taken yet");
  79. }
  80. return GetScreenShot(GetFullPath(ScreenShotNum));
  81. }
  82. /// <summary>
  83. /// Gets the ScreenShot as texture2d.
  84. /// </summary>
  85. /// <returns>The ScreenShot texture2d.</returns>
  86. /// <param name="x">The x coordinate.</param>
  87. /// <param name="y">The y coordinate.</param>
  88. /// <param name="cropSize">Crop size.</param>
  89. /// <param name="ScreenShotNum">ScreenShot number.</param>
  90. public static Texture2D GetScreenShot_Texture2D(int x, int y, int cropSize,int ScreenShotNum = 0 )
  91. {
  92. if (GetFullPath(ScreenShotNum) == "")
  93. {
  94. Debug.Log("No screenshot was taken yet");
  95. }
  96. Texture2D tex = GetScreenShot(GetFullPath(ScreenShotNum));
  97. return Crop(tex,x,y,cropSize);
  98. }
  99. /// <summary>
  100. /// Gets the ScreenShot as texture2d.
  101. /// </summary>
  102. /// <returns>The ScreenShot texture2d.</returns>
  103. /// <param name="WorldPosition">World position.</param>
  104. /// <param name="cropSize">Crop size.</param>
  105. /// <param name="ScreenShotNum">ScreenShot number.</param>
  106. public static Texture2D GetScreenShot_Texture2D(Vector3 WorldPosition, int cropSize,int ScreenShotNum = 0 )
  107. {
  108. if (GetFullPath(ScreenShotNum) == "")
  109. {
  110. Debug.Log("No screenshot was taken yet");
  111. }
  112. Vector3 V3 = Camera.main.WorldToScreenPoint(WorldPosition);
  113. return GetScreenShot_Texture2D((int) V3.x, (int) V3.y, cropSize,ScreenShotNum);
  114. }
  115. /// <summary>
  116. /// Gets the ScreenShot as a sprite.
  117. /// </summary>
  118. /// <returns>sprite.</returns>
  119. /// <param name="ScreenShotNum">ScreenShot number.</param>
  120. public static Sprite GetScreenShot_Sprite(int ScreenShotNum = 0 )
  121. {
  122. if (GetFullPath(ScreenShotNum) == "")
  123. {
  124. Debug.Log("No screenshot was taken yet");
  125. }
  126. Texture2D tex = GetScreenShot(GetFullPath(ScreenShotNum));
  127. Rect rect = new Rect(0, 0, tex.width,tex.height);
  128. return Sprite.Create(tex,rect,new Vector2(0.5f,0.5f),PxPerUnit);
  129. }
  130. /// <summary>
  131. /// Gets the ScreenShot as a sprite.
  132. /// </summary>
  133. /// <returns>sprite.</returns>
  134. /// <param name="x">The x coordinate.</param>
  135. /// <param name="y">The y coordinate.</param>
  136. /// <param name="cropSize">Crop size.</param>
  137. /// <param name="ScreenShotNum">ScreenShot number.</param>
  138. public static Sprite GetScreenShot_Sprite(int x, int y , int cropSize, int ScreenShotNum = 0 )
  139. {
  140. if (GetFullPath(ScreenShotNum) == "")
  141. {
  142. Debug.Log("No screenshot was taken yet");
  143. }
  144. Texture2D tex1 = GetScreenShot(GetFullPath(ScreenShotNum));
  145. Texture2D tex2 = Crop(tex1,x,y,cropSize);
  146. Rect rect = new Rect(0, 0, tex2.width,tex2.height);
  147. return Sprite.Create(tex2,rect,new Vector2(0.5f,0.5f),PxPerUnit);
  148. }
  149. /// <summary>
  150. /// Gets the ScreenShot as a sprite.
  151. /// </summary>
  152. /// <returns>sprite.</returns>
  153. /// <param name="WorldPosition">World position.</param>
  154. /// <param name="cropSize">Crop size.</param>
  155. /// <param name="ScreenShotNum">ScreenShot number.</param>
  156. public static Sprite GetScreenShot_Sprite(Vector3 WorldPosition, int cropSize, int ScreenShotNum = 0 )
  157. {
  158. if (GetFullPath(ScreenShotNum) == "")
  159. {
  160. Debug.Log("No screenshot was taken yet");
  161. }
  162. Vector3 V3 = Camera.main.WorldToScreenPoint(WorldPosition);
  163. return GetScreenShot_Sprite((int) V3.x, (int) V3.y , cropSize, ScreenShotNum );
  164. }
  165. /// <summary>
  166. /// Gets the ScreenShot as PNG/byte[]
  167. /// </summary>
  168. /// <returns>byte[]</returns>
  169. /// <param name="ScreenShotNum">ScreenShot number.</param>
  170. public static byte[] GetScreenShot_PNG(int ScreenShotNum = 0)
  171. {
  172. if (GetFullPath(ScreenShotNum) == "")
  173. {
  174. Debug.Log("No screenshot was taken yet");
  175. }
  176. return ConvertToPNG(GetScreenShot(GetFullPath(ScreenShotNum)));
  177. }
  178. /// <summary>
  179. /// Gets the ScreenShot as PNG/byte[]
  180. /// </summary>
  181. /// <returns>byte[]</returns>
  182. /// <param name="x">The x coordinate.</param>
  183. /// <param name="y">The y coordinate.</param>
  184. /// <param name="cropSize">Crop size.</param>
  185. /// <param name="ScreenShotNum">ScreenShot number.</param>
  186. public static byte[] GetScreenShot_PNG(int x, int y , int cropSize, int ScreenShotNum = 0)
  187. {
  188. if (GetFullPath(ScreenShotNum) == "")
  189. {
  190. Debug.Log("No screenshot was taken yet");
  191. }
  192. Texture2D tex1 = GetScreenShot(GetFullPath(ScreenShotNum));
  193. Texture2D tex2 = Crop(tex1,x,y,cropSize);
  194. return ConvertToPNG(tex2);
  195. }
  196. /// <summary>
  197. /// Gets the ScreenShot as PNG/byte[]
  198. /// </summary>
  199. /// <returns>byte[]</returns>
  200. /// <param name="WorldPosition">World position.</param>
  201. /// <param name="cropSize">Crop size.</param>
  202. /// <param name="ScreenShotNum">ScreenShot number.</param>
  203. public static byte[] GetScreenShot_PNG(Vector3 WorldPosition, int cropSize, int ScreenShotNum = 0)
  204. {
  205. if (GetFullPath(ScreenShotNum) == "")
  206. {
  207. Debug.Log("No screenshot was taken yet");
  208. }
  209. Vector3 V3 = Camera.main.WorldToScreenPoint(WorldPosition);
  210. return GetScreenShot_PNG((int) V3.x, (int) V3.y , cropSize, ScreenShotNum);
  211. }
  212. /// <summary>
  213. /// Gets the ScreenShot.
  214. /// </summary>
  215. /// <returns>The ScreenShot.</returns>
  216. /// <param name="filePath">File path.</param>
  217. private static Texture2D GetScreenShot(string filePath)
  218. {
  219. Texture2D tex = null;
  220. byte[] fileData;
  221. if (File.Exists(filePath))
  222. {
  223. // Debug.Log("true");
  224. fileData = File.ReadAllBytes(filePath);
  225. tex = new Texture2D(Screen.width,Screen.height);
  226. tex.LoadImage(fileData);
  227. }
  228. if (tex == null)
  229. {
  230. Debug.LogError("the file doesn't exist yet");
  231. }
  232. return tex;
  233. }
  234. /// <summary>
  235. /// Crop the specified tex, x, y and cropSize.
  236. /// </summary>
  237. /// <param name="tex">Tex.</param>
  238. /// <param name="x">The x coordinate.</param>
  239. /// <param name="y">The y coordinate.</param>
  240. /// <param name="cropSize">Crop size.</param>
  241. public static Texture2D Crop(Texture2D tex,int x, int y,int cropSize)
  242. {
  243. Texture2D tex2 = new Texture2D(cropSize, cropSize, TextureFormat.RGB24, false);
  244. Debug.Log(x);
  245. Debug.Log(y);
  246. Debug.Log(x - (cropSize/2));
  247. Debug.Log(y - (cropSize/2));
  248. Debug.Log(cropSize);
  249. if (0 > x - (cropSize/2))
  250. {
  251. x = (cropSize/2);
  252. }
  253. if (Screen.width < x + (cropSize/2))
  254. {
  255. x = Screen.width - (cropSize/2);
  256. }
  257. if (0 > y - (cropSize/2))
  258. {
  259. y = (cropSize/2);
  260. }
  261. if (Screen.height < y + (cropSize/2))
  262. {
  263. y = Screen.height - (cropSize/2);
  264. }
  265. if(
  266. 0 > x - (cropSize/2)
  267. || 0 > y - (cropSize/2)
  268. || Screen.width < x + (cropSize/2)
  269. || Screen.height < y + (cropSize/2)
  270. )
  271. {
  272. Debug.Log("unable to return image");
  273. return tex2; //this texture will be all black
  274. }
  275. Debug.Log(tex.GetPixels(x - (cropSize/2), y - (cropSize/2), cropSize, cropSize).Length);
  276. Color[] pixels = tex.GetPixels(x - (cropSize/2), y - (cropSize/2), cropSize, cropSize);
  277. tex2.SetPixels(0, 0, cropSize, cropSize, pixels, 0);
  278. tex2.Apply();
  279. return tex2;
  280. }
  281. /// <summary>
  282. /// Crops the and save.
  283. /// </summary>
  284. /// <param name="x">The x coordinate.</param>
  285. /// <param name="y">The y coordinate.</param>
  286. /// <param name="cropSize">Crop size.</param>
  287. /// <param name="ScreenShotNum">ScreenShot number.</param>
  288. public static void CropAndSave(int x, int y,int cropSize,int ScreenShotNum = 0)
  289. {
  290. Texture2D tex = Crop(GetScreenShot(GetFullPath(ScreenShotNum)),x,y,cropSize);
  291. byte[] PNG = ConvertToPNG(tex);
  292. Save(PNG, ScreenShotNum);
  293. }
  294. /// <summary>
  295. /// Crops the and save.
  296. /// </summary>
  297. /// <param name="WorldPosition">World position.</param>
  298. /// <param name="cropSize">Crop size.</param>
  299. /// <param name="ScreenShotNum">ScreenShot number.</param>
  300. public static void CropAndSave(Vector3 WorldPosition,int cropSize,int ScreenShotNum = 0)
  301. {
  302. Vector3 V3 = Camera.main.WorldToScreenPoint(WorldPosition);
  303. CropAndSave((int) V3.x, (int) V3.y,cropSize,ScreenShotNum);
  304. }
  305. /// <summary>
  306. /// Save the specified ScreenShotNum.
  307. /// </summary>
  308. /// <param name="ScreenShotNum">ScreenShot number.</param>
  309. public static void Save(int ScreenShotNum = 0)
  310. {
  311. Texture2D tex = GetScreenShot(GetFullPath(ScreenShotNum));
  312. byte[] PNG = ConvertToPNG(tex);
  313. Save(PNG, ScreenShotNum);
  314. }
  315. /// <summary>
  316. /// Save the specified PNG.
  317. /// </summary>
  318. /// <param name="PNG">PNG</param>
  319. /// <param name="ScreenShotNum">ScreenShot number.</param>
  320. public static void Save(byte[] PNG, int ScreenShotNum = 0)
  321. {
  322. string FileName = GetFullPath(ScreenShotNum);
  323. try
  324. {
  325. // Open file for reading
  326. System.IO.FileStream _FileStream =
  327. new System.IO.FileStream(FileName, System.IO.FileMode.Create,
  328. System.IO.FileAccess.Write);
  329. // Writes a block of bytes to this stream using data from
  330. // a byte array.
  331. _FileStream.Write(PNG, 0, PNG.Length);
  332. // close file stream
  333. _FileStream.Close();
  334. }
  335. catch (Exception _Exception)
  336. {
  337. Debug.LogError("Unknown Error");
  338. }
  339. }
  340. /// <summary>
  341. /// Converts to PNG/byte[]
  342. /// </summary>
  343. /// <returns>byte[]</returns>
  344. /// <param name="tex">Tex.</param>
  345. public static byte[] ConvertToPNG(Texture2D tex)
  346. {
  347. return tex.EncodeToPNG();
  348. }
  349. /// <summary>
  350. /// Deletes the all ScreenShots.
  351. /// </summary>
  352. public static void DeleteScreenShots()
  353. {
  354. var info = new DirectoryInfo(_ScreenShotPath);
  355. var fileInfo = info.GetFiles();
  356. foreach (FileInfo fi in fileInfo)
  357. {
  358. fi.Delete();
  359. }
  360. }
  361. }