firefighters tutorial: added UI button to reposition the bucket brigade lines

This commit is contained in:
Brian Will 2024-02-26 20:54:52 -08:00
Родитель 4a66021bfd
Коммит 2f1f7df9f4
4 изменённых файлов: 33 добавлений и 3 удалений

Просмотреть файл

@ -22,4 +22,4 @@ This step adds behiour to the bots, who are organized into teams. Each team form
# Step 4: Animation and UI
This step replaces the bot capsules with animated characters. A UI HUD element displays the total count of fires that have been doused.
This step replaces the bot capsules with animated characters. A UI HUD element displays the total count of fires that have been doused. Clicking a HUD button triggers repositioning of the bucket brigade lines.

Просмотреть файл

@ -1,3 +1,4 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False">
<ui:Label tabindex="-1" text="Number of fires doused: 0" parse-escape-sequences="true" display-tooltip-when-elided="true" name="DouseCounter" style="font-size: 28px; color: rgb(255, 255, 255);" />
<ui:Label tabindex="-1" text="Number of fires doused: 0" parse-escape-sequences="true" display-tooltip-when-elided="true" name="DouseCounter" style="font-size: 28px; color: rgb(0, 0, 0);" />
<ui:Button text="Reposition the bucket brigade lines" parse-escape-sequences="true" display-tooltip-when-elided="true" name="RepositionButton" style="font-size: 28px; -unity-text-align: middle-left; width: 30%;" />
</ui:UXML>

Просмотреть файл

@ -1,14 +1,33 @@
using UnityEditor.Compilation;
using UnityEngine;
using UnityEngine.UIElements;
public class UIController : MonoBehaviour
{
private Label dousedLabel;
private Button repositionButton;
private bool reposition = false;
private void OnEnable()
{
var root = GetComponent<UIDocument>().rootVisualElement;
dousedLabel = root.Q<Label>();
repositionButton = root.Q<Button>();
repositionButton.clicked += OnRepositionButton;
}
private void OnRepositionButton()
{
reposition = true;
}
public bool ShouldReposition()
{
var temp = reposition;
reposition = false;
return temp;
}
public void SetNumFiresDoused(int numFiresDoused)

Просмотреть файл

@ -4,6 +4,7 @@ using UnityEngine;
namespace Tutorials.Firefighters
{
[UpdateBefore(typeof(LineSystem))]
public partial struct UISystem : ISystem
{
private bool initialized;
@ -29,10 +30,19 @@ namespace Tutorials.Firefighters
configManaged.UIController = GameObject.FindObjectOfType<UIController>();
}
var shouldReposition = configManaged.UIController.ShouldReposition();
var totalFiresDoused = 0;
foreach (var team in SystemAPI.Query<RefRO<Team>>())
foreach (var (team, entity) in
SystemAPI.Query<RefRO<Team>>()
.WithEntityAccess())
{
totalFiresDoused += team.ValueRO.NumFiresDoused;
if (shouldReposition)
{
SystemAPI.SetComponentEnabled<RepositionLine>(entity, true);
}
}
configManaged.UIController.SetNumFiresDoused(totalFiresDoused);