• 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
1
Question by SteenPetersen · Aug 14, 2017 at 05:26 PM · guiscrollviewguilayout

GUILayout.BeginScrollView cant seem to change where the box I create is positoned.

Hi I am trying to make a chat window for my multiplayer Game. I have done everything except for the fact that the window where the text is placed is always in the top left hand corner of the screen and it is driving me nuts.

 Vector2 scrollPosition = new Vector2(5698, 312);  // I have put an abstract value here to show that this values does not matter at all and no matter what value I write here it does the same thing.

// and this is the text inside OnGui()

     scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(450), GUILayout.Height(450));
     
             foreach (var cm in chatHistory)
             {
                 GUILayout.Label(cm);
             }
     
      GUILayout.EndScrollView();
 

This bow always ends up being placed in the top left hand corner of the screen and I do not understand why. how can I tell the Scrollview to have a certain x and Y coordinate and a certain width and height.

thanks.

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
1
Best Answer

Answer by Bunny83 · Aug 14, 2017 at 09:41 PM

A ScrollView is also a layouted gui element when you use the GUILayout version. Like any GUILAyout element you never explicitly specify the position. Every element gets it's position and size from the layoutsystem.

One way is to use a horizontal and a vertical layout group and use a Space before the ScrollView. However that might be a bit too complicated for your case. If you just want to specify where a certain layour group should be positioned you can use GUILayout.BeginArea and GUILayout.EndArea around your ScrollView. In BeginArea you have to specify a screen rect. Everything between BeginArea and EndArea will be layouted seperately.

The Scroll position only controls the scroll offset of the child objects of the ScrollView. Though you can only scroll when the child objects would overflow the scrollview area. The same way your browser won't allow any scrolling then the whole website fits onto the screen / window.

Comment
Add comment · Show 3 · 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
avatar image SteenPetersen · Aug 15, 2017 at 04:32 AM 0
Share

Hi Bunny and thank you for answering my question - before I accept it as correct I just want to make sure I get everything clear - I have wrapped my code with the "beginarea" and have been able to position it where I want it. What I dont fully understand is your last paragraph and the reason as to why I cannot scroll now. I have made the "scrollview" the same size and the scrollbar does appear, but it wont scroll up. I do have a Peice of code that forces it down but it doesnt seem to be affecting it in anyway in this case. Have tried commenting it out with no change.

here is the relevant code now with your input:

  Vector2 scrollPosition = new Vector2(0, 0);  // variable      
 
 
  GUILayout.BeginArea(new Rect(0, Screen.height - 255, 450, 222));
 
         scrollPosition = GUILayout.BeginScrollView(new Vector2(0, Screen.height -255), GUILayout.Width(450), GUILayout.Height(222));
 
         foreach (var cm in chatHistory)
         {
             GUILayout.Label(cm);
         }
 
 
         GUILayout.EndScrollView();
 
         GUILayout.EndArea();
 
         current$$anonymous$$essage = GUI.TextField(new Rect(0, Screen.height - 25, 450, 25), current$$anonymous$$essage);
         current$$anonymous$$essage = Regex.Replace(current$$anonymous$$essage, @"[^a-zA-Z0-9/ ]", "");

avatar image Bunny83 SteenPetersen · Aug 15, 2017 at 10:40 AM 0
Share

You currently use a fix scroll position. You pass in

 new Vector2(0, Screen.height -255)

So the scroll view will always show the same part. In your original code you correctly pass scrollPosition .

avatar image SteenPetersen Bunny83 · Aug 16, 2017 at 06:52 AM 1
Share

Fantastic thanks for the help and good explanation.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

112 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Invisible BeginScrollView thumb 0 Answers

ScrollView - How to stop infinite scrolling? 1 Answer

GUIlayout scrollbar, Not working well... 1 Answer

ScrollWindow AutoUpdating value help :( 1 Answer

How to change thickness of ScrollBar of GUILayout ScrollView? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges