Skip to content

Commit b796a90

Browse files
firefighters tutorial step 4: animation and UI
1 parent dc53fa2 commit b796a90

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+5663
-17
lines changed

EntitiesSamples/Assets/Miscellaneous/AnimateGameObject/SpawnSystem.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using System;
21
using Unity.Burst;
32
using Unity.Collections;
43
using Unity.Entities;
54
using UnityEngine;
6-
using Object = UnityEngine.Object;
75

86
namespace Miscellaneous.AnimationWithGameObjects
97
{

EntitiesSamples/Assets/Tutorials/Firefighters/Common/Bot.prefab

-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ MeshRenderer:
6060
m_ReflectionProbeUsage: 1
6161
m_RayTracingMode: 2
6262
m_RayTraceProcedural: 0
63-
m_RayTracingAccelStructBuildFlagsOverride: 0
64-
m_RayTracingAccelStructBuildFlags: 1
65-
m_SmallMeshCulling: 1
6663
m_RenderingLayerMask: 1
6764
m_RendererPriority: 0
6865
m_Materials:

EntitiesSamples/Assets/Tutorials/Firefighters/Common/Config.prefab

+2
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,5 @@ MonoBehaviour:
8888
type: 3}
8989
GroundCellPrefab: {fileID: 3422103013957337004, guid: 0142cf2bddc0e2f448344a8fb2222b4c,
9090
type: 3}
91+
BotAnimatedPrefabGO: {fileID: 5701704621631426976, guid: 36e93c435d1648a4c86d516057a9cbe2,
92+
type: 3}

EntitiesSamples/Assets/Tutorials/Firefighters/Common/ExecuteAuthoring.cs

+21-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public class ExecuteAuthoring : MonoBehaviour
99
public bool ExecuteBotSystem;
1010
public bool ExecuteBucketSystem;
1111
public bool ExecuteTeamSystem;
12-
12+
public bool ExecuteUISystem;
13+
public bool ExecuteAnimationSystem;
14+
1315
class Baker : Baker<ExecuteAuthoring>
1416
{
1517
public override void Bake(ExecuteAuthoring authoring)
@@ -34,6 +36,16 @@ public override void Bake(ExecuteAuthoring authoring)
3436
{
3537
AddComponent<ExecuteTeam>(entity);
3638
}
39+
40+
if (authoring.ExecuteUISystem)
41+
{
42+
AddComponent<ExecuteUI>(entity);
43+
}
44+
45+
if (authoring.ExecuteAnimationSystem)
46+
{
47+
AddComponent<ExecuteAnimation>(entity);
48+
}
3749
}
3850
}
3951
}
@@ -53,4 +65,12 @@ public struct ExecuteBucket : IComponentData
5365
public struct ExecuteTeam : IComponentData
5466
{
5567
}
68+
69+
public struct ExecuteUI : IComponentData
70+
{
71+
}
72+
73+
public struct ExecuteAnimation : IComponentData
74+
{
75+
}
5676
}

EntitiesSamples/Assets/Tutorials/Firefighters/Common/firefighters.png.meta

+114
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EntitiesSamples/Assets/Tutorials/Firefighters/README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ This step adds code to spread the fire. Three solutions are demonstrated:
1818

1919
# Step 3: Bot behaviour
2020

21-
This step adds behiour to the bots, who are organized into teams. Each team forms a line between a pond and the closest fire. A bucket is filled at the pond and then passed up the line.
21+
This step adds behiour to the bots, who are organized into teams. Each team forms a line between a pond and the closest fire. A bucket is filled at the pond and then passed up the line.
22+
23+
# Step 4: Animation and UI
24+
25+
This step replaces the bot capsules with animated characters. A UI HUD element displays the total count of fires that have been doused.

EntitiesSamples/Assets/Tutorials/Firefighters/README.md.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EntitiesSamples/Assets/Tutorials/Firefighters/Step 1/BotAuthoring.cs

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public struct Bot : IComponentData
2828
public Entity Bucket; // The bucket that the bot is carrying.
2929
public bool IsCarrying; // True if carrying a bucket.
3030
public Entity Team; // The team to which the bot belongs.
31+
32+
public readonly bool IsMoving()
33+
{
34+
return !(State == BotState.IDLE
35+
|| State == BotState.CLAIM_BUCKET
36+
|| State == BotState.WAIT_IN_LINE
37+
|| State == BotState.FILL_BUCKET);
38+
}
3139
}
3240

3341
public enum BotState

EntitiesSamples/Assets/Tutorials/Firefighters/Step 1/Components.cs

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Unity.Entities;
2+
using UnityEngine;
23

34
namespace Tutorials.Firefighters
45
{
@@ -7,6 +8,7 @@ public struct Team : IComponentData
78
public Entity Filler;
89
public Entity Douser;
910
public bool HasBucket;
11+
public int NumFiresDoused;
1012
}
1113

1214
// all bots in the team (including the Filler and Douser) in order of passing, starting with the Filler
@@ -24,4 +26,9 @@ public struct Heat : IBufferElementData
2426
{
2527
public float Value;
2628
}
29+
30+
public class BotAnimation : IComponentData
31+
{
32+
public GameObject AnimatedGO; // the GO that is rendered and animated
33+
}
2734
}

EntitiesSamples/Assets/Tutorials/Firefighters/Step 1/ConfigAuthoring.cs

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class ConfigAuthoring : MonoBehaviour
4141
public GameObject BucketPrefab;
4242
public GameObject PondPrefab;
4343
public GameObject GroundCellPrefab;
44+
public GameObject BotAnimatedPrefabGO;
4445

4546
class Baker : Baker<ConfigAuthoring>
4647
{
@@ -76,6 +77,9 @@ public override void Bake(ConfigAuthoring authoring)
7677
PondPrefab = GetEntity(authoring.PondPrefab, TransformUsageFlags.Dynamic),
7778
GroundCellPrefab = GetEntity(authoring.GroundCellPrefab, TransformUsageFlags.Dynamic),
7879
});
80+
var configManaged = new ConfigManaged();
81+
configManaged.BotAnimatedPrefabGO = authoring.BotAnimatedPrefabGO;
82+
AddComponentObject(entity, configManaged);
7983
}
8084
}
8185
}
@@ -110,4 +114,10 @@ public struct Config : IComponentData
110114
public Entity PondPrefab;
111115
public Entity GroundCellPrefab;
112116
}
117+
118+
public class ConfigManaged : IComponentData
119+
{
120+
public GameObject BotAnimatedPrefabGO;
121+
public UIController UIController;
122+
}
113123
}

EntitiesSamples/Assets/Tutorials/Firefighters/Step 3/BotSystem.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Unity.Entities;
33
using Unity.Mathematics;
44
using Unity.Transforms;
5+
using Unity.VisualScripting;
6+
using UnityEngine;
57

