
Android
csharpusing UnityEngine;using System.Collections;using System.IO;public class SaveScreenshot : MonoBehaviour{ public string screenshotPath = "screenshot.png"; private Texture2D screenshotTexture;
void Start() { // 获取当前屏幕截图 screenshotTexture = new Texture2D(Screen.GetScreenWidth(), Screen.GetScreenHeight()); Graphics.Blit(Screen.currentScreen, screenshotTexture); // 将截图保存到文件 string filePath = Application.dataPath + "/screenshot.png"; FileStream file = new FileStream(filePath, FileMode.Create); Texture2D.SaveAs(file, screenshotTexture); file.Close(); Debug.Log("Screenshot saved successfully."); }}
在上述代码中,首先获取当前屏幕的宽度和高度,并使用Graphics.Blit方法将整个屏幕截图到一个Texture2D对象中。然后,创建一个文件流来写入屏幕截图,并调用Texture2D.SaveAs方法将图片保存到指定路径。最后,在代码中添加一个Log来确认是否成功保存了截图。请注意,这段代码中的screenshotPath变量需要根据实际情况进行修改,以便将截图保存到正确的位置。通过将这段代码与您的Unity3D项目集成,您就可以在每个关卡或场景中自动保存当前屏幕截图,并将其保存到Android手机的图片库中。这样,在需要时就可以方便地查看和分享您的游戏截图了。Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号