Unity Editor Issue With Texture2D inside ScrollView

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!

My second question, which is optional to answer, would be if anyone had good insight on to make these prefab images selectable. In the sense that if I mouse over one it would highlight the bg a different color or a toggle box underneath the image that would trigger the selectable variable in the object prefab itself.

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!

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
    }
}

Don’t declare Rect… inside of OnGUI()

Rect WindowRect;
Rect ScrollView;

Try moving it outside.

Thank you, I moved it to the top of the class with the rest of the variables. But that didn’t have any affect on the result of the code. Obviously the correct organizational thing to do though lol, my bad.

I can’t check atm, but remember that OnGUI() is called more than once per frame,
and Rect might not always contain the same values in each OnGUI() pass.

…, Layout, Repaint.
And during a Repaint, it seems Rect has the correct values.

ex:

...
Event evt = Event.current;
...
if( evt.type == EventType.repaint )
    rect_Last_Layout_Element = GUILayoutUtility.GetLastRect();
...

This is the result of the code btw, I want it to be able to scroll obviously but the scroll doesn’t show up?