Skip to content

Commit 2f565c9

Browse files
committed
cleanup
1 parent 224e28c commit 2f565c9

File tree

3 files changed

+76
-49
lines changed

3 files changed

+76
-49
lines changed

Assets/Prefabs/UI/UIToolkit/PostGameUICanvasUITK.prefab

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ GameObject:
7676
m_Component:
7777
- component: {fileID: 2100339957089282678}
7878
- component: {fileID: 1286002055205536738}
79+
- component: {fileID: 7623313333782190830}
7980
m_Layer: 5
8081
m_Name: MessageFeed
8182
m_TagString: Untagged
@@ -118,6 +119,19 @@ MonoBehaviour:
118119
m_WorldSpaceSizeMode: 1
119120
m_WorldSpaceWidth: 1920
120121
m_WorldSpaceHeight: 1080
122+
--- !u!114 &7623313333782190830
123+
MonoBehaviour:
124+
m_ObjectHideFlags: 0
125+
m_CorrespondingSourceObject: {fileID: 0}
126+
m_PrefabInstance: {fileID: 0}
127+
m_PrefabAsset: {fileID: 0}
128+
m_GameObject: {fileID: 5550880791410142769}
129+
m_Enabled: 1
130+
m_EditorHideFlags: 0
131+
m_Script: {fileID: 11500000, guid: 5972d28314a4d794cbe88ba0120b39c0, type: 3}
132+
m_Name:
133+
m_EditorClassIdentifier:
134+
doc: {fileID: 2063287831439623020}
121135
--- !u!1 &8225198564806970540
122136
GameObject:
123137
m_ObjectHideFlags: 0
@@ -128,7 +142,6 @@ GameObject:
128142
m_Component:
129143
- component: {fileID: 5886284794912537810}
130144
- component: {fileID: 6577115012106585769}
131-
- component: {fileID: 8405269881768119238}
132145
m_Layer: 5
133146
m_Name: MessageItem
134147
m_TagString: Untagged
@@ -164,23 +177,9 @@ MonoBehaviour:
164177
m_Name:
165178
m_EditorClassIdentifier:
166179
m_PanelSettings: {fileID: 11400000, guid: 30704bc49d34869449e8bfdb3ab57841, type: 2}
167-
m_ParentUI: {fileID: 2063287831439623020}
180+
m_ParentUI: {fileID: 1286002055205536738}
168181
sourceAsset: {fileID: 9197481963319205126, guid: 67ec44aea0b0f484c86c6489932cfcad, type: 3}
169182
m_SortingOrder: 0
170183
m_WorldSpaceSizeMode: 1
171184
m_WorldSpaceWidth: 1920
172185
m_WorldSpaceHeight: 1080
173-
--- !u!114 &8405269881768119238
174-
MonoBehaviour:
175-
m_ObjectHideFlags: 0
176-
m_CorrespondingSourceObject: {fileID: 0}
177-
m_PrefabInstance: {fileID: 0}
178-
m_PrefabAsset: {fileID: 0}
179-
m_GameObject: {fileID: 8225198564806970540}
180-
m_Enabled: 1
181-
m_EditorHideFlags: 0
182-
m_Script: {fileID: 11500000, guid: 5972d28314a4d794cbe88ba0120b39c0, type: 3}
183-
m_Name:
184-
m_EditorClassIdentifier:
185-
doc: {fileID: 6577115012106585769}
186-
AddMessage: 0

Assets/Scenes/PostGame.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:210256cb84e18304ddd5a5b6639fd1f381c07c16d428d5783ea70484d0e8a258
3-
size 31377
2+
oid sha256:dc602b61e976980167216fdb5e6d3014e7b96d87e9ebb31be2621ee50843c746
3+
size 31965

Assets/Scripts/Gameplay/UI/MessageFeed.cs

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Unity.BossRoom.Gameplay.Messages;
1010
using Unity.BossRoom.Infrastructure;
1111
using VContainer;
12+
using Random = System.Random;
1213

1314
public class MessageFeed : MonoBehaviour
1415
{
@@ -96,9 +97,20 @@ void OnLifeStateChangedEvent(LifeStateChangedEventMessage eventMessage)
9697
}
9798
}
9899

