• ECS:随机数


    各个工作线程的随机数种子是稳定的,RandomSystem的实现如下:

    using Unity.Entities;
    using Unity.Jobs;
    using Unity.Burst;
    using Unity.Collections;
    using Unity.Jobs.LowLevel.Unsafe;
    using Random = Unity.Mathematics.Random;
    
    [UpdateInGroup( typeof(InitializationSystemGroup))]
    public class RandomSystem : ComponentSystem
    {
        public NativeArray<Random> randomArray;
    
        protected override void OnCreate()
        {
            base.OnCreate();
    
            // JobsUtility.MaxJobThreadCount = 7 JobsUtility.MaxJobThreadCount = 128
            randomArray = new NativeArray<Random>(JobsUtility.MaxJobThreadCount, Allocator.Persistent);
    
            var seed = new System.Random();
            for (int i = 0; i < randomArray.Length; ++i)
            {
                randomArray[i] = new Random((uint)seed.Next());
            }
        }
    
        protected override void OnDestroy()
        {
            randomArray.Dispose();
    
            base.OnDestroy();
        }
    
        protected override void OnUpdate()
        {}
    
    }

    在其他的job中使用随机数,下面的代码去掉了冗余逻辑,只留下关键代码:

    using UnityEngine;
    using Unity.Entities;
    using Unity.Transforms;
    using Unity.Jobs;
    using Unity.Burst;
    using Unity.Mathematics;
    using Unity.Collections;
    using Unity.Jobs.LowLevel.Unsafe;
    using Random = Unity.Mathematics.Random;
    
    public partial class TestSystem : JobComponentSystem
    {
        private EndSimulationEntityCommandBufferSystem endSimulationECBS;
        private EntityQueryDesc desc;
    
        protected override void OnCreate()
        {
            endSimulationECBS = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
            desc = new EntityQueryDesc()
            {
                All = new ComponentType[] { ComponentType.ReadOnly<Translation>(), ComponentType.ReadOnly<Rotation>() },
            };
            base.OnCreate();
        }
    
        protected override void OnDestroy()
        {
            base.OnDestroy();
        }
    
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            EntityQuery query = GetEntityQuery(desc);
            NativeArray<Entity> generateSkillEntities = new NativeArray<Entity>(query.CalculateEntityCount(), Allocator.TempJob);
    
            TestJob job = new TestJob()
            {
                ecb = endSimulationECBS.CreateCommandBuffer().ToConcurrent(),
                randomArray = World.GetExistingSystem<RandomSystem>().randomArray,
            };
            JobHandle handle = job.Schedule(this, inputDeps);
    
            endSimulationECBS.AddJobHandleForProducer(handle);
            return handle;
        }
    
        private struct TestJob : IJobForEachWithEntity<Translation, Rotation>
        {
            public EntityCommandBuffer.Concurrent ecb;
    
            // 属性标签很重要
            [NativeDisableParallelForRestriction]
            public NativeArray<Random> randomArray;
    
            // JOB_ID
            [Unity.Collections.LowLevel.Unsafe.NativeSetThreadIndex]
            private int nativeThreadIndex;
    
            public void Execute(Entity entity, int index, [ReadOnly] ref Translation translation, [ReadOnly] ref Rotation rotation)
            {
                Random rand = randomArray[nativeThreadIndex];
                float distance = rand.NextFloat(min, max);
                // ...
            }
        }
    }
  • 相关阅读:
    String类中常用的操作
    android 界面布局 很好的一篇总结[转]
    LeetCode Top 100 Liked Questions in Golang(updating...)
    春招(实习生招聘) 总结
    Tinywebserver:一个简易的web服务器
    Tinychatserver: 一个简易的命令行群聊程序
    Tinyshell: 一个简易的shell命令解释器
    LeetCode169 Majority Element, LintCode47 Majority Number II, LeetCode229 Majority Element II, LintCode48 Majority Number III
    LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III
    剑指offer 1-6
  • 原文地址:https://www.cnblogs.com/sifenkesi/p/13089273.html
Copyright © 2020-2023  润新知