68
namespace Tutorials.Firefighters
79
{
@@ -105,6 +107,8 @@ private void BotUpdate_MainThread(ref SystemState state, DynamicBuffer<Heat> hea
105107
HeatSystem.DouseFire(bot.ValueRO.TargetPos, heatBuffer, numRows, numCols);
106108

107109
SystemAPI.SetComponentEnabled<RepositionLine>(bot.ValueRO.Team, true);
110+
var team = SystemAPI.GetComponentRW<Team>(bot.ValueRO.Team);
111+
team.ValueRW.NumFiresDoused++;
108112

109113
bot.ValueRW.State = BotState.MOVE_TO_LINE;
110114
}
@@ -189,7 +193,16 @@ private static bool MoveToTarget(ref LocalTransform botTrans, float2 targetPos,
189193
{
190194
var pos = botTrans.Position;
191195
var dir = targetPos - pos.xz;
192-
var moveVector = math.normalizesafe(dir) * moveSpeed;
196+
var moveVectorNormalized = math.normalizesafe(dir);
197+
var moveVector = moveVectorNormalized * moveSpeed;
198+
199+
// The the animated model faces up the z axis, so we need to rotate it 90 degrees clockwise.
200+
var modelRotation = math.radians(90);
201+
202+
// atan2 returns a counter-clockwise angle of rotation, so we negate to make it clockwise
203+
var facingRotation = -math.atan2(moveVectorNormalized.y, moveVectorNormalized.x);
204+
205+
botTrans.Rotation = quaternion.RotateY(modelRotation + facingRotation);
193206

194207
if (math.lengthsq(moveVector) >= math.lengthsq(dir))
195208
{

EntitiesSamples/Assets/Tutorials/Firefighters/Step 3/Firefighters_Step3.unity

+31-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ OcclusionCullingSettings:
1313
--- !u!104 &2
1414
RenderSettings:
1515
m_ObjectHideFlags: 0
16-
serializedVersion: 10
16+
serializedVersion: 9
1717
m_Fog: 0
1818
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
1919
m_FogMode: 3
@@ -44,6 +44,7 @@ RenderSettings:
4444
LightmapSettings:
4545
m_ObjectHideFlags: 0
4646
serializedVersion: 12
47+
m_GIWorkflowMode: 1
4748
m_GISettings:
4849
serializedVersion: 2
4950
m_BounceScale: 1
@@ -66,6 +67,9 @@ LightmapSettings:
6667
m_LightmapParameters: {fileID: 0}
6768
m_LightmapsBakeMode: 1
6869
m_TextureCompression: 1
70+
m_FinalGather: 0
71+
m_FinalGatherFiltering: 1
72+
m_FinalGatherRayCount: 256
6973
m_ReflectionCompression: 2
7074
m_MixedBakeMode: 2
7175
m_BakeBackend: 1
@@ -129,6 +133,7 @@ GameObject:
129133
m_Component:
130134
- component: {fileID: 776551999}
131135
- component: {fileID: 776551998}
136+
- component: {fileID: 776552000}
132137
m_Layer: 0
133138
m_Name: Directional Light
134139
m_TagString: Untagged
@@ -144,8 +149,9 @@ Light:
144149
m_PrefabAsset: {fileID: 0}
145150
m_GameObject: {fileID: 776551997}
146151
m_Enabled: 1
147-
serializedVersion: 11
152+
serializedVersion: 10
148153
m_Type: 1
154+
m_Shape: 0
149155
m_Color: {r: 1, g: 0.95686275, b: 0.8392157, a: 1}
150156
m_Intensity: 1
151157
m_Range: 10
@@ -195,7 +201,6 @@ Light:
195201
m_BoundingSphereOverride: {x: 0, y: 0, z: 0, w: 0}
196202
m_UseBoundingSphereOverride: 0
197203
m_UseViewFrustumForShadowCasterCull: 1
198-
m_ForceVisible: 0
199204
m_ShadowRadius: 0
200205
m_ShadowAngle: 0
201206
--- !u!4 &776551999
@@ -213,6 +218,29 @@ Transform:
213218
m_Children: []
214219
m_Father: {fileID: 0}
215220
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
221+
--- !u!114 &776552000
222+
MonoBehaviour:
223+
m_ObjectHideFlags: 0
224+
m_CorrespondingSourceObject: {fileID: 0}
225+
m_PrefabInstance: {fileID: 0}
226+
m_PrefabAsset: {fileID: 0}
227+
m_GameObject: {fileID: 776551997}
228+
m_Enabled: 1
229+
m_EditorHideFlags: 0
230+
m_Script: {fileID: 11500000, guid: 474bcb49853aa07438625e644c072ee6, type: 3}
231+
m_Name:
232+
m_EditorClassIdentifier:
233+
m_Version: 3
234+
m_UsePipelineSettings: 1
235+
m_AdditionalLightsShadowResolutionTier: 2
236+
m_LightLayerMask: 1
237+
m_RenderingLayers: 1
238+
m_CustomShadowLayers: 0
239+
m_ShadowLayerMask: 1
240+
m_ShadowRenderingLayers: 1
241+
m_LightCookieSize: {x: 1, y: 1}
242+
m_LightCookieOffset: {x: 0, y: 0}
243+
m_SoftShadowQuality: 0
216244
--- !u!1 &1568938494
217245
GameObject:
218246
m_ObjectHideFlags: 0

0 commit comments

Comments
 (0)