Created
September 3, 2021 06:19
-
-
Save JohnnyTurbo/37727eca7e3d22da0eeeb5a24ca54a66 to your computer and use it in GitHub Desktop.
Code used in Component Data From Entity tutorial video on the Turbo Makes Games YouTube Channel - https://youtu.be/sXYVmjdVi9g
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
using UnityEngine; | |
namespace TMG.ECS_GetComponentFromEntity | |
{ | |
[GenerateAuthoringComponent] | |
public class BlobAssetManagedData : IComponentData | |
{ | |
public Entity[] Leaders; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
using Unity.Rendering; | |
using UnityEngine; | |
namespace TMG.ECS_GetComponentFromEntity | |
{ | |
public class FollowerInputSystem : SystemBase | |
{ | |
protected override void OnUpdate() | |
{ | |
var followerEntity = GetSingletonEntity<FollowerMoveData>(); | |
var followerMoveData = EntityManager.GetComponentData<FollowerMoveData>(followerEntity); | |
var followerRenderMesh = EntityManager.GetSharedComponentData<RenderMesh>(followerEntity); | |
#region InputRegion | |
var entityIndex = -1; | |
if (Input.GetKeyDown(KeyCode.Alpha1)) | |
{ | |
entityIndex = 0; | |
} | |
else if (Input.GetKeyDown(KeyCode.Alpha2)) | |
{ | |
entityIndex = 1; | |
} | |
else if (Input.GetKeyDown(KeyCode.Alpha3)) | |
{ | |
entityIndex = 2; | |
} | |
else if (Input.GetKeyDown(KeyCode.Alpha4)) | |
{ | |
entityIndex = 3; | |
} | |
else if (Input.GetKeyDown(KeyCode.Alpha5)) | |
{ | |
entityIndex = 4; | |
} | |
if (entityIndex < 0) | |
{ | |
return; | |
} | |
#endregion | |
var leaderEntity = followerMoveData.LeaderReference.Value.Array[entityIndex].Value; | |
followerMoveData.CurrentLeaderEntity = leaderEntity; | |
var leaderMaterial = EntityManager.GetSharedComponentData<RenderMesh>(leaderEntity).material; | |
followerRenderMesh.material = leaderMaterial; | |
EntityManager.SetComponentData(followerEntity, followerMoveData); | |
EntityManager.SetSharedComponentData(followerEntity, followerRenderMesh); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Collections; | |
using Unity.Entities; | |
using Unity.Transforms; | |
namespace TMG.ECS_GetComponentFromEntity | |
{ | |
[GenerateAuthoringComponent] | |
public struct FollowerMoveData : IComponentData | |
{ | |
public Entity CurrentLeaderEntity; | |
public float MinFollowDistance; | |
public BlobAssetReference<LeaderEntityBlobAsset> LeaderReference; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
using Unity.Mathematics; | |
using Unity.Transforms; | |
namespace TMG.ECS_GetComponentFromEntity | |
{ | |
public class FollowerMoveSystem : SystemBase | |
{ | |
protected override void OnUpdate() | |
{ | |
var allTranslations = GetComponentDataFromEntity<Translation>(true); | |
var deltaTime = Time.DeltaTime; | |
Entities.ForEach((Entity e, ref Translation translation, ref Rotation rotation, in FollowerMoveData followerMoveData, in MovementData movementData) => | |
{ | |
if(!allTranslations.HasComponent(followerMoveData.CurrentLeaderEntity)){return;} | |
var targetPosition = allTranslations[followerMoveData.CurrentLeaderEntity].Value; | |
#region MoveAndRotateRegion | |
rotation.Value = quaternion.LookRotation(translation.Value - targetPosition, new float3(0, 1, 0)); | |
var toTarget = targetPosition - translation.Value; | |
if(math.length(toTarget) <= followerMoveData.MinFollowDistance){return;} | |
var moveStep = math.normalize(toTarget) * movementData.MoveSpeed * deltaTime; | |
translation.Value += moveStep; | |
#endregion | |
}).Run(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
namespace TMG.ECS_GetComponentFromEntity | |
{ | |
[GenerateAuthoringComponent] | |
public struct GameControllerTag : IComponentData {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
using UnityEngine; | |
namespace TMG.ECS_GetComponentFromEntity | |
{ | |
public struct LeaderEntity | |
{ | |
public Entity Value; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
namespace TMG.ECS_GetComponentFromEntity | |
{ | |
public struct LeaderEntityBlobAsset : IComponentData | |
{ | |
public BlobArray<LeaderEntity> Array; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
using Unity.Mathematics; | |
using UnityEngine; | |
using Random = Unity.Mathematics.Random; | |
namespace TMG.ECS_GetComponentFromEntity | |
{ | |
[GenerateAuthoringComponent] | |
public struct LeaderMoveData : IComponentData | |
{ | |
public float3x2 MovementBounds; | |
public float3 CurrentDestination; | |
public Random Random; | |
public void SetRandomDestination() | |
{ | |
CurrentDestination = Random.NextFloat3(MovementBounds[0], MovementBounds[1]); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
using Unity.Mathematics; | |
using Unity.Transforms; | |
namespace TMG.ECS_GetComponentFromEntity | |
{ | |
public class LeaderMoveSystem : SystemBase | |
{ | |
protected override void OnStartRunning() | |
{ | |
Entities.ForEach((ref LeaderMoveData leaderMoveData) => | |
{ | |
leaderMoveData.SetRandomDestination(); | |
}).Run(); | |
} | |
protected override void OnUpdate() | |
{ | |
var deltaTime = Time.DeltaTime; | |
Entities.ForEach((Entity e, ref Translation translation, ref Rotation rotation, ref LeaderMoveData leaderMoveData, in MovementData movementData) => | |
{ | |
var currentDestination = leaderMoveData.CurrentDestination; | |
rotation.Value = quaternion.LookRotation(translation.Value - currentDestination, new float3(0, 1, 0)); | |
var toDestination = currentDestination - translation.Value; | |
if (math.length(toDestination) <= 0.01f) | |
{ | |
leaderMoveData.SetRandomDestination(); | |
} | |
var moveStep = math.normalize(toDestination) * movementData.MoveSpeed * deltaTime; | |
translation.Value += moveStep; | |
}).Run(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Entities; | |
using UnityEngine; | |
namespace TMG.ECS_GetComponentFromEntity | |
{ | |
[GenerateAuthoringComponent] | |
public struct MovementData : IComponentData | |
{ | |
public float MoveSpeed; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Collections; | |
using Unity.Entities; | |
namespace TMG.ECS_GetComponentFromEntity | |
{ | |
public class SetupEntityBlobAssetSystem : SystemBase | |
{ | |
protected override void OnStartRunning() | |
{ | |
var gameControllerEntity = GetSingletonEntity<GameControllerTag>(); | |
var blobAssetManagedData = EntityManager.GetComponentData<BlobAssetManagedData>(gameControllerEntity); | |
using var blobBuilder = new BlobBuilder(Allocator.Temp); | |
ref var leaderEntityBlobAsset = ref blobBuilder.ConstructRoot<LeaderEntityBlobAsset>(); | |
var leaderEntityArray = | |
blobBuilder.Allocate(ref leaderEntityBlobAsset.Array, blobAssetManagedData.Leaders.Length); | |
for (var i = 0; i < blobAssetManagedData.Leaders.Length; i++) | |
{ | |
var curLeader = blobAssetManagedData.Leaders[i]; | |
leaderEntityArray[i] = new LeaderEntity | |
{ | |
Value = curLeader | |
}; | |
} | |
var followerData = GetSingleton<FollowerMoveData>(); | |
followerData.LeaderReference = | |
blobBuilder.CreateBlobAssetReference<LeaderEntityBlobAsset>(Allocator.Persistent); | |
SetSingleton(followerData); | |
} | |
protected override void OnUpdate() | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment