パパ先生のゲーム開発ブログ

二児のパパがゲーム開発アレコレを書いているブログ。Unity / UE4 / Scratch / プログラミングゼミ / プログラミング教育

[Unity] ECSスクリプト(Data / Authoring / System)を右クリックから作る

Unity2019.3

Unity DOTS、ECS(Entity Component System)のスクリプトファイル

・IComponentData
・Authoring (ConvetToEntity)
・(Job)ComponentSystem


右クリックのCreateからベースファイルを作成することができます。
(*Unity2019.3で確認しています)
 f:id:papa-sensei:20200202163041p:plain

 

Runtime Component Type

Create->ECS->Runtime Component Type
IComponentDataインターフェースの基本Data

(作成されるファイル)

using System;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
[Serializable]</span>
public struct NewComponent : IComponentData
{
    // Add fields to your component here. Remember that:
    // * A component itself is for storing data and doesn't 'do' anything.
    // * To act on the data, you will need a System.
    // * Data in a component must be blittable, which means a component can
    //   only contain fields which are primitive types or other blittable
    //   structs; they cannot contain references to classes.
    // * You should focus on the data structure that makes the most sense
    //   for runtime use here. Authoring Components will be used for 
    //   authoring the data in the Editor.    
}

 

Authoring Component Type

Create->ECS->Authoring Component Type
IConvertGameObjectToEntityインターフェースとコンバートのテンプレート。

(作成されるファイル)

using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;

[DisallowMultipleComponent]
[RequiresEntityConversion]
public class NewComponent1 : MonoBehaviour, IConvertGameObjectToEntity
{
    // Add fields to your component here. Remember that:
    // * The purpose of this class is to store data for authoring purposes - it is not for use while the game is
    //   running.
    // * Traditional Unity serialization rules apply: fields must be public or marked with [SerializeField], and
    //   must be one of the supported types.
    // For example,
    //    public float scale;
    
    public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    {
        // Call methods on 'dstManager' to create runtime components on 'entity' here. Remember that:
        // * You can add more than one component to the entity. It's also OK to not add any at all.
        // * If you want to create more than one entity from the data in this class, use the 'conversionSystem'
        //   to do it, instead of adding entities through 'dstManager' directly.
        // For example,
        //   dstManager.AddComponentData(entity, new Unity.Transforms.Scale { Value = scale });        
    }
}

 
 

System

Create->ECS->System
振る舞い定義のJobComponentSystem
IJobForEach.Execute と JobHandleのOnUpdateテンプレート

 (作成されるファイル)

using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
using Unity.Transforms;
using static Unity.Mathematics.math;

public class NewSystem : JobComponentSystem
{
    // This declares a new kind of job, which is a unit of work to do.
    // The job is declared as an IJobForEach<Translation, Rotation>,
    // meaning it will process all entities in the world that have both
    // Translation and Rotation components. Change it to process the component
    // types you want.
    // The job is also tagged with the BurstCompile attribute, which means
    // that the Burst compiler will optimize it for the best performance.
    [BurstCompile]
    struct NewSystemJob : IJobForEach<Translation, Rotation>
    {
        // Add fields here that your job needs to do its work.
        // For example,
        //    public float deltaTime;
        
        public void Execute(ref Translation translation, [ReadOnly] ref Rotation rotation)
        {
            // Implement the work to perform for each entity here.
            // You should only access data that is local or that is a
            // field on this job. Note that the 'rotation' parameter is
            // marked as [ReadOnly], which means it cannot be modified,
            // but allows this job to run in parallel with other jobs
            // that want to read Rotation component data.
            // For example,
            //     translation.Value += mul(rotation.Value, new float3(0, 0, 1)) * deltaTime;
        }
    }
    
    protected override JobHandle OnUpdate(JobHandle inputDependencies)
    {
        var job = new NewSystemJob();
        
        // Assign values to the fields on your job here, so that it has
        // everything it needs to do its work when it runs later.
        // For example,
        //     job.deltaTime = UnityEngine.Time.deltaTime;
        // Now that the job is set up, schedule it to be run. 
        return job.Schedule(this, inputDependencies);
    }
}

 
ふと、必要なusingや定義がわからなくなった時に便利そう。