您的位置:首页 > 汽车 > 新车 > Unity Android端截图保存并获取展示

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

2024/11/16 15:35:37 来源:https://blog.csdn.net/derbi123123/article/details/141065442  浏览:    关键词: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_EDITORAssetDatabase.SaveAssets();AssetDatabase.Refresh();
#endif}

以日期为名称调用

    public void TakeAScreenShot() {string englishTime = DateTime.Now.ToString(); //2000/9/27 1:27:28string chineseTime = string.Empty;string[] split1 = englishTime.Split('/'); //2000 - 9 - 27 1:27:28string[] split2 = split1[2].Split(' '); //27 - 1:27:28string[] split3 = split2[1].Split(':'); //1 - 27 - 28chineseTime += 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_EDITORif (filePath.EndsWith("png"))
#endifm_ScreenShotTextures.Add(ReadTexture(filePath));}m_ScreenShotTextures.Reverse();}}SetScreenShot();}private Texture2D ReadTexture(string filePath){Texture2D pathTex = new Texture2D(Screen.width, Screen.height);
#if UNITY_EDITORstring[] split1 = filePath.Split("\\"); //...... - ***.png
#elif UNITY_ANDROIDstring[] split1 = filePath.Split("/"); //...... - ***.png
#endifstring[] split2 = split1[split1.Length - 1].Split("."); // *** - pngpathTex.name = split2[0];byte[] bytes = File.ReadAllBytes(filePath);pathTex.LoadImage(bytes);pathTex.Apply();return pathTex;}

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com