100+
[ContextMenu("Add RandomMessage")]
101+
public void AddRandomMessage()
102+
{
103+
var rand = new Random(nameof(AddRandomMessage).GetHashCode());
104+
m_Messages.Add(new Message { isShown = true, startTime = Time.realtimeSinceStartup, message = rand.Next().ToString() });
105+
}
106+
99107
void Start()
100108
{
101109
var root = doc.rootVisualElement;
110+
111+
// you could load this template from a template uxml, don't need to be a part of the visual tree
112+
// could make it a prop on this monobehaviour
113+
// VisualTreeAsset messageTempalte; or load it via Resources api, than you don't need to hide them
102114
var templateLabel = root.Q<Label>("messageLabel");
103115
var templateBox = root.Q<VisualElement>("messageBox");
104116

@@ -108,13 +120,45 @@ void Start()
108120

109121
m_Messages = new List<Message>();
110122

111-
// Create a container for all messages
112-
m_MessageContainer = new VisualElement();
123+
// Create a container for all messages
124+
var listView = root.Q<ListView>("messageList");
125+
listView.makeItem += () =>
126+
{
127+
// Create a new message if no reusable messages are available
128+
var newBox = new VisualElement();
129+
newBox.AddToClassList("messageBox");
130+
//newBox.style.position = Position.Absolute; // Explicitly position it
131+
132+
// Position the new message box below the last message
133+
//newBox.style.top = m_MessageContainer.childCount * (messageHeight + verticalSpacing);
134+
135+
var newLabel = new Label();
136+
newLabel.AddToClassList("message");
137+
newBox.Add(newLabel);
138+
139+
140+
newBox.style.opacity = 1;
141+
newBox.style.display = DisplayStyle.Flex;
142+
143+
newBox.RegisterCallback<AttachToPanelEvent>((e)=> StartCoroutine(FlyInWithBounce(e.target as VisualElement, -300, 50, 0.2f, 0.2f)));
144+
145+
return newBox;
146+
};
147+
148+
listView.bindItem += (element, i) =>
149+
{
150+
element.Q<Label>().text = m_Messages[i].message;
151+
};
152+
153+
// collection change events will take care of creating and disposing items
154+
listView.itemsSource = m_Messages;
155+
156+
157+
m_MessageContainer = listView;
113158
m_MessageContainer.style.flexDirection = FlexDirection.Column; // Arrange messages vertically
114159

115160
// make sure other visual elements don't get pushed down by the message container
116161
m_MessageContainer.style.position = Position.Absolute;
117-
doc.rootVisualElement.Add(m_MessageContainer);
118162
}
119163

120164
void OnDestroy()
@@ -132,11 +176,11 @@ void Update()
132176
if (m.isShown)
133177
{
134178
// Check if a message should begin fading out
135-
if (Time.realtimeSinceStartup - m.startTime > 5 && m.messageBox.style.opacity == 1)
136-
{
137-
StartFadeout(m, 1f);
138-
m.isShown = false;
139-
}
179+
// if (Time.realtimeSinceStartup - m.startTime > 5 && m.style.opacity == 1)
180+
//{
181+
//StartFadeout(m, 1f);
182+
// m.isShown = false;
183+
//}
140184
}
141185
}
142186
}
@@ -161,40 +205,22 @@ void ShowMessage(string message)
161205

162206
if (newMessage == null)
163207
{
164-
// Create a new message if no reusable messages are available
165-
var newBox = new VisualElement();
166-
newBox.AddToClassList("messageBox");
167-
newBox.style.position = Position.Absolute; // Explicitly position it
168-
// Position the new message box below the last message
169-
//newBox.style.top = m_MessageContainer.childCount * (messageHeight + verticalSpacing);
170-
171-
var newLabel = new Label();
172-
newLabel.AddToClassList("message");
173-
newBox.Add(newLabel);
174-
175208
newMessage = new Message()
176209
{
177210
isShown = true,
178211
startTime = Time.realtimeSinceStartup,
179-
messageBox = newBox,
180-
Label = newLabel
181212
};
182213

183-
m_MessageContainer.Add(newBox); // Add the message box to the parent container
184214
m_Messages.Add(newMessage); // Add to the list of messages
185215
}
186216

187217
// Set the properties of the reused or new message
188218
newMessage.isShown = true;
189-
newMessage.Label.text = message;
219+
190220
newMessage.startTime = Time.realtimeSinceStartup;
191-
newMessage.messageBox.style.opacity = 1;
192-
newMessage.messageBox.style.display = DisplayStyle.Flex;
193-
194-
// Start a fly-in animation
195-
StartCoroutine(FlyInWithBounce(newMessage.messageBox, -300, 50, 0.2f, 0.2f));
221+
newMessage.message = message;
196222
}
197-
223+
198224
IEnumerator FlyInWithBounce(VisualElement element, float startLeft, float targetLeft, float duration, float bounceDuration)
199225
{
200226
float elapsedTime = 0;
@@ -243,6 +269,7 @@ IEnumerator FlyInWithBounce(VisualElement element, float startLeft, float target
243269
element.style.left = targetLeft;
244270
}
245271

272+
/*
246273
static void StartFadeout(Message message, float opacity)
247274
{
248275
message.messageBox.schedule.Execute(() =>
@@ -259,12 +286,13 @@ static void StartFadeout(Message message, float opacity)
259286
}
260287
}).Every((long)0.1f).Until(() => opacity <= 0);
261288
}
289+
*/
262290

291+
// if you bind the itemsource to the list you don't actually have to manually do this
263292
class Message
264293
{
265294
public bool isShown;
266295
public float startTime; // The time when the message was shown
267-
public VisualElement messageBox;
268-
public Label Label;
296+
public string message;
269297
}
270298
}

0 commit comments

Comments
 (0)