简体   繁体   English

如何在 Android 上的 Unity 中禁用用于屏幕截图的游戏对象?

[英]How to disable Gameobjects for Screenshot capturing in Unity on Android?

I have a simple goal for my Unity application running on Android: create a function, which disables my GUI, creates a screenshot, stores the image and enables the GUI again.对于在 Android 上运行的 Unity 应用程序,我有一个简单的目标:创建一个函数,该函数禁用我的 GUI、创建屏幕截图、存储图像并再次启用 GUI。

This is my current code:这是我当前的代码:

The whole UI is stored in a GameObject which my handler class holds as property:整个 UI 存储在我的处理程序类作为属性保存的 GameObject 中:

private bool makeScreenshotFlag;
public GameObject UserInterface;

makeScreenshotFlag above is used in the Update function:上面的makeScreenshotFlag是在 Update 函数中使用的:

// Update is called once per frame
void Update()
{
    if (makeScreenshotFlag)
    {
        // Working with this flag is necessary, 
        // since we need to wait for the next frame, 
        // where the gui is not visible;
        MakeScreenShot();
    }
}

If the corresponding button gets pressed I fire the following Method:如果相应的按钮被按下,我会触发以下方法:

public void OnCreateScreenshotButtonPressed()
{
    UserInterface.SetActive(false);
    makeScreenshotFlag = true;
}

And at last the method for the screenshot itself:最后是截图本身的方法:

private void MakeScreenShot()
{
    if (UserInterface.activeSelf) return; // wait for the next frame if the gui is still active

    try
    {
        string filename = "screenshot_" + DateTime.Now.ToString("HH-mm-ss--dd-MM-yyyy") + ".png";
        ScreenCapture.CaptureScreenshot(filename);
    }
    catch (Exception e)
    {
        DebugHelper.NewLog = "Error capturing screenshot: " + e.Message;
    }
    finally
    {
        makeScreenshotFlag = false;
        UserInterface.SetActive(true);
    }
}

In theory this should work, however the documentation for ScreenCapture.CaptureScreenshot() states the following:理论上这应该有效,但是ScreenCapture.CaptureScreenshot()的文档说明如下:

The CaptureScreenshot returns immediately on Android. CaptureScreenshot 在 Android 上立即返回。 The screen capture continues in the background.屏幕截图在后台继续。 The resulting screen shot is saved in the file system after a few seconds.几秒钟后,生成的屏幕截图将保存在文件系统中。

In practice this means, when my screenshot is made, the GUI is already enabled again.实际上,这意味着,当我制作屏幕截图时,GUI 已经再次启用。 So far I couldn't find any event or similar to get notified as soon as the screenshot is made.到目前为止,我找不到任何事件或类似事件,以便在截取屏幕截图后立即得到通知。 The only thing I can think of is to store the screenshot filename and check if the file exists each frame after the screenshot is started.我唯一能想到的就是存储截图文件名并在截图开始后检查文件是否存在每一帧。

Is there any other, more practical way?有没有其他更实用的方法?

Most probably, you are capturing the frame with UI itself.最有可能的是,您正在使用 UI 本身捕获帧。 It's better to wait till the end of the frame using Coroutine.最好使用 Coroutine 等到帧结束。

public IEnumerator MakeScreenShot()
{
    // Wait till the last possible moment before screen rendering to hide the UI
    yield return null;

    // Disable UI       
    UserInterface.SetActive(false);

    // Wait for screen rendering to complete
    yield return new WaitForEndOfFrame();

    // Take screenshot
    Application.CaptureScreenshot("screenshot.png");

    // Show UI after we're done
    UserInterface.SetActive(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM