Unity 截图

class CaptureScreen : Editor
    {
        [MenuItem("Tool/CaptureScreen")]
        public static void DoAction()
        {
            Camera camera = Camera.main;
            camera.clearFlags = CameraClearFlags.Depth;

            //--------------截图的精细度跟这个尺寸有关系,越大,细节越多,另外一些看不见的物件,可能是lod的问题--------------
            int width = 1960*6;
            int height = 1960*6;
            //--------------------------------
            string path = Application.persistentDataPath + "/capture/screen_"+width+"_"+height+".png";

            RenderTexture rt = new RenderTexture(width, height, 0);
            camera.targetTexture = rt;
            camera.Render();

            RenderTexture.active = rt;
            Texture2D texture2D = new Texture2D(width, height, TextureFormat.RGB24, false);
            texture2D.ReadPixels(new Rect(0, 0, width, height), 0, 0, false);
            texture2D.Apply();

            byte[] data = texture2D.EncodeToPNG();

            string directoryPath = Path.GetDirectoryName(path);
            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            File.WriteAllBytes(path, data);

            camera.targetTexture = null;
            RenderTexture.active = null;
            GameObject.DestroyImmediate(rt);
            data = null;

            Debug.Log("capture save to " + path);

        }
    }
原文地址:https://www.cnblogs.com/jingxuan2583/p/14148164.html