- Home /
Editor GUILayout.ScrollView not scrolling when typing
I am having a rather frustrating time with ScrollViews. Here's a snippet from my custom editor that should be enough to reproduce the problem.
EditorGUILayout.BeginVertical();
scrollPosition = GUILayout.BeginScrollView (scrollPosition);
_target.text = EditorGUILayout.TextArea(_target.text, style);
GUILayout.EndScrollView ();
EditorGUILayout.EndVertical ();
The problem is that when I give a few carriages so that the scroll bars appear, the scrolling does not follow the typing and I cannot see what I type.
Any ideas?
Answer by Adam-Mechtley · Dec 14, 2016 at 08:02 PM
Hi @Rs. That's super annoying! Would you mind filing a bug report please? In the meantime you can do something like this (though it's a little naive):
EditorGUILayout.BeginVertical();
scrollPosition = EditorGUILayout.BeginScrollView (scrollPosition);
EditorGUI.BeginChangeCheck ();
string newText = EditorGUILayout.TextArea(_target.text);
if (EditorGUI.EndChangeCheck ())
{
if (newText.StartsWith (_target.text))
scrollPosition.y = Mathf.Infinity;
_target.text = newText;
}
EditorGUILayout.EndScrollView ();
EditorGUILayout.LabelField (scrollPosition.ToString ());
EditorGUILayout.EndVertical ();
Setting the y to $$anonymous$$athf.Infinity? :D I LI$$anonymous$$E THAT!
It's also quite clever to check if the text has changed in the end that way. The thing is that StartsWith does a full string comparison, I can't really do that in an asset I'm selling. Isn't there a way to know where the text cursor is? $$anonymous$$aybe I'm wrong but I kind of remember some API of that kind.
I posted on your other question. You could try manually walking like this (I've put both methods in so you can profile and compare performance):
scrollPosition = EditorGUILayout.BeginScrollView (scrollPosition);
EditorGUI.BeginChangeCheck ();
string newText = EditorGUILayout.TextArea (targetText);
if (EditorGUI.EndChangeCheck ())
{
string shorter, longer;
if (newText.Length > targetText.Length)
{
shorter = targetText;
longer = newText;
}
else
{
shorter = newText;
longer = targetText;
}
UnityEngine.Profiling.Profiler.BeginSample ("STARTSWITH", this);
bool changedAtEnd = longer.StartsWith (shorter);
UnityEngine.Profiling.Profiler.EndSample ();
UnityEngine.Profiling.Profiler.BeginSample ("$$anonymous$$ANUALLY WAL$$anonymous$$", this);
changedAtEnd = true;
for (int i = shorter.Length - 1; i >= 0; --i)
{
if (shorter[i] != longer[i])
{
changedAtEnd = false;
break;
}
}
UnityEngine.Profiling.Profiler.EndSample ();
if (changedAtEnd)
scrollPosition.y = $$anonymous$$athf.Infinity;
targetText = newText;
}
EditorGUILayout.EndScrollView ();
Your answer
Follow this Question
Related Questions
How to allocate layout region inside the inspector? 1 Answer
Get mouse position in Editor based on screen coordinates 1 Answer
Inspector turns black on refresh 4 Answers
What should be handled in the custom inspector version of a script? 1 Answer
How to specify the rotation angle in degrees, in the inspector? 2 Answers