Skip to content

Commit 5baee3e

Browse files
committed
feat: samples UI fix
1 parent 8c16416 commit 5baee3e

File tree

4 files changed

+19
-22
lines changed

4 files changed

+19
-22
lines changed

Samples~/ChatGPT/ChatGPT Sample.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ MonoBehaviour:
668668
m_EditorClassIdentifier:
669669
inputField: {fileID: 1079664277}
670670
button: {fileID: 1920761190}
671-
context: {fileID: 1216428644}
671+
scroll: {fileID: 356682880}
672672
sent: {fileID: 8660877164928630359, guid: 53cd26a6eefa5a74086203cd118a04d3, type: 3}
673673
received: {fileID: 961534450149497043, guid: adc0146c2853ed1418133faaf4379eb1, type: 3}
674674
--- !u!4 &499436300
@@ -879,7 +879,7 @@ MonoBehaviour:
879879
m_ScaleFactor: 1
880880
m_ReferenceResolution: {x: 1024, y: 768}
881881
m_ScreenMatchMode: 0
882-
m_MatchWidthOrHeight: 1
882+
m_MatchWidthOrHeight: 0
883883
m_PhysicalUnit: 3
884884
m_FallbackScreenDPI: 96
885885
m_DefaultSpriteDPI: 96

Samples~/ChatGPT/ChatGPT.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
using System.Linq;
21
using UnityEngine;
32
using UnityEngine.UI;
43
using System.Collections.Generic;
5-
using System.Threading.Tasks;
64

75
namespace OpenAI
86
{
97
public class ChatGPT : MonoBehaviour
108
{
119
[SerializeField] private InputField inputField;
1210
[SerializeField] private Button button;
13-
[SerializeField] private RectTransform context;
11+
[SerializeField] private ScrollRect scroll;
1412

1513
[SerializeField] private RectTransform sent;
1614
[SerializeField] private RectTransform received;
@@ -26,18 +24,17 @@ private void Start()
2624
button.onClick.AddListener(SendReply);
2725
}
2826

29-
private async void AppendMessage(ChatMessage message)
27+
private void AppendMessage(ChatMessage message)
3028
{
31-
context.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 0);
29+
scroll.content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 0);
3230

33-
var item = Instantiate(message.Role == "user" ? sent : received, context);
31+
var item = Instantiate(message.Role == "user" ? sent : received, scroll.content);
3432
item.GetChild(0).GetChild(0).GetComponent<Text>().text = message.Content;
3533
item.anchoredPosition = new Vector2(0, -height);
36-
37-
await Task.Delay(200);
38-
34+
LayoutRebuilder.ForceRebuildLayoutImmediate(item);
3935
height += item.sizeDelta.y;
40-
context.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
36+
scroll.content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
37+
scroll.verticalNormalizedPosition = 0;
4138
}
4239

4340
private async void SendReply()
@@ -55,6 +52,7 @@ private async void SendReply()
5552
messages.Add(newMessage);
5653

5754
button.enabled = false;
55+
inputField.text = "";
5856
inputField.enabled = false;
5957

6058
// Complete the instruction

Samples~/Text Completion Chat/TextCompletionChat Sample.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ MonoBehaviour:
482482
m_EditorClassIdentifier:
483483
inputField: {fileID: 1110244728}
484484
button: {fileID: 56740866}
485-
context: {fileID: 751059434}
485+
scroll: {fileID: 1210104932}
486486
sent: {fileID: 8660877164928630359, guid: f1ce5484a49526e478c032bcc83af3af, type: 3}
487487
received: {fileID: 961534450149497043, guid: 26e2f7dec8c0be04faf44df827e6dcbd, type: 3}
488488
--- !u!4 &499436300
@@ -1226,7 +1226,7 @@ MonoBehaviour:
12261226
m_ScaleFactor: 1
12271227
m_ReferenceResolution: {x: 1024, y: 768}
12281228
m_ScreenMatchMode: 0
1229-
m_MatchWidthOrHeight: 1
1229+
m_MatchWidthOrHeight: 0
12301230
m_PhysicalUnit: 3
12311231
m_FallbackScreenDPI: 96
12321232
m_DefaultSpriteDPI: 96

Samples~/Text Completion Chat/TextCompletionChat.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using UnityEngine;
22
using UnityEngine.UI;
3-
using System.Threading.Tasks;
43
using System.Collections.Generic;
54

65
namespace OpenAI
@@ -9,17 +8,16 @@ public class TextCompletionChat : MonoBehaviour
98
{
109
[SerializeField] private InputField inputField;
1110
[SerializeField] private Button button;
12-
[SerializeField] private RectTransform context;
11+
[SerializeField] private ScrollRect scroll;
1312

1413
[SerializeField] private RectTransform sent;
1514
[SerializeField] private RectTransform received;
1615

1716
private float height;
1817
private OpenAIApi openai = new OpenAIApi();
1918

20-
private List<ChatMessage> messages = new List<ChatMessage>();
21-
private string prompt = "Act as a random stranger in a chat room and reply to the questions.\nQ: ";
2219
private string userInput;
20+
private string prompt = "Act as a random stranger in a chat room and reply to the questions.\nQ: ";
2321

2422
private void Start()
2523
{
@@ -28,14 +26,15 @@ private void Start()
2826

2927
private void AppendMessage(string message, bool isUser = true)
3028
{
31-
context.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 0);
29+
scroll.content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 0);
3230

33-
var item = Instantiate(isUser ? sent : received, context);
34-
LayoutRebuilder.ForceRebuildLayoutImmediate(item);
31+
var item = Instantiate(isUser ? sent : received, scroll.content);
3532
item.GetChild(0).GetChild(0).GetComponent<Text>().text = message;
3633
item.anchoredPosition = new Vector2(0, -height);
34+
LayoutRebuilder.ForceRebuildLayoutImmediate(item);
3735
height += item.sizeDelta.y;
38-
context.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
36+
scroll.content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
37+
scroll.verticalNormalizedPosition = 0;
3938
}
4039

4140
private async void SendReply()

0 commit comments

Comments
 (0)