【问题标题】:Unity 2D Took a screen capture but it show the previous screen capture instead of the current screen captureUnity 2D 进行了屏幕截图,但它显示了上一个屏幕截图而不是当前屏幕截图
【发布时间】:2020-07-29 21:35:46
【问题描述】:

我是 Unity 新手,我现在面临一个问题,即显示上一个屏幕截图而不是当前屏幕截图。

我的代码: 截图

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TakeScreenshot : MonoBehaviour
{

    IEnumerator CaptureIt()
    {
        ScreenCapture.CaptureScreenshot("*.png", 0);
        yield return new WaitForEndOfFrame();
        yield return new WaitForSecondsRealtime(1.5f);
    }

    public void TakeAShot()
    {
        StartCoroutine(CaptureIt());
    }
}

截图展示

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

public class ScreenshotPreview : MonoBehaviour
{

    [SerializeField]
    GameObject canvas;

    // Use this for initialization
    void Start()
    {
        string url = Application.persistentDataPath + "/" + "*.png";
        var bytes = File.ReadAllBytes(url);
        Texture2D texture = new Texture2D(2, 2);
        bool imageLoadSuccess = texture.LoadImage(bytes);
        while (!imageLoadSuccess)
        {
            print("image load failed");
            bytes = File.ReadAllBytes(url);
            imageLoadSuccess = texture.LoadImage(bytes);
        }
        print("Image load success: " + imageLoadSuccess);
        canvas.GetComponent<Image>().overrideSprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0f, 0f), 100f);
    }
}

有没有人可以帮助我解决这个问题?

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    您正在Start() 加载屏幕截图。所以它在游戏开始时加载。我猜你在游戏后期会做新的截图。因此,要在画布中更新您的屏幕截图,您需要在拍摄新屏幕截图后加载它。将代码从 ScreenshotPreviewStart 移动到其他一些公共方法。并在截图后调用此方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多