untiy3D 通过外部Json文件来控制场景截图

{
"SceneName":"PrintScreen",
"Position":{"x":0,"y":10,"z":20},
"Rotation":{"x":0,"y":0,"z":0},
"ScreenWidth":1024,
"ScreenHeight":768,
"ImageName":"Test01.png",
"OutPath":"C://OutPath"
}

上面是Json文件的数据,通过上面的数据,我们来控制截取某个场景的局部图片,上面的参数分别为:

SceneName:场景名称

Position:摄像机位置

Rotation :摄像机朝向

ScreenWidth:图片的宽

ScreenHight:图片的高

ImageName:图片的名称

OutPath:图片保存的位置

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;

[Serializable]
public class ListRot
{
    public float x;
    public float y;
    public float z;
}

public class JsonManage
{
    public string SceneName;
    public ListRot Position;
    public ListRot Rotation;
    public int ScreenWidth;
    public int ScreenHeight;
    public string ImageName;
    public string OutPath;

}

public class PrintScreen : MonoBehaviour
{
    private Camera camera;
    private Rect rect;
    JsonManage jm;

    AsyncOperation async;
    int num ;

    void Start()
    {
        num = 0;

        camera = Camera.main;
        //读取json信息
        jm = JsonUtility.FromJson<JsonManage>(File.ReadAllText("C:\\PrintScreen.json"));

        //参数赋值
        camera.transform.position = new Vector3(jm.Position.x, jm.Position.y, jm.Position.z);
        camera.transform.eulerAngles = new Vector3(jm.Rotation.x, jm.Rotation.y, jm.Rotation.z);
        rect.width = jm.ScreenWidth;
        rect.height = jm.ScreenHeight;
        

        //跳转场景后保留Camera对象
        DontDestroyOnLoad(this);
        async = SceneManager.LoadSceneAsync(jm.SceneName);
        
    }
    private void Update()
    {
       //判断场景跳转成功,可截图一次
        if (async.isDone && num == 0)
        {
            num++;
            Capture3();
        }
    }

    //private void OnGUI()
    //{
    //    if(GUI.Button(new Rect(10, 10, 100, 50), "截图"))
    //    {
    //        print("开始截图");
    //        Capture3();
    //    }
    //}

    public Texture2D Capture3()
    {
        // 创建一个RenderTexture对象    
        RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
        // 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机    
        camera.targetTexture = rt;
        camera.Render();
        //ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。    
        //ps: camera2.targetTexture = rt;    
        //ps: camera2.Render();    
        //ps: -------------------------------------------------------------------    

        // 激活这个rt, 并从中中读取像素。    
        RenderTexture.active = rt;
        Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(rect, 0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素    
        screenShot.Apply();

        // 重置相关参数,以使用camera继续在屏幕上显示    
        camera.targetTexture = null;
        //ps: camera2.targetTexture = null;    
        RenderTexture.active = null; // JC: added to avoid errors    
        GameObject.Destroy(rt);

        // 最后将这些纹理数据,成一个png图片文件    
        byte[] bytes = screenShot.EncodeToPNG();

        //文件夹不存在时自动创建文件夹
        if (!Directory.Exists(jm.OutPath))
        {
            Directory.CreateDirectory(jm.OutPath);
        }
        //string filename = Application.dataPath + "/Screenshot3.png";

        string filename = jm.OutPath + @"/" + jm.ImageName +".png";
        System.IO.File.WriteAllBytes(filename, bytes);
        //AssetDatabase.Refresh();
        Debug.Log(string.Format("截屏了一张照片: {0}", filename));

        return screenShot;
    }
}  

操作方法:

1、写好Json文件放在C盘下(本案例的Json文件是放在"C:\\PrintScreen.json")

2、创建空场景将此脚本挂在MainCamera上

3、再创建一个需要截图的场景,场景名需要跟Json文件的第一个参数一致(运行时需保证所有场景均加入了BuildSettings中)

4、运行即可得到一张截图,保存在指定位置(本案例的截图保存在C:\OutPath)

此案例可以控制任意场景的截图,只需要外部在Json文件中修改即可,capture3()截图代码来自网络,在此感谢那位兄弟的分享。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章