はじめに
少し前に話題になったProject Tiny
ですが、最近は全く聞かなくなってしまったような気がします。
調べたところJanuary 22, 2021
にProject Tiny Full Preview 0.32
が公開され、それ以降はバージョンアップはされていないようです。
Unity - Project Tiny 0.32 preview is available: UI & new Skinned Mesh Renderer -Blendshape sample - Unity Forum
これをみるとオワッタか・・・と思うかもしれませんが、一応Dec 9, 2021
にECS
頑張ってるよとの報告があったので、それに付随してProject Tiny
も進んでいると信じたいです。
forum.unity.com
今更チュートリアルかいかとも思われるかもしれませんが、DOTS
の練習にもなると思いますし触ってみたいと思います。
docs.google.com
Unity DOTS
・Project Tiny
に幸あれ。
導入方法
Package Manager
からインストール or manifest.json
でインストールを行います。
com.unity.tiny.all
また必要な依存先のパッケージも自動でインストールしてくれるので便利です。
環境
Unity 2020.1.17f1 URP*1
Project Tiny Full 0.32.0-preview.55
↓ 依存先
DOTS Runtime 0.32.0-preview.54
Entities 0.17.0-preview.41
Project Tiny 0.32.0-preview.54
Project Tiny for Web 0.32.0-preview.54
Project Tiny for Desktop 0.32.0-preview.54
Unity Physics 0.6.0-preview.3
2D Entities Physics 0.2.0-preview.5
Platforms 0.10.0-preview.10
Platforms Andriod 0.10.0-preview.10
Platforms iOS 0.10.0-preview.10
Platforms Linux 0.10.0-preview.10
Platforms macOS 0.10.0-preview.10
Platforms Web 0.10.0-preview.10
Platforms Windows 0.10.0-preview.10
Platforms 0.10.0-preview.10
Render-Pipeline Universal 8.3.1 (8.2.0)*2
Hybrid Renderer 0.11.0-preview.42
フォルダ・アセンブリの下準備
1. Assets
フォルダ直下にGameSystems
というフォルダを作成
2. 作成したフォルダの中にGameSystems
という名前のAssembly Definition
を作成
3. Assembly Definition
のAssembly Definition References
に以下を追加する
- Unity.Entities
- Unity.Collections
- Unity.Transforms
- Unity.Mathematics
- Unity.Tiny.Core
- Unity.Tiny.Rendering
- Unity.Scenes*3
私の場合は以下のようなフォルダ構成になっています。
Assets |-- GameSystems | |-- GameSystems.asmdef |-- Scenes | |-- Main.scene |-- Settings |-- URPで用いる設定ファイル等
スクリプト作成
ECS
の説明はしませんが、まずはComponent Data
を作成します。
using Unity.Entities; namespace GameSystems { [GenerateAuthoringComponent] public struct RotateComponent : IComponentData { public float Speed; } }
次にそれを利用したSystem
を作成。
using Unity.Entities; using Unity.Mathematics; using Unity.Transforms; namespace GameSystems { public class RotateSystem : SystemBase { protected override void OnUpdate() { var deltaTime = Time.DeltaTime; Entities.ForEach((ref Rotation rot, in RotateComponent rc) => { rot.Value = math.mul(rot.Value, quaternion.RotateY(deltaTime * rc.Speed)); }) .ScheduleParallel(); } } }
勝手にnamespace
をつけたり、in
修飾子に変えたりしましたがほぼチュートリアル通りです。
ScheduleParallel()
なのでJobSystem
を用いたマルチスレッドで動作しますが、対象のEntity
が少ないと逆に時間がかかってしまうことがあるので注意です。
Assets |-- GameSystems | |-- GameSystems.asmdef | |-- RotateComponent.cs | |-- RotateSystem.cs |-- Scenes | |-- Main.scene |-- Settings |-- URPで用いる設定ファイル等
シーン作成
Main.scene
というシーンを新規作成し、Main Camera
ゲームオブジェクトにアタッチされているAudio Listener
を削除します。
Hierarchy
から3D Object -> Cube
を選択してCube
を作成、位置を調整してあげます。
Cube
をクリックしてインスペクターから以下の操作をします。
Box Collider
を削除RotateComponent
をアタッチし、Speed
を0.2
に設定UniversalRenderingPipline/Lit
なMaterial
を設定
また後ろに壁を作成しました。(私の環境だとCamera
のBackground
が反映されなかったので暫定的に)
Box Collider
を削除- 大きさを調整
UniversalRenderingPipline/Unlit
なMaterial
を設定
サブシーンを作成する
次にScene
上に存在するゲームオブジェクトを全て選択している状態で右クリックしNew Sub Scene -> From Selection
を選択し、サブシーンを作成します。
少しだけ補足すると、サブシーンは事前にGameObject
をEntity
へと変換してくれる便利機能です。Conver to Entity
をサブシーン内のゲームオブジェクトに事前に実行しているようなイメージです。
Assets |-- GameSystems | |-- GameSystems.asmdef | |-- RotateComponent.cs | |-- RotateSystem.cs |-- Materials | |-- Wall.mat | |-- Cube.mat |-- Scenes | |-- Main.scene | |-- SubScene.scene |-- Settings |-- URPで用いる設定ファイル等
buildconfiguration作成
Assets
フォルダ以下にBuild
というフォルダを作成。
その中でCreate -> Build -> DOTS Runtime Build Configuration
を選択して、buildconfiguration
ファイルを作成します。
またインスペクターより以下の設定をします。
SceneList
にMain.scene
を設定Dots Runtime Build Profile
のTarget
は自身の環境を設定Dots Runtime Root Assembly
にGameSystems.asmdef
を設定Tiny Rendering Setting
をアタッチ
Assets |-- Build | |-- DotsRuntimeBuildConfiguration.buildconfiguration |-- GameSystems | |-- GameSystems.asmdef | |-- RotateComponent.cs | |-- RotateSystem.cs |-- Materials | |-- Wall.mat | |-- Cube.mat |-- Scenes | |-- Main.scene | |-- SubScene.scene |-- Settings |-- URPで用いる設定ファイル等
動作確認をする
DotsRuntimeBuildConfiguration.buildconfiguration
の右上のBuild and Run
を選択すれば、ビルド・再生してくれます。