1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| using System.IO; using UnityEditor; using UnityEngine; using UnityEngine.SceneManagement;
public class CameraShot : MonoBehaviour {
private Camera m_camera; void Start() { m_camera = GetComponent<Camera>(); m_camera.orthographic = true; m_camera.orthographicSize = 200; m_camera.transform.rotation = Quaternion.Euler(90, 0, 0); m_camera.transform.position = new Vector3(0, 100, 0); }
void Update() { if (Input.GetKeyDown("s")) { string path = string.Format("{0}/ScreenShot/min_{1}.jpg", Application.streamingAssetsPath, SceneManager.GetActiveScene().name); Screenshot(path); } }
void Screenshot(string filePath) { RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 16); m_camera.targetTexture = rt; m_camera.Render(); RenderTexture.active = rt; Texture2D t = new Texture2D(Screen.width, Screen.height);
t.ReadPixels(new Rect(0, 0, t.width, t.height), 0, 0); t.Apply();
byte[] mbyte = new byte[t.EncodeToJPG().Length]; mbyte = t.EncodeToJPG(); Debug.Log(mbyte.Length);
File.WriteAllBytes(filePath, t.EncodeToJPG());
UnityEditor.AssetDatabase.Refresh();
m_camera.targetTexture = null; RenderTexture.active = null; Destroy(rt); }
[MenuItem("XEditor/Scene/CreateScreenShotCamera")] public static void CreateScreenShotCamera() { GameObject go = new GameObject("Camera Screen Shot"); go.AddComponent<Camera>(); go.AddComponent<CameraShot>(); } }
|