Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Unity.Entities;
public class BounceBallComponent : ComponentDataWrapper<BounceBall>{}
[System.Serializable]
public struct BounceBall : IComponentData
{
public float velocity;
}
using Unity.Entities;
using Unity.Jobs;
using Unity.Collections;
using Unity.Transforms;
using Random = Unity.Mathematics.Random;
using UnityEngine;
public class BounceSystem : JobComponentSystem
{
NativeArray<Random> randoms;
protected override void OnCreateManager()
{
randoms = new NativeArray<Random>(1, Allocator.Persistent)
{
[0] = new Random(123456789)
};
}
protected override void OnDestroyManager()
{
randoms.Dispose();
}
protected override JobHandle OnUpdate(JobHandle inputDeps)
{
var handle = new BallJob()
{
deltaTime = Time.deltaTime
}.Schedule(this, inputDeps);
return new BounceJob()
{
randoms = randoms
}.ScheduleSingle(this, handle);
}
[Unity.Burst.BurstCompile]
struct BallJob : IJobProcessComponentData<Position, BounceBall>
{
public float deltaTime;
public void Execute(ref Position pos, ref BounceBall ball)
{
ball.velocity -= 5f * deltaTime;
pos.Value.y += ball.velocity;
}
}
[Unity.Burst.BurstCompile]
struct BounceJob : IJobProcessComponentData<Position, BounceBall>
{
public NativeArray<Random> randoms;
public void Execute(ref Position pos, ref BounceBall ball)
{
if (pos.Value.y < 0)
{
var random = randoms[0];
var x = random.NextFloat(-10, 10);
pos.Value.y = 0;
pos.Value.x = x;
pos.Value.z = 0;
ball.velocity = random.NextFloat(1.3f, 2);
randoms[0] = random;
}
}
}
}
using UnityEngine;
using Unity.Entities;
using Unity.Collections;
public class RandomBootstrap : MonoBehaviour
{
// BounceBallComponent, MeshInstanceRendeer, PositionをセットしたGameObjectEntity(色違いを3つ)
[SerializeField] GameObject prefab1, prefab2, prefab3;
void Start()
{
var em = World.Active.GetExistingManager<EntityManager>();
const int rap = 10;
using (var entities = new NativeArray<Entity>(rap, Allocator.Temp))
{
em.Instantiate(prefab1, entities);
}
using (var entities = new NativeArray<Entity>(rap, Allocator.Temp))
{
em.Instantiate(prefab2, entities);
}
using (var entities = new NativeArray<Entity>(rap, Allocator.Temp))
{
em.Instantiate(prefab3, entities);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.