I am programming my first EditorWindow and I am having a hard time working with the documentations. Especially the Scrollview. I learned from testing that I have to do “beginscrollview” before “beginhorizontal” for it to work. But at that time I had placeholder strings for the list of textures I wanted to display.
But since I inserted the for-loop for the textures in the horizontal-space, the scrollbar doesn’t show up anymore. Is it because I mix GUI with GUILayout? Or because of the Texture-Rects?
(With placeholder-textures for now, but) it should look like this:
The Code:
void OnGUI()
{
GUILayout.Label("Object", EditorStyles.boldLabel);
scrollPos = EditorGUILayout.BeginScrollView(scrollPos, true); //Scrollable things starting from here
EditorGUILayout.BeginHorizontal(); //Multiple Objects in line horizontal
for(int j = 0; j < prefabList.Count; j++) //List of Objects
{
Texture curIcon = prefabList[j].prefabIcon as Texture; //Icon of Object from List
EditorGUI.DrawPreviewTexture(new Rect(100*j+20,0,70,70), curIcon); //Draw that icon as a texture in the Window
}
EditorGUILayout.EndHorizontal(); //everything else not in line
EditorGUILayout.EndScrollView(); //everything else not scrollable
}
What am I doing wrong?
(Also, if I want to be able to click on the icons to select the corresponding object to be instantiated how would I do that? I have the Object together with the Icon in a list, that should be no problem, but what about the hovering over and clicking an icon thing?
Maybe I just can’t think straight right now…)
Like LukaKotar said any kind of Layout group only works with layout elements. Otherwise the layout group doesn’t get any information how big the content area has to be.
Since you just need a rect of size 70 x 70 for each element all you have to do is using GetRect(70, 70) inside your loop:
for(int j = 0; j < prefabList.Count; j++)
{
Rect rect = GUILayoutUtility.GetRect(70, 70);
Texture curIcon = prefabList[j].prefabIcon as Texture;
EditorGUI.DrawPreviewTexture(rect, curIcon);
}
GetRect is the most important method when it comes to the layout system. Almost all layout element functions are internally calling some overload of GetRect to reserve a rectangle and then just call the non layout element function with that rect.
Note: GetRect will return an empty rect during the “Layout” event and the actual layouted rect during any other event.
The scrollbar only seems to work with EditorGUILayout elements. A somewhat nasty workaround is to create empty space with EditorGUILayout.Space() as many times as needed per loop iteration, so that the scrollbar appears. Space() takes up 10 pixels.
Something like this worked for me:
scrollPos = EditorGUILayout.ScrollView(scrollPos, GUILayout.Width(x), GUILayout.Height(y));
EditorGUILayout.BeginHorizontal();
for( ... ; ... ; ... ){
// Your GUI code here
for(int x = 0; x < 10; x++){
// Loop through as many times as needed
EditorGUILayout.Space();
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndScrollView()
Additionally, I also used GUILayout.BeginArea() before the scroll view to restrict what’s being scrolled.
It’s not an ideal solution since you are limited to multiples of 10. But still, I rather be able to scroll too far than not at all.