Unity截全屏

U3d中的Application.CaptureScreenshot方法,是一截全屏并保存在一张图片的方法
1、它是异步
如果你一定要在,程序中使用Application.CaptureScreenshot,且还要使用它截下的图片时,要注意:图片是在Application.CaptureScreenshot调用完成后一帧或更多久,才要被保存下来的,所以你要在Application.CaptureScreenshot调用完成后的下一要判断图片已经被保存好,才能去使用它。不然你会发现找不到图片的。

2、它保存的路径问题
在pc上可以给它传一个在Application.persistentDataPath下的全路径,
但在android和ios中只能给它传一个在Application.persistentDataPath下的相关路径

3.实现方法示例

   #region 截屏
    /// <summary>
    /// 截取全屏图
    /// </summary>
    /// <param name="Path_save">图片保存路径</param>
    /// <param name="func"></param>
    public static void CapruerScreen(string Path_save, float waitTime,LuaFunction func)
    {
        if (Application.platform == RuntimePlatform.Android)
        {
            Main.Instance.StartCoroutine(CaptureScreenshot(Path_save, waitTime, func));
        }
        else if(Application.platform == RuntimePlatform.IPhonePlayer)//IOS平台
        {
            Main.Instance.StartCoroutine(Screenshot(new Rect(0, 0, Screen.width, Screen.height), Path_save,func));
        }
    }

    /// <summary>
    /// 简单截屏的方法
    /// </summary>
    /// <param name="path">保存路径</param>
    /// <param name="func">回掉</param>
    /// <returns></returns>
    public static IEnumerator CaptureScreenshot(string path,float waitTime, LuaFunction func = null)
    {
        // 路径处理
        // 注:path是一个在Application.persistentDataPath全路径
        // Application.persistentDataPath这个方法的参数:
        //      在pc呆传全路径
        //      在android和ios上可能传Application.persistentDataPath下的相关路径
        string newPath = path;
        //非编辑器只取相对于沙盒的相对路径
        if (Application.isMobilePlatform)
        {
            newPath = path.Replace(Application.persistentDataPath + "/", "");
        }
        Debuger.Log("newPath--->" + newPath);
        Application.CaptureScreenshot(newPath);
        float time = Time.time;
        bool b = false;
        yield return new WaitUntil(() =>
        {
            b = System.IO.File.Exists(path);
            return b || ((Time.time - time) > waitTime);
        });
        string str = path;
        if (b == false)
        {
            str = "-1";
        }
        if (func != null)
        {
            func.Call(str);
        }
    }

    private static IEnumerator Screenshot(Rect rect, string path,LuaFunction func=null)
    {
        //等待渲染线程结束
        yield return new WaitForEndOfFrame();
        //初始化Texture2D
        Texture2D mTexture = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGBA32, false);
        //读取屏幕像素信息并存储为纹理数据
        mTexture.ReadPixels(rect, 0, 0);
        //应用
        mTexture.Apply();

        //将图片信息编码为字节信息
        byte[] bytes = mTexture.EncodeToPNG();
        //保存
        System.IO.File.WriteAllBytes(path, bytes);
        if (func != null&&File.Exists(path))
        {
            Debuger.Log("C#文件存在");
            func.Call(path);
        }
        //如果需要可以返回截图
        //return mTexture;
    }
    #endregion
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity 中,在通过 WebGL 发布游戏时实现全屏功能非常简单。Unity 提供了一种简单的方式来实现通过代码控制游戏全屏显示的功能。 首先,在你的代码中,你需要使用 Screen 类来获取屏幕相关的设置。为了将游戏设置为全屏显示,你可以使用 Screen.SetResolution() 方法。这个方法接受三个参数,宽度、高度和一个布尔值,确定是否将游戏设置为全屏显示。将布尔值设置为 true,游戏将以全屏模式展示。 例如,你可以创建一个按钮,当用户点击按钮时,游戏将设置为全屏显示。在按钮的点击事件处理程序中,你可以使用以下代码: ``` using UnityEngine; public class FullScreenButton : MonoBehaviour { public void SetFullScreen() { Screen.SetResolution(Screen.width, Screen.height, true); } } ``` 这个脚本将在用户点击按钮时调用 SetFullScreen() 方法,将游戏设置为全屏显示。 另外,需要注意的是,在使用 WebGL 发布游戏时,浏览器可能会阻止网页自动进入全屏模式。为了解决这个问题,你可以使用 FullScreen API,这是一种浏览器内置的 JavaScript API,用于请求将网页显示为全屏模式。你可以在 Unity 的 JavaScript 中编写一个脚本来调用 FullScreen API,以确保你的游戏可以在 WebGL 所在的浏览器中正常进入全屏模式。 通过这种方式,你就可以在 Unity 的 WebGL 游戏中实现全屏显示的功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值