Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

My kind of Roll-A-Ball Tutorial Pure ECS

Discussion in 'Entity Component System' started by Emre_U, Mar 31, 2018.

  1. Emre_U

    Emre_U

    Joined:
    Jan 27, 2015
    Posts:
    49
    I have completed my Roll-A-Ball Tutorial in pure ECS.

    There are 4 systems and 1 Job, 1 BootTime script and 1 Component script.

    I don't know everything about ECS yet. But it is a fun way of coding. And whatever I have learned I have written down in the ReadMe on GitHub ( A highly detailed and commented code in there but mostly from point of view of a hobbyist). If you are like me and watching and reading codes for learning ECS, maybe the readMe help you to click it in your brain.

    At least this is another example with some code in it which you can analyze.

    https://github.com/KptEmreU/Roll-A-Ball-ECS-style

    I am a GitHub noob so If it is not working give me DM or something. I will wake up in 6 hours.

    Edit: Btw, you are also more than welcome to point better ways of doing things in ECS or correct my errors. At least I am sure it is working now.
     
    Last edited: Mar 31, 2018
  2. FROS7

    FROS7

    Joined:
    Apr 8, 2015
    Posts:
    26
    Thank you for this! The comments are REALLY helpful. great job
     
  3. Emre_U

    Emre_U

    Joined:
    Jan 27, 2015
    Posts:
    49
    Edit: We have a bug in here though, if you create 100k or so and go very fast and erase ball to quick around 50k entity dead (it is variable and if you are slow it may not happen at all) there is an

    Code (CSharp):
    1. Internal:JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
    Might be our experimental code too
    EndOfEdit.


    This job came from /u/Shinao in Reddit, this is like x38 times faster than my EraseBallsSystem. It is the job version of Component System. Can't say I understand everything in

    Code (CSharp):
    1. protected override JobHandle OnUpdate(JobHandle inputDeps)
    But it is working lightning fast :D Try to spawn 20k or 40k cubes to test it.

    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Transforms;
    3. using Unity.Entities;
    4. using Unity.Jobs;
    5. using Unity.Collections;
    6. using Unity.Mathematics;
    7. using UnityEngine.UI;
    8. using System.Collections.Generic;
    9. public class EraseBallsSystem : JobComponentSystem
    10. {
    11.     public void SetUI()
    12.     {
    13.         scoreText = GameObject.Find("ScoreText").GetComponent<Text>();
    14.     }
    15.     public Text scoreText;
    16.     public struct PlayerGroup
    17.     {
    18.         public ComponentDataArray<Position> playerPos;
    19.         public ComponentDataArray<Cube> cube;
    20.         public int Length;
    21.    
    22.     }
    23.     public struct BallsGroup
    24.     {
    25.         public ComponentDataArray<Ball> ball;
    26.         public ComponentDataArray<Position> ballPos;
    27.         public EntityArray entityArray;
    28.         public SubtractiveComponent<Die> die;
    29.         public int Length;
    30.     }
    31.     [Inject] PlayerGroup playerGroup;
    32.     [Inject] BallsGroup ballsGroup;
    33.     [Inject] EndFrameBarrier endFrameBarrier;
    34.     [ComputeJobOptimization]
    35.     public struct EraseJob : IJobParallelFor
    36.     {
    37.         public Vector3 playerPos;
    38.         public ComponentDataArray<Position> ballsPositions;
    39.         [ReadOnly]
    40.         public EntityArray entities;
    41.         [NativeDisableParallelForRestriction]
    42.         public EntityCommandBuffer commandBuffer;
    43.         public void Execute(int index)
    44.         {
    45.             float dist = math.distance(playerPos, ballsPositions[index].Value);
    46.        
    47.             if (dist < 1 & dist != 0)
    48.                 commandBuffer.DestroyEntity(entities[index]);
    49.         }
    50.     }
    51.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    52.     {
    53.         scoreText.text = (20000 - ballsGroup.Length).ToString();
    54.         // Takes too long to create a nativearray, is there even a pool of it ?
    55.         var handles = new NativeArray<JobHandle>(playerGroup.Length, Allocator.Temp);
    56.         for (int i = 0; i < playerGroup.Length; i++) {
    57.             var job = new EraseJob() {
    58.                 playerPos = playerGroup.playerPos[i].Value,
    59.                 ballsPositions = ballsGroup.ballPos,
    60.                 entities = ballsGroup.entityArray,
    61.                 commandBuffer = endFrameBarrier.CreateCommandBuffer()
    62.             };
    63.             handles[i] = job.Schedule(ballsGroup.Length, 1, inputDeps);
    64.         }
    65.         var combinedHandles = JobHandle.CombineDependencies(handles);
    66.         handles.Dispose();
    67.         return combinedHandles;
    68.     }
    69. }
     
    Last edited: Apr 1, 2018
  4. Tzelon

    Tzelon

    Joined:
    Aug 24, 2013
    Posts:
    18
    Thanks, man! very helpful.
     
  5. shinyhalo

    shinyhalo

    Joined:
    Oct 6, 2018
    Posts:
    4
    This would be useful to me for learning but it's 8 months old and I suspect things have changed. I just finished a youtube tutorial on pure ECS movement that failed because TransformMatrix was deprecated. I tried without it and the camera moves instead of the cube. sigh.

    Could you update this to the latest ECS style in 2018.3.0b10 ?

    I guess that's the problem with making tutorials while it's in beta...I'm just anxious to learn this so I'm digging around.
     
  6. Jeremiahparker

    Jeremiahparker

    Joined:
    Jan 18, 2019
    Posts:
    2
    Does anyone know a full tutorial on pure ECS?
     
  7. psuong

    psuong

    Joined:
    Jun 11, 2014
    Posts:
    118
    There used to be a pure TwoStickShooter on the EntityComponentSystemSamples, but I think it's moved to a different repo. I would look here and take a look at the examples.

    https://github.com/Unity-Technologies/EntityComponentSystemSamples

    If I find a pure example/tutorial on github, I'll certainly post it here. :)
     
    Jeremiahparker likes this.
  8. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    436
    There is no full tutorial and I would not expect one until ECS comes out of preview. API changes a lot and tutorial would be outdated very fast. Currently, the best way to learn is too study samples provided in the samples repository, read the forum, experiment with the current version and ask questions on the forum.
     
    Antypodish likes this.