Untiy保存截图保存到本地文件夹、并下载截图到场景里

来源:互联网 发布:手机屏幕玻璃材质知乎 编辑:程序博客网 时间:2024/06/03 14:51

Unity有三种截图方法,之前很多论坛上也有这里就不再赘述了。
下面直接上代码教给小白一个简单保存截图而且保存到本地文件夹的方法。

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

public class AcceptScreenTexture : MonoBehaviour {

//此为图片保存的路径
private string mPath1=”D:\virtual mirror\PhoneImage”+PhoneNumber.ToString()+”.png”;
public RawImage PhoneImageMain;

void 保存图片()
{
StartCoroutine(getTexture2d());
}
void 调用图片()
{
StartCoroutine(ShowPhoneImage(i));
}
IEnumerator ShowPhoneImage(int i) //读取照片到主照片
{
WWW www = new WWW (“file://D:\virtual mirror\PhoneImage”+i.ToString()+”.png”);
yield return www;
PhoneImageMain.texture = www.texture;
Resources.UnloadUnusedAssets(); //释放内存
}

IEnumerator getTexture2d() //利用截取屏幕长款所在矩阵的画面转换为像素并由字节转换为PNG格式
{
yield return new WaitForEndOfFrame();
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0,false);
tex.Apply();
byte[] imagebytes = tex.EncodeToPNG();
tex.Compress(false);

    File.WriteAllBytes(mPath1,imagebytes);    Destroy(tex);   }

}

0 0