Unity截图保存到本地,安卓手机

1、简单粗暴 ,直接上代码。

using UnityEngine;
using System.Collections;
using System.IO;

public class SavedScreen : MonoBehaviour
{

    string path = "";
    void Start()
    {
        // 开启一个协程
        StartCoroutine(UploadPNG());
        print(Application.persistentDataPath);
        path = "D:/";
    }


    // 定义一个协程
    IEnumerator UploadPNG()
    {


        // 因为"WaitForEndOfFrame"在OnGUI之后执行
        // 所以我们只在渲染完成之后才读取屏幕上的画面
        yield return new WaitForEndOfFrame();



        int width = Screen.width;
        int height = Screen.height;
        // 创建一个屏幕大小的纹理,RGB24 位格(24位格没有透明通道,32位的有)
        Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

        // 读取屏幕内容到我们自定义的纹理图片中
        tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
        // 保存前面对纹理的修改
        tex.Apply();

        // 编码纹理为PNG格式
        byte[] bytes = tex.EncodeToPNG();
        // 销毁没用的图片纹理
        Destroy(tex);

        // 将字节保存成图片,这个路径只能在PC端对图片进行读写操作
        File.WriteAllBytes(Application.dataPath + "/onPcSavedScreen.png", bytes);
        // 这个路径会将图片保存到手机的沙盒中,这样就可以在手机上对其进行读写操作了
        File.WriteAllBytes(Application.persistentDataPath + "/onMobileSavedScreen.png", bytes);
        //保存到D盘
        File.WriteAllBytes(path+ "/onMeKey.png", bytes);
    }
}

2、放上效果图

保存到D盘的

在这里插入图片描述

保存到Application.dataPath
在这里插入图片描述

保存到Application.persistentDataPath 在这里插入图片描述

哈哈 结束 自己测试吧!!!!!!!!
这个方法我没有在手机上测试,之后我改了路径测试了一下 在手机上是找不到的,所以我就查文档,寻找到了另一种方法,可以保存到安卓手机相册,看我下一篇文档······

猜你喜欢

转载自blog.csdn.net/weixin_43109909/article/details/82878656