【Unityゲーム開発】Unity TinyでEntityの動かし方

こんにちは。
今回はUnityTinyで Entityの動かし方を紹介します。
こちらの記事は以下のバージョンで動作確認しました。
Unity 2019.2.0b
Project Tiny 0.15.3
Project Tiny 0.15.3
もくじ
・IComponentData
・インスペクター
・スクリプト
・デモ動画
・IComponentData
・インスペクター
・スクリプト
・デモ動画
IComponentDataの設定
下記のスクリプトを動かしたいEntityにアタッチします。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using Unity.Entities; using Unity.Mathematics; namespace Test { public struct MoveTest : IComponentData { public float3 Direction; public float Speed; } } |
インスペクター
動かしたいEntityのインスペクターを下記のように設定しました
Speedは任意で設定てください。
スクリプト
Scriptフォルダに下記のMovementScriptを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
using Unity.Entities; using Unity.Mathematics; using Unity.Tiny.Core; using Unity.Tiny.Core2D; using Unity.Tiny.Input; namespace Test { public class MovementSystem : ComponentSystem { protected override void OnUpdate() { Entities.ForEach((Entity entity, ref MoveTest movetest, ref Translation translation) => { var position = translation.Value; var inputSystem = World.GetExistingSystem<inputsystem>(); if (inputSystem.GetKeyDown(KeyCode.W)) { movetest.Direction = new Unity.Mathematics.float3(x: 0, y: 1, z: 0); } else if (inputSystem.GetKey(KeyCode.A)) { movetest.Direction = new Unity.Mathematics.float3(x: -1, y: 0, z: 0); } else if (inputSystem.GetKey(KeyCode.S)) { movetest.Direction = new Unity.Mathematics.float3(x: 0, y: -1, z: 0); } else if (inputSystem.GetKey(KeyCode.D)) { movetest.Direction = new Unity.Mathematics.float3(x: 1, y: 0, z: 0); } if (inputSystem.GetKeyUp(KeyCode.W)|| inputSystem.GetKeyUp(KeyCode.A) || inputSystem.GetKeyUp(KeyCode.S) || inputSystem.GetKeyUp(KeyCode.D) ) { movetest.Direction = new Unity.Mathematics.float3(x: 0, y: 0, z: 0); } position += movetest.Direction * World.TinyEnvironment().frameDeltaTime * movetest.Speed; translation.Value = position; }); } } } </inputsystem> |
デモ動画
上記のようにオブジェクトが動くのが確認できました。
Unityを習得したらゲーム業界へ転職しよう!
・ゲーム業界特化の専門エージェント
・未経験からの転職サポート
・エンジニア経験のコンサルタント対応
・未経験からの転職サポート
・エンジニア経験のコンサルタント対応
TechStarsで転職した人の給与はなんと平均で83万円もアップ!
「自分は未経験だから、、」「転職できる技術力があるか不安、、」
この様な悩みをお持ちでも大丈夫です!エンジニア出身のコンサルタントが親切丁寧に対応!
ベンチャーや中小企業、有名大手企業など、幅広い選択肢の 中から、あなたに合った職場を紹介いたします!
人気記事:Unityは独学で勉強できる?

ノマドしながらゲームやアプリ開発。
主にゲーム開発や海外生活についてブログを書います。
現在フィリピンのセブ島在住。
大学卒業→ドイツ留学→日本で就職→ノマド生活開始
アプリリリース合計8本
ゆるいノマドの日常をYoutubeでアップしてます。