Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TIYF · Sep 18, 2016 at 01:21 PM · editor-scriptingeditorwindowscrollviewguilayout

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]

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Custom EditorWindow Scrollbars not working with GUILayout areas 1 Answer

How do you use EditorGUILayout.TextArea with EditorGUILayout.ScrollViewScope? 1 Answer

Undo slider in an EditorWindow 1 Answer

EditorUtiltiy.GetAssetPreview(Object asset) 0 Answers

Reordable List in a window 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges