Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity Editor Issue With Texture2D inside ScrollView

Discussion in 'UGUI & TextMesh Pro' started by TIYF, Sep 16, 2016.

  1. TIYF

    TIYF

    Joined:
    Sep 29, 2013
    Posts:
    7
    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!

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEditor;
    6.  
    7. public class WorldManagerWindow : EditorWindow
    8. {
    9.     GameObject[] prefabList; //List of prefabs in the prefab folder
    10.     Vector2 prefabScroll; //Vector2 used for our prefab scroll bar
    11.     float t_x = 10f; // x relation of the labelfield to the image in the prefab scroll
    12.     float t_y = 0f; //arbitrary variable for the y relation of the labelfield to the image in the prefab scroll
    13.  
    14.     [MenuItem("World Manager/World Manager Editor")]
    15.     static void Init()
    16.     {
    17.         //Show existing window instance. If one doesn't exist, make one.
    18.         WorldManagerWindow window = (WorldManagerWindow)EditorWindow.GetWindow(typeof(WorldManagerWindow));
    19.         window.Show();
    20.     }
    21.  
    22.     void OnInspectorUpdate()
    23.     {
    24.         prefabList = Resources.LoadAll<GameObject>("Prefabs"); //Load all prefabs from the prefab folder
    25.         Repaint();
    26.     }
    27.  
    28.     void OnGUI()
    29.     {
    30.         Rect WindowRect; //Possibly arbitrary rect for storing window size
    31.         Rect ScrollView; //Possibly arbitrary rect for storing scroll view size
    32.  
    33.         t_x = 0; //initialize the x offset variable for labels
    34.  
    35.         //Not sure about the necessity of this organizational rect yet
    36.         WindowRect = EditorGUILayout.BeginHorizontal(GUILayout.Width(1000), GUILayout.Height(500));
    37.  
    38.         #region Scrollview for Prefabs
    39.         prefabScroll = EditorGUILayout.BeginScrollView(prefabScroll); //The start of the scrolling
    40.         ScrollView = EditorGUILayout.BeginHorizontal(); //Horizontal layout of prefab images and labels in the scrollview
    41.        
    42.         //Loop through all the objects in the prefab list and call the display function
    43.         foreach (GameObject p in prefabList)
    44.         {
    45.             DisplayPrefabBox(p); //Draws image and label to screen
    46.             t_x += 100f; //offset for the next prefab image
    47.         }
    48.         EditorGUILayout.EndHorizontal();
    49.         EditorGUILayout.EndScrollView();
    50.         #endregion
    51.  
    52.         EditorGUILayout.EndHorizontal();
    53.     }
    54.  
    55.     void DisplayPrefabBox(GameObject prefab)
    56.     {
    57.         AssetPrefabScript script = prefab.GetComponent<AssetPrefabScript>();
    58.         Sprite sprite = script.imageS;
    59.         Texture t = sprite.texture;
    60.         Rect tr = sprite.textureRect;
    61.         Rect r = new Rect(tr.x / t.width, tr.y / t.height, tr.width / t.width, tr.height / t.height);
    62.  
    63.         EditorGUILayout.BeginVertical(); //Should begin vertical view of image and then label underneath it
    64.         GUI.DrawTextureWithTexCoords(new Rect(t_x, t_y, tr.width, tr.height), t, r); //Draw the image to the window
    65.         EditorGUI.LabelField(new Rect(t_x, (t_y + 200), 100, 100), prefab.name); //draw the label
    66.         EditorGUILayout.EndVertical();
    67.  
    68.         t_x += (tr.width); //add the width of the last image to the offset
    69.     }
    70. }
    71.  
     
  2. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    Don't declare Rect... inside of OnGUI()
    Code (csharp):
    1.  
    2. Rect WindowRect;
    3. Rect ScrollView;
    4.  
    Try moving it outside.
     
  3. TIYF

    TIYF

    Joined:
    Sep 29, 2013
    Posts:
    7
    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.
     
  4. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    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:
    Code (csharp):
    1. ...
    2. Event evt = Event.current;
    3. ...
    4. if( evt.type == EventType.repaint )
    5.     rect_Last_Layout_Element = GUILayoutUtility.GetLastRect();
    6. ...
     
  5. TIYF

    TIYF

    Joined:
    Sep 29, 2013
    Posts:
    7
    This is the result of the code btw, I want it to be able to scroll obviously but the scroll doesn't show up?