Git Product home page Git Product logo

udon-task's Introduction

UdonTask

Releases

This package enables the execution of Udon from a separate thread.

Udonを別スレッドで実行可能にするやつ。

Installation

Iwashi Packagesを開いて「Add to VCC」を押してVCCからUdonTaskを追加します。

Usage

通常

using UnityEngine;
using UdonSharp;
using VRC.Udon.Common.Interfaces;
using Iwashi.UdonTask;

public class UdonTaskSample : UdonSharpBehaviour
{
  public void ExecuteTask()
  {
    UdonTask.New((IUdonEventReceiver)this, nameof(OnProcess), nameof(OnComplete));
  }

  public void OnProcess()
  {
    /* ここに重たい処理を書く。
     * メインスレッドでしか触れないものは当然触れません。要注意! */
  }

  public void OnComplete()
  {
    Debug.Log("完了!");
  }
}

戻り値・引数付き

using UnityEngine;
using UdonSharp;
using VRC.Udon.Common.Interfaces;
using Iwashi.UdonTask;

public class UdonTaskSample : UdonSharpBehaviour
{
	public void ExecuteTask()
	{
		UdonTask.New((IUdonEventReceiver)this, nameof(OnProcess), nameof(OnComplete), "onProcessContainer", "onReturnContainer", "イワシ");
	}

	public UdonTaskContainer OnProcess(UdonTaskContainer onProcessContainer)
	{
		var container = UdonTaskContainer.New();
		var str = onProcessContainer.GetVariable<string>(0);
		container = container.AddVariable($"{str}ーモ");
		Debug.Log(container.GetVariable<string>(container.Count() - 1));
		return container;
	}

	public void OnComplete(UdonTaskContainer onReturnContainer)
	{
		Debug.Log(onReturnContainer.GetVariable<string>(0));
	}
}
  • 第1引数にIUdonEventReceiverを指定します。(IUdonEventReceiver)thisを使うと自身のUdonSharpBehaviourを設定できます。
  • 10秒以上かかる処理はUdonが死ぬので実行できません。9.9秒くらいを測って分割するようにしてください。
  • 引数付きを利用する場合は関数の引数は必ずそのスクリプト内で一意の名前になるようにしてください。
    • UdonTask.Newする際に引数の名前を指定する必要があります。

Samples

UnityのPackageManagerのUdonTask→Samplesからサンプルシーンをインポートできます。

サンプルシーンではBase64エンコードされた17枚の画像データを高速で読み込むテスト等ができます。

テストワールド

TIPS

  • 別スレッドからSendCustomEventDelayedSeconds/SendCustomEventDelayedFramesを呼ぶとメインスレッドを呼び出せる。
  • 別スレッドで触ってるフィールドをメインスレッドで触るとタイミング次第でUdonが死ぬ。
    • そもそもタイミング次第で死ぬ。UdonVMのスタックがずれるっぽい?特定のフィールドが別の型に化けるとか起きる。
  • 別スレッドからでもDebug.Logの出力は可能。

Note

  • Harmonyを用いてスレッドセーフなUdonログの出力を実装。
  • スレッドセーフな疑似ランダム生成器クラスを実装。

udon-task's People

Contributors

chiugame avatar ureishi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.