我正在尝试在 Unity 中创建一个终端模拟器。我可以输入文本并将其显示为上面的输出,并且当通过 ScrollView 输入足够的文本时,输出将生成滚动条。
但是我不知道如何在用户输入文本时使滚动条跳到底部。
这是用于绘制 GUI 窗口的方法。
// relevant instance variables
private Rect _windowPosition = new Rect();
List<string> output = new List<string>();
string user_input = "";
Vector2 scroll_pos = new Vector2(1,1);
...
...
...
private void onWindow(int windowID){
GUILayout.BeginVertical();
// Output is placed here. Is Scrollable
scroll_pos = GUILayout.BeginScrollView( scroll_pos );
GUILayout.Label( String.Join("\n", output.ToArray()) );
GUILayout.EndScrollView();
// Output ends here. Next piece is user input
user_input = GUILayout.TextField(user_input);
if (Event.current.Equals(Event.KeyboardEvent("return"))) {
output.Add(user_input);
user_input = ""; //clears the TextField
scroll_pos.x = 1;
scroll_pos.y = 1;
}
GUILayout.EndVertical();
GUI.DragWindow();
}
从我的搜索中,我看到它说我应该更改scroll_pos变量,因为它用于控制/读取滚动条的位置。滚动条的值标准化为 0 - 1 之间。
我尝试强制scroll_pos 值同时为0 和1,但没有任何影响。除此之外,我不知道如何解决这个问题。
最佳答案
尝试改变
scroll_pos.x = 1;
scroll_pos.y = 1;
至
scroll_pos.y += 9999;
如果您想强制水平滚动,也可以使用 X 来实现,但通常控制台不会生成水平滚动,它们会根据您配置的列数强制换行。
关于unity-game-engine - Unity3D ScrollView/ScrollBar 从下到上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31972772/