Пример #1
0
    void Start()
    {
        if (_GameOfLifeTexture == null)
        {
            return;
        }

        _TextureWidth = _GameOfLifeTexture.width;
        _Pixels       = new NativeArray <float>(_TextureWidth * _TextureWidth, Allocator.Persistent);
        _ColorsNative = new NativeArray <Color>(_TextureWidth * _TextureWidth, Allocator.Persistent);
        _Colors       = new Color[_Pixels.Length];
        _Tex          = new Texture2D(_GameOfLifeTexture.width, _GameOfLifeTexture.height, TextureFormat.RGBA32, false);
        for (int x = 0; x < _Pixels.Length; x++)
        {
            _Pixels[x] = Random.Range(0, 1f);
        }
        var size = (int)math.pow(2, _MapSize);

        _Random = new Unity.Mathematics.Random((uint)(DateTime.Now.Ticks));
        _Map    = new MapData(size, size);
        _MoveDirectionIndexOffset    = new NativeArray <int>(8, Allocator.Persistent);
        _MoveDirectionIndexOffset[0] = size;
        _MoveDirectionIndexOffset[1] = 1 + size;
        _MoveDirectionIndexOffset[2] = 1;
        _MoveDirectionIndexOffset[3] = 1 - size;
        _MoveDirectionIndexOffset[4] = -size;
        _MoveDirectionIndexOffset[5] = -1 - size;
        _MoveDirectionIndexOffset[6] = -1;
        _MoveDirectionIndexOffset[7] = -1 + size;
        var directionsJob = new GenerateDirectionsJob()
        {
            Map = _Map
        };
        var handle      = directionsJob.Schedule(_Map.tiles.Length, 64);
        var generateJob = new GenerateRandomLifeJob()
        {
            tiles  = _Map.tiles,
            random = _Random
        };

        handle = generateJob.Schedule(_Map.tiles.Length, 64, handle);
        handle.Complete();
    }
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            if (mapData.IsValid)
            {
                return(inputDeps);
            }

            var maps = query.ToComponentDataArray <Map>(Allocator.TempJob);

            if (maps.Length == 0)
            {
                maps.Dispose();
                return(inputDeps);
            }

            var map = maps[0];

            maps.Dispose();
            mapData = new MapData(map.width, map.height);

            var updateMap = new GenerateMapJob
            {
                mapTiles = mapData.tiles,
                width    = mapData.width
            };

            inputDeps = updateMap.Schedule(this, inputDeps);
            var directions = new GenerateDirectionsJob
            {
                map = mapData
            };

            inputDeps = directions.Schedule(this, inputDeps);

            _barier.AddJobHandleForProducer(inputDeps);
            return(inputDeps);
        }