白黒羊

白黒羊

白黒羊

  • Home
  • Products
  • News
  • Support
  • Contact

SHIROKUROHITSUJI

UniTask.WaitForEndOfFrame + ScreenCaptureAsTexture でクラッシュする

2020年12月27日UniTask, Unity By: shimahinuko

ちゃんと UniTaskのReadmeに書いてありましたね。

await UniTask.WaitForEndOfFrame() is not equilavelnt to coroutine’s yield return new WaitForEndOfFrame(). Coroutine’s WaitForEndOfFrame seems to run after the PlayerLoop is done. Some methods that require coroutine’s end of frame(ScreenCapture.CaptureScreenshotAsTexture, CommandBuffer, etc) does not work correctly when replace to async/await. In that case, use a coroutine.

https://github.com/Cysharp/UniTask

コルーチンの yield return new WaitForEndOfFrame() は PlayerLoopが終わってから実行されるようで、ScreenCapture.CaptureScreenshotAsTexture のような完全にフレームが終了するのを待つ必要があるメソッドではそちらを使うべきとのことです。

クラッシュしたコード

using Cysharp.Threading.Tasks;
using UnityEngine;

public class CaptureScreen
{
    private Texture2D _texture;

    public async UniTask CaptureWholeScreenAsync()
    {
        await UniTask.WaitForEndOfFrame();
        _texture = ScreenCapture.CaptureScreenshotAsTexture();
        var bytes = _texture.EncodeToJPG();
    }
}

修正したコード

using System.Collections;
using Cysharp.Threading.Tasks;
using UniRx;
using UnityEngine;

public class CaptureScreen
{
    private Texture2D _texture;

    public async UniTask CaptureWholeScreenAsync()
    {
        await RecordFrame().ToObservable();
        var bytes = _texture.EncodeToJPG();
    }

    private IEnumerator RecordFrame()
    {
        yield return new WaitForEndOfFrame();
        _texture = ScreenCapture.CaptureScreenshotAsTexture();
    }
}

備考

Unity 2020.2の Release Note に ScreenCapture.CaptureScreenshotAsTexture でクラッシュするバグを直したよ〜という記述があるので、古いバージョンでは UniTaskとか関係なくクラッシュするのかもしれないです。

sassembla/EditorScreenshotScript – GitHub Gist

What’s new in Unity 2020.2.0 – Unity

参考

ScreenCaptureAsTexture UniTask Unity
Share:

Previous

Photoshopで「XXXXX.psdを保存できません。プログラムエラーです。」と表示されて保存できないときの回避策

Next

UnityのScroll View をモバイルで使うと中身が消えるときがある

Leave a Comment コメントをキャンセル

カテゴリー

  • CFD (9)
    • OpenFOAM (9)
  • Design (6)
    • Adobe_XD (2)
    • Font (3)
    • Photoshop (2)
  • Diary (4)
  • Event (9)
    • AdventCalendar (3)
    • unity1week (6)
  • Git (3)
    • GitLab (2)
  • Programming (6)
    • C# (2)
      • Rider (1)
    • C++ (2)
    • javascript (2)
  • Unity (22)
    • Editor拡張 (1)
    • TextMeshPro (2)
    • UniTask (2)
    • UnityAsset (8)
    • UnityCloudBuild (2)
    • WebGL (2)
  • ゲーム (2)
    • 冠を持つ神の手 (2)
  • 機械学習 (1)
  • 開発 (19)
    • Admob (3)
    • Android (2)
    • Database (1)
      • MySQL (1)
    • DiscordAPI (2)
    • iOS (2)
    • PlayFab (1)
    • Web (2)
      • AmazonS3 (2)
      • Heroku (1)
      • RestAPI (1)
      • WordPress (1)
    • 虹の降る海 (6)
    • 設計 (1)
2020年12月
日 月 火 水 木 金 土
 12345
6789101112
13141516171819
20212223242526
2728293031  
« 11月   1月 »

アーカイブ

  • 2021年4月
  • 2021年3月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年10月
  • 2020年9月
  • 2020年8月
  • 2020年7月
  • 2020年5月
  • 2020年1月
  • 2019年12月
  • 2019年11月
  • 2019年10月
  • 2019年8月
  • 2019年7月
  • 2019年6月
  • 2019年5月
  • 2019年4月
  • 2019年2月
  • 2018年11月
  • 2018年9月
  • 2018年8月
  • 2018年7月

カテゴリー

  • Admob
  • Adobe_XD
  • AdventCalendar
  • AmazonS3
  • Android
  • C#
  • C++
  • CFD
  • Database
  • Design
  • Diary
  • DiscordAPI
  • Editor拡張
  • Event
  • Font
  • Git
  • GitLab
  • Heroku
  • iOS
  • javascript
  • MySQL
  • OpenFOAM
  • Photoshop
  • PlayFab
  • RestAPI
  • Rider
  • TextMeshPro
  • UniTask
  • Unity
  • unity1week
  • UnityAsset
  • UnityCloudBuild
  • Web
  • WebGL
  • WordPress
  • 冠を持つ神の手
  • 機械学習
  • 虹の降る海
  • 設計
  • 開発

メタ情報

  • ログイン
  • 投稿フィード
  • コメントフィード
  • WordPress.org