Unity Android端截图保存并获取展示

截屏保存方法

    public static IEnumerator ScreenShot(string filePath, string fileName)
    {
        yield return new WaitForEndOfFrame();
        Rect rect = new Rect(0, 0, Screen.width, Screen.height);
        Texture2D screenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        screenShot.ReadPixels(rect, 0, 0);
        screenShot.Apply();
        byte[] bytes = screenShot.EncodeToPNG();
        if (!Directory.Exists(filePath))
            Directory.CreateDirectory(filePath);
        File.WriteAllBytes(filePath + "/" + fileName + ".png", bytes);
#if UNITY_EDITOR
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
#endif
    }

以日期为名称调用

    public void TakeAScreenShot() 
    {
        string englishTime = DateTime.Now.ToString(); //2000/9/27 1:27:28
        string chineseTime = string.Empty;
        string[] split1 = englishTime.Split('/'); //2000 - 9 - 27 1:27:28
        string[] split2 = split1[2].Split(' '); //27 - 1:27:28
        string[] split3 = split2[1].Split(':'); //1 - 27 - 28
        chineseTime += split1[0] + "年" + split1[1] + "月" + split2[0] + "日" + split3[0] + "时" + split3[1] + "分" + split3[2] + "秒";    
        StartCoroutine(UnityHelper.ScreenShot(UIPanel_ScreenShot.M_Path, chineseTime, false));
    }

获取截图

    private List<Texture2D> m_ScreenShotTextures;
    
    private void GetFilesInFolder(string folderPath)
    {
        if (Directory.Exists(folderPath))
        {
            string[] currentFiles = Directory.GetFiles(folderPath);
            if (currentFiles.Length > 0)
            {
                m_ScreenShotTextures = new List<Texture2D>();
                foreach (string filePath in currentFiles)
                {
#if UNITY_EDITOR
                    if (filePath.EndsWith("png"))
#endif
                        m_ScreenShotTextures.Add(ReadTexture(filePath));
                }
                m_ScreenShotTextures.Reverse();
            }
        }
        SetScreenShot();
    }

    private Texture2D ReadTexture(string filePath)
    {
        Texture2D pathTex = new Texture2D(Screen.width, Screen.height);
#if UNITY_EDITOR
        string[] split1 = filePath.Split("\\"); //...... - ***.png
#elif UNITY_ANDROID
        string[] split1 = filePath.Split("/"); //...... - ***.png
#endif
        string[] split2 = split1[split1.Length - 1].Split("."); // *** - png
        pathTex.name = split2[0];
        byte[] bytes = File.ReadAllBytes(filePath);
        pathTex.LoadImage(bytes);
        pathTex.Apply();
        return pathTex;
    }

展示:把上面获取的结果进行展示即可

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值