获取 Image 组件上的Texture纹理,保存成图片到本地

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

public class SavePng : MonoBehaviour
{
    [Tooltip("截图存储路径")]
    public string path;
    public bool capture;


    public RawImage rawImage;
    Texture2D mtexture;

    //保存图像信息,时间戳+像素RGB
    private struct ImageData
    {
        public long timestamp;
        public byte[] data;
    }
    // Start is called before the first frame update
    void Start()
    {
        path = Application.streamingAssetsPath+"/Pictures/";
    }

    // Update is called once per frame
    void Update()
    {        
        if (Input.GetKeyDown(KeyCode.S))
            InputSave();
    }
    //按钮回调
    public void InputSave()
    {
        if (!capture)
        {
            capture = true;
            StartCoroutine(CaptureAndSave02());
            Debug.Log("Successd!");
        }
    }

    public IEnumerator CaptureAndSave02()
    {
        if (capture)
        {
            //等待屏幕渲染结束后获取屏幕像素信息
            yield return new WaitForEndOfFrame();
            mtexture = TextureToTexture2D(rawImage.texture);![在这里插入图片描述](https://img-blog.csdnimg.cn/20210319150638808.gif)

            mtexture.Apply();
            //将图片信息编码为字节信息 
            ImageData pack;
            pack.data = mtexture.EncodeToPNG();
            //保存图片到 你的路径
            pack.timestamp = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            FileStream file = File.Create(path + pack.timestamp + ".png");
            file.Write(pack.data, 0, pack.data.Length);
            file.Close();
            capture = false;
        }
    }

    /// <summary>
    /// 运行模式下Texture转换成Texture2D
    /// </summary>
    /// <param name="texture"></param>
    /// <returns></returns>
    private Texture2D TextureToTexture2D(Texture texture)
    {
        Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
        RenderTexture currentRT = RenderTexture.active;
        RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
        Graphics.Blit(texture, renderTexture);

        RenderTexture.active = renderTexture;
        texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
        texture2D.Apply();

        RenderTexture.active = currentRT;
        RenderTexture.ReleaseTemporary(renderTexture);

        return texture2D;
    }
}

在这里插入图片描述

2021年3月28日使用一次

ImageData pack;
            pack.data = mtexture.EncodeToPNG();
            //保存图片到 你的路径
            pack.timestamp = System.DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
            FileStream file = File.Create(path + pack.timestamp + ".png");
            file.Write(pack.data, 0, pack.data.Length);
            file.Close();

其它:

//从本地读取文件返回byte
  byte[] ReadImg(string fileName)
    {
        FileInfo fileInfo = new FileInfo(Application.dataPath + "/" + fileName);
        byte[] buffer = new byte[fileInfo.Length];
        using (FileStream fs = fileInfo.OpenRead())
        {
            fs.Read(buffer, 0, buffer.Length);
        }

        return buffer;
    }
  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
是的,您需要将VideoPlayer的“Texture”属性设置为RawImage组件纹理属性,这样可以将视频的帧数据渲染到RawImage组件上。具体实现步骤如下: 1. 在Unity中创建一个空的GameObject,并将VideoPlayer组件添加到该GameObject上。 2. 将视频文件拖拽到VideoPlayer组件的“Video Clip”属性中。 3. 在UI中创建一个RawImage组件,并将该组件纹理属性设置为VideoPlayer的“Texture”属性。 4. 在代码中获取VideoPlayer组件,并开始播放视频。播放视频的方法可以是通过调用VideoPlayer的Play方法,也可以是通过设置VideoPlayer的控制参数,例如“Play on Awake”属性等。 5. 在Update方法中,将VideoPlayer的“Texture”属性赋值给RawImage组件纹理属性,这样可以将视频的帧数据渲染到RawImage组件上。 代码示例: ```C# using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; public class VideoController : MonoBehaviour { public RawImage rawImage; public VideoPlayer videoPlayer; void Start() { videoPlayer.Play(); // 开始播放视频 } void Update() { rawImage.texture = videoPlayer.texture; // 将视频的帧数据渲染到RawImage组件上 } } ``` 需要注意的是,为了保证视频的流畅播放,建议将视频的分辨率和RawImage的尺寸匹配,否则会出现视频拉伸或压缩的情况。同时,视频文件的大小也可能较大,需要考虑设备性能等因素来保证视频的流畅播放。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值