G_Capture.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using UnityEngine;
  2. using System.Text;
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. namespace G_Engine {
  7. public class G_Capture : MonoBehaviour {
  8. public static IEnumerator ScreenShoot(Rect rect)
  9. {
  10. //Wait for graphics to render
  11. yield return new WaitForEndOfFrame();
  12. //Create a texture to pass to encoding
  13. Texture2D texture = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
  14. //Put buffer into texture
  15. texture.ReadPixels(rect, 0, 0);
  16. //Split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
  17. yield return 0;
  18. byte[] bytes = texture.EncodeToPNG();
  19. string imagePath = "";
  20. switch (Application.platform)
  21. {
  22. case RuntimePlatform.WindowsEditor:
  23. imagePath = Application.dataPath + "/TAQRCode.png";
  24. break;
  25. case RuntimePlatform.Android:
  26. imagePath = "/sdcard" + "/TAQRCode.png";
  27. break;
  28. case RuntimePlatform.IPhonePlayer:
  29. imagePath = Application.dataPath + "/TAQRCode.png";
  30. break;
  31. }
  32. //Save our test image (could also upload to WWW)
  33. File.WriteAllBytes(imagePath, bytes);
  34. //Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
  35. DestroyObject(texture);
  36. MSMgr.Instance().StartShareSDK(MSMgr.ShareType.TravelAlbum, imagePath);
  37. }
  38. private IEnumerator ScreenShoot(string filePath)
  39. {
  40. //Wait for graphics to render
  41. yield return new WaitForEndOfFrame();
  42. RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 24);
  43. Texture2D screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
  44. //Render from all!
  45. foreach (Camera cam in Camera.allCameras)
  46. {
  47. cam.targetTexture = rt;
  48. cam.Render();
  49. cam.targetTexture = null;
  50. }
  51. RenderTexture.active = rt;
  52. screenShot.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
  53. Camera.main.targetTexture = null;
  54. //Added to avoid errors
  55. RenderTexture.active = null;
  56. Destroy(rt);
  57. //Split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
  58. yield return 0;
  59. byte[] bytes = screenShot.EncodeToPNG();
  60. File.WriteAllBytes(filePath, bytes);
  61. }
  62. /// <summary>
  63. /// Captures the screenshot2.
  64. /// </summary>
  65. /// <returns>The screenshot2.</returns>
  66. /// <param name="rect">Rect.截图的区域,左下角为o点</param>
  67. public static Texture2D CaptureScreenshot(Rect rect)
  68. {
  69. // 先创建一个的空纹理,大小可根据实现需要来设置
  70. Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
  71. // 读取屏幕像素信息并存储为纹理数据,
  72. screenShot.ReadPixels(rect, 0, 0);
  73. screenShot.Apply();
  74. // 然后将这些纹理数据,成一个png图片文件
  75. byte[] bytes = screenShot.EncodeToPNG();
  76. string filename = Application.dataPath + "/TAQRCode.png";
  77. System.IO.File.WriteAllBytes(filename, bytes);
  78. Debug.Log(string.Format("截屏了一张图片: {0}", filename));
  79. // 最后,我返回这个Texture2d对象,这样我们直接,所这个截图图示在游戏中,当然这个根据自己的需求的。
  80. return screenShot;
  81. }
  82. /// <summary>
  83. /// 对相机截图。
  84. /// </summary>
  85. /// <returns>The screenshot2.</returns>
  86. /// <param name="camera">Camera.要被截屏的相机</param>
  87. /// <param name="rect">Rect.截屏的区域</param>
  88. public static Texture2D CaptureCamera(Camera camera, Rect rect)
  89. {
  90. // 创建一个RenderTexture对象
  91. RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
  92. // 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
  93. camera.targetTexture = rt;
  94. camera.Render();
  95. //ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。
  96. //ps: camera2.targetTexture = rt;
  97. //ps: camera2.Render();
  98. //ps: -------------------------------------------------------------------
  99. // 激活这个rt, 并从中中读取像素。
  100. RenderTexture.active = rt;
  101. Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
  102. screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素
  103. screenShot.Apply();
  104. // 重置相关参数,以使用camera继续在屏幕上显示
  105. camera.targetTexture = null;
  106. //ps: camera2.targetTexture = null;
  107. RenderTexture.active = null; // JC: added to avoid errors
  108. GameObject.Destroy(rt);
  109. // 最后将这些纹理数据,成一个png图片文件
  110. byte[] bytes = screenShot.EncodeToPNG();
  111. string filename = Application.dataPath + "/Screenshot.png";
  112. System.IO.File.WriteAllBytes(filename, bytes);
  113. Debug.Log(string.Format("截屏了一张照片: {0}", filename));
  114. return screenShot;
  115. }
  116. }
  117. }