unity截图保存在手机

25 篇文章 10 订阅
24 篇文章 1 订阅

直接上代码

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



public class ScreenShot : MonoBehaviour
{
    //这个相机是用来截屏的,相机的Claer Flags 的属性选择为Depth Only 
    public Camera CameraTrans;
    private Texture2D mTexture1;
    private string mPath;
    public GameObject picture;
    private string mingming;


    // Use this for initialization
    void Start()
    {
        CameraTrans = GameObject.FindGameObjectWithTag("ARCamera").GetComponent<Camera>();ji
    }

    // Update is called once per frame
    void Update()
    {

    }
    /// <summary>
    /// 开启相机
    /// </summary>

    public void screenshot()
    {
        //用时间命名
        string mingming = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day + System.DateTime.Now.Hour + System.DateTime.Now.Minute + System.DateTime.Now.Second;
        //平台判断
        if (Application.platform == RuntimePlatform.Android)
        {
         
            //安卓设备的相册路径
            mPath = "/sdcard/DCIM/Camera/" + mingming + ".jpg";
            Debug.Log(mPath);

        }
        else if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            mPath = Application.dataPath + "\\Resources\\" + mingming + ".jpg";
            Debug.Log(mPath);
        }
        StartCoroutine(CaptureByCamera(CameraTrans, new Rect(0, 0, 1024, 768), mPath));

    }
    private IEnumerator CaptureByCamera(Camera mCamera, Rect mRect, string mFileName)
    {
        //等待渲染线程结束  
        yield return new WaitForEndOfFrame();

        //初始化RenderTexture  
        RenderTexture mRender = new RenderTexture((int)mRect.width, (int)mRect.height, 0);
        //设置相机的渲染目标  
        mCamera.targetTexture = mRender;
        //开始渲染  
        mCamera.Render();

        //激活渲染贴图读取信息  
        RenderTexture.active = mRender;

        Texture2D mTexture = new Texture2D((int)mRect.width, (int)mRect.height, TextureFormat.RGB24, false);
        //读取屏幕像素信息并存储为纹理数据  
        mTexture.ReadPixels(mRect, 0, 0);
        //应用  
        mTexture.Apply();

        //释放相机,销毁渲染贴图  
        mCamera.targetTexture = null;
        RenderTexture.active = null;
        GameObject.Destroy(mRender);

        //将图片信息编码为字节信息  
        byte[] bytes = mTexture.EncodeToPNG();

        //保存  
        File.WriteAllBytes(mFileName, bytes);
        //将纹理图存在静态类里
        mTexture1 = mTexture;
      //  picture.GetComponent<Image>().sprite = Sprite.Create(mTexture1, new Rect(0, 0, 1024, 768), new Vector2(Screen.height / 2, Screen.width / 2));
        //如果需要可以返回截图  
        //return mTexture;  

    }


    // Start is called before the first frame update


    public void OnScreenShotClick()
    {
        //if (picture == null) return;
        Debug.Log("截图");
        screenshot();
       
      
    }
    public void ScreenShotBtn()
    {
        //StartCoroutine(CaptureByRect());
     
    }





  
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ToDoNothing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值