Unity Scrollview scrollt nicht nach unten
Posted: 20 Feb 2025, 12:10
Ich arbeite gerade mit Unity und Chatgpt. Ich stelle GPT eine Frage und die Antwort wird in einem Feld TMP (TMP) angezeigt. Ich kann jedoch nicht ganz nach unten scrollen, der Scroller -Griff erreicht seinen Boden, aber der Text ist nicht einmal am Ende. Die Hierarchie sieht ungefähr so aus: < /p>
Jede Hilfe würde als ich hart an diesem
Code: Select all
-Canvas
--Scroll View
---Viewport
----Content
-----Text(TMP) (has a content size fitter)
----Scrollbar Vertical
--Input Field (working as intended)
--scrollbarcontroller (empty gameobject with script to handle the scrolling)< /code>
< /div>
< /div>
< /p>
Aktueller Code: < /p>
< div class = "snippet">
using UnityEngine;
using UnityEngine.UI;
public class DynamicScrollbar : MonoBehaviour
{
public ScrollRect scrollRect;
public RectTransform content;
private float lastContentHeight = 0f;
private float lastScrollbarSize = 1f; // Keep track of the correct size
private void Start()
{
lastContentHeight = content.rect.height;
UpdateScrollbarSize();
}
private void LateUpdate()
{
if (content.rect.height != lastContentHeight)
{
lastContentHeight = content.rect.height;
UpdateScrollbarSize();
}
else if (scrollRect.verticalScrollbar.size != lastScrollbarSize)
{
// If Unity tries to reset it, override it with the correct value
scrollRect.verticalScrollbar.size = lastScrollbarSize;
}
}
private void UpdateScrollbarSize()
{
if (scrollRect.verticalScrollbar != null)
{
float newSize = (content.rect.height > scrollRect.viewport.rect.height) ?
Mathf.Clamp(scrollRect.viewport.rect.height / content.rect.height, 0.1f, 1f) : 1f;
lastScrollbarSize = newSize; // Store the correct size
scrollRect.verticalScrollbar.size = newSize;
}
}
}
Jede Hilfe würde als ich hart an diesem