Git Product home page Git Product logo

ecslite-threads-unity's Introduction

Unity Jobs support for LeoEcsLite C# Entity Component System framework

Unity jobs support for LeoECS Lite.

Tested on unity 2020.3 (dependent on it) and contains assembly definition for compiling to separate assembly file for performance reason.

Important! Don't forget to use DEBUG builds for development and RELEASE builds in production: all internal error checks / exception throwing works only in DEBUG builds and eleminated for performance reasons in RELEASE.

Table of content

Socials

discord

discord

Installation

As unity module

This repository can be installed as unity module directly from git url. In this way new line should be added to Packages/manifest.json:

"com.leoecscommunity.ecslite.threads.unity": "https://github.com/LeoECSCommunity/ecslite-threads-unity.git",

By default last released version will be used. If you need trunk / developing version then develop name of branch should be added after hash:

"com.leoecscommunity.ecslite.threads.unity": "https://github.com/LeoECSCommunity/ecslite-threads-unity.git#develop",

As source

If you can't / don't want to use unity modules, code can be downloaded as sources archive of required release from Releases page.

Example

Component

struct C1 {
    public int Id;
}

JobSystem

class TestJobSystem : EcsUnityJobSystem<TestJob, C1> {
    protected override int GetChunkSize (EcsSystems systems) {
        // How many entities will be processed in one chunk.
        // This method will be called each frame.
        return 1000;
    }

    protected override EcsWorld GetWorld (EcsSystems systems) {
        // World for component pools and filter.
        return systems.GetWorld ();
    }
    
    protected override EcsFilter GetFilter (EcsWorld world) {
        // Filter for processing.
        return world.Filter<C1> ().End ();
    }
    
    // Optional additional initialization of job structure.
    protected override void SetData (EcsSystems systems, ref TestJob job) {
        job.DeltaTime = Time.deltaTime;
    }
}

Important! EcsUnityJobSystem supports up to 4 component pools.

Job

struct TestJob : IEcsUnityJob<C1> {
    public float DeltaTime;
    NativeArray<int> _entities;
    [NativeDisableParallelForRestriction]
    NativeArray<C1> _pool1;
    [NativeDisableParallelForRestriction]
    NativeArray<int> _indices1;

    public void Init (NativeArray<int> entities, NativeArray<C1> pool1, NativeArray<int> indices1) {
        // entities array.
        _entities = entities;
        // dense array of components.
        _pool1 = pool1;
        // sparse indices array of components.
        _indices1 = indices1;
    }

    public void Execute (int index) {
        // We should get entity id from entities collection. 
        var entity = _entities[index];
        // To get access for C1 data.
        var pool1Idx = _indices1[entity];
        var c1 = _pool1[pool1Idx];
        // We can change it.
        c1.Id = (c1.Id + 1) % 10000;
        // And save it back.
        _pool1[pool1Idx] = c1;
    }
}

Important! Don't forget to add NativeDisableParallelForRestriction attribute to pool/indices arrays.

License

The software is released under the terms of the MIT license.

No personal support or any guarantees.

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.