- Home /
Editor GUI ScrollView Actual Scrollbar Not Showing Up??
Hey all, I am currently working on the GUI to an asset for a project I'm working on in a team setting. The purpose of this UI is to allow our game designers to pick from a list of prefabs, select a prefab, and insert it into the game world.
My issue is right now I am trying to make use of the scrollview to allow me to display the sprite associated with each prefab object and the name of the object. Eventually adding functionality to select the prefab from the list. Question one is simple, why does this code not pull up a horizontal scroll bar for my scroll view. I feel like I am completely overlooking something when it comes to the proper use of the scroll view. The images all display how I want them to other than the fact that it just cuts off early and doesn't allow me to scroll if anyone could please elaborate on why this happens that would be great!
Anyways, thank you guys very much, and sorry for any messy code you may find. I'm still learning the best way to organize all of this Editor GUI code!
[code] using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor;
public class WorldManagerWindow : EditorWindow { GameObject[] prefabList; //List of prefabs in the prefab folder Vector2 prefabScroll; //Vector2 used for our prefab scroll bar float t_x = 10f; // x relation of the labelfield to the image in the prefab scroll float t_y = 0f; //arbitrary variable for the y relation of the labelfield to the image in the prefab scroll
[MenuItem("World Manager/World Manager Editor")]
static void Init()
{
//Show existing window instance. If one doesn't exist, make one.
WorldManagerWindow window = (WorldManagerWindow)EditorWindow.GetWindow(typeof(WorldManagerWindow));
window.Show();
}
void OnInspectorUpdate()
{
prefabList = Resources.LoadAll<GameObject>("Prefabs"); //Load all prefabs from the prefab folder
Repaint();
}
void OnGUI()
{
Rect WindowRect; //Possibly arbitrary rect for storing window size
Rect ScrollView; //Possibly arbitrary rect for storing scroll view size
t_x = 0; //initialize the x offset variable for labels
//Not sure about the necessity of this organizational rect yet
WindowRect = EditorGUILayout.BeginHorizontal(GUILayout.Width(1000), GUILayout.Height(500));
#region Scrollview for Prefabs
prefabScroll = EditorGUILayout.BeginScrollView(prefabScroll); //The start of the scrolling
ScrollView = EditorGUILayout.BeginHorizontal(); //Horizontal layout of prefab images and labels in the scrollview
//Loop through all the objects in the prefab list and call the display function
foreach (GameObject p in prefabList)
{
DisplayPrefabBox(p); //Draws image and label to screen
t_x += 100f; //offset for the next prefab image
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.EndScrollView();
#endregion
EditorGUILayout.EndHorizontal();
}
void DisplayPrefabBox(GameObject prefab)
{
AssetPrefabScript script = prefab.GetComponent<AssetPrefabScript>();
Sprite sprite = script.imageS;
Texture t = sprite.texture;
Rect tr = sprite.textureRect;
Rect r = new Rect(tr.x / t.width, tr.y / t.height, tr.width / t.width, tr.height / t.height);
EditorGUILayout.BeginVertical(); //Should begin vertical view of image and then label underneath it
GUI.DrawTextureWithTexCoords(new Rect(t_x, t_y, tr.width, tr.height), t, r); //Draw the image to the window
EditorGUI.LabelField(new Rect(t_x, (t_y + 200), 100, 100), prefab.name); //draw the label
EditorGUILayout.EndVertical();
t_x += (tr.width); //add the width of the last image to the offset
}
} [/code]
Answer by Azengar · Nov 21, 2016 at 07:20 AM
Hello, I'm not very experienced with Unity either but I stumbled on the same issue and here is the solution I came up with, hope that will help.
The problem is that you're using the GUILayout element : EditorGUILayout.BeginScrollView
and inside it you're putting GUI elements (and not GUILayouts elements !) with GUI.DrawTextureWithTexCoords
which are, it would seem, not recognised by the GUILayout scroll viewer.
So the only way I found to get this to work is to use a GUI scrollviewer inside a GUILayout scrollviewer (because if you get rid of your GUILayout scroll viewer the inspector display will be messed up) :
// GUILayout scroll viewer (Does not affect the scrollping position)
// Assuming you display sprites, you'll want to set the size of the both scroll viewers
// to the size of the texture
GUILayout.BeginScrollView(scroll,
GUILayout.Width(texture.width),
GUILayout.Height(texture.height));
// GUI scroll viewer
scroll = GUI.BeginScrollView(new Rect(0, 0, texture.width, texture.height),
scroll,
new Rect(0, 0, texture.width, texture.height));
// Your GUI Elements inside the scroll viewer
GUI.EndScrollView();
GUILayout.EndScrollView();
I know that it's not a very beautiful solution but I couldn't find any better way and maybe this will help someone with the same issue as I didn't find anything about this on google.