Want to add 5 buttons vertically inside scrollview, help needed

My Window

Rect rect_inv_main_window = new Rect(Screen.width*10/100, Screen.height*10/100, Screen.width-Screen.width*20/100, Screen.height-Screen.height*20/100);

Please see if this is my correct code, and how can i add 5 buttons and get the scroller -

void DoMyWindow2(int windowIDE) {
		 scrollPosition = GUI.BeginScrollView(rect_inv_main_window, scrollPosition, new Rect(Screen.width*15/100, Screen.height*15/100, Screen.width-Screen.width*25/100, Screen.height-Screen.height*25/100));


		GUI.skin.button = inv_main_window_button;

GUI.Button(new Rect(Screen.width*20/100, Screen.height*20/100+10, 20, 20), "x1");
		GUI.Button(new Rect(Screen.width*20/100, Screen.height*30/100+10, 20, 20), "x2");
		GUI.Button(new Rect(Screen.width*20/100, Screen.height*40/100+10, 20, 20), "x3");
		GUI.Button(new Rect(Screen.width*20/100, Screen.height*50/100+10, 20, 20), "x4");
		GUI.Button(new Rect(Screen.width*20/100, Screen.height*60/100+10, 20, 20), "x5");
		GUI.Button(new Rect(Screen.width*20/100, Screen.height*70/100+10, 20, 20), "x6");
		GUI.Button(new Rect(Screen.width*20/100, Screen.height*80/100+10, 20, 20), "x7");

		
		// close button, keep this out of scroller
		if (GUI.Button(new Rect(Screen.width*75/100+10, Screen.height*70/100+10, 20, 20), "x")){
			open_Inventory_Window=false;
			Time.timeScale = 1.0f;
            Debug.Log("Clicked the button with text");	
		}
		
        GUI.EndScrollView(); 
    }

This is what i want to achieve
1220678--50282--$default.jpg

This is my window, no scroller…why ?

Your ‘rect_inv_main_window’ appears to be bigger than the viewRect parameter of the scrollview, though it should be the other way around and the first two paramters of the viewRect are normally 0.

Also if you want the close button to appear outside of the scrollview you should put it behind EndScrollView()

Ref:

which one is 0 and why ? i am not getting you, what exactly should be my line of code ?

Why don’t you try selection grid instead of a bunch of buttons? I guess is the same idea, I use it frecuently in my code. Here is an example of how I’m doing it:

GUILayout.BeginArea(Rect(800,240, 420, 220));
		
		scrollPosition1 = GUILayout.BeginScrollView (
		scrollPosition1, GUILayout.Width (420), GUILayout.Height (220));
		selGridInt1 = GUILayout.SelectionGrid (selGridInt1, arrNombresAnadidos, 1);
		GUILayout.EndScrollView ();
		GUILayout.EndArea();

Of course as you can see here, the area and scrollview have the same size (420 x 220) Try it and then tell us how it goes, happy coding! :slight_smile:

Can you extend this code, add 5 buttons VERTICALLY in a scrollbar, so i’l try to change my entire thing

Rect rect_inv_main_window = new Rect(100,100,200,200);
	Vector2 scrollPosition;
	
	
	void OnGUI(){
		GUI.Window(0,rect_inv_main_window,DoMyWindow2,"Window");
	}
	
	void DoMyWindow2(int windowIDE) {

         scrollPosition = GUI.BeginScrollView(new Rect(10,20,rect_inv_main_window.width * 0.5f, rect_inv_main_window.height - 40), scrollPosition, new Rect(0, 0, 50,Screen.height));


		GUI.Button(new Rect(20, 50, 20, 20), "x1");
        GUI.Button(new Rect(20, 100, 20, 20), "x2");
        GUI.Button(new Rect(20, 150, 20, 20), "x3");
        GUI.Button(new Rect(20, 200, 20, 20), "x4");
        GUI.Button(new Rect(20, 250, 20, 20), "x5");
        GUI.Button(new Rect(20, 300, 20, 20), "x6");
        GUI.Button(new Rect(20, 350, 20, 20), "x7");


        GUI.EndScrollView(); 
		
		if (GUI.Button(new Rect(150, 150, 20, 20), "x")){
            Debug.Log("Clicked the button with text");  
        }
    }

The first parameter of the scrollview is the size of the object in the gui, the third paramter is the “internal” size of the scrollview.

If you try selectiongrid with parameter “1” at the end, it will work as you want (vertically), check the code I posted before, it’s much simpler than coding a lot of buttons.

Edit: Anyway if you want to use the scrollview and a bunch of buttons just check the area you are showing in the scrollview, if it’s too high, it won’t show the way you want.

This is working perfectly fine for me, i am stuck with 1 issue, i dont want the user to scroll using the scroller bar, he should be able to swipe anywhere on the screen to scroll

How can i do it ?

You can scroll by changing the scrollPosition…i do not own a mobile license for unity so i used the standard axis u may need to alter those according to work with swipe. But the idea is the same…simply change scrollPosition variable.

If you didnt change your input axis, you should be able to scroll with w,a,s,d using this example.

Rect rect_inv_main_window = new Rect(100,100,200,200);

    Vector2 scrollPosition;
	float scrollSpeed = 3;
    

    void Update(){
		scrollPosition.x += Input.GetAxis("Horizontal") * scrollSpeed;
		scrollPosition.y -= Input.GetAxis("Vertical") * scrollSpeed;
	}
	

    void OnGUI(){
        GUI.Window(0,rect_inv_main_window,DoMyWindow2,"Window");
    }

    

    void DoMyWindow2(int windowIDE) {

         scrollPosition = GUI.BeginScrollView(new Rect(10,20,rect_inv_main_window.width * 0.5f, rect_inv_main_window.height - 40), scrollPosition, new Rect(0, 0, Screen.width ,Screen.height));

        GUI.Button(new Rect(20, 50, 20, 20), "x1");
        GUI.Button(new Rect(20, 100, 20, 20), "x2");
        GUI.Button(new Rect(20, 150, 20, 20), "x3");
        GUI.Button(new Rect(20, 200, 20, 20), "x4");
        GUI.Button(new Rect(20, 250, 20, 20), "x5");
        GUI.Button(new Rect(20, 300, 20, 20), "x6");
        GUI.Button(new Rect(20, 350, 20, 20), "x7");

        GUI.EndScrollView(); 


        if (GUI.Button(new Rect(150, 150, 20, 20), "x")){
            Debug.Log("Clicked the button with text");  
        }
    }

i changed just the height and width values and w,a,s,d is not working

Here are the lines i changed [rest all is same] -

scrollPosition = GUI.BeginScrollView(new Rect(10,20,rect_inv_main_window.width*0.9f, rect_inv_main_window.height - 20), scrollPosition, new Rect(0, 0, 50,Screen.height));

GUI.Button(new Rect(0, 10, 100, 40), "x1");
        GUI.Button(new Rect(0, 60, 100, 40), "x2");
        GUI.Button(new Rect(0, 110, 100, 40), "x3");
        GUI.Button(new Rect(0, 160, 100, 40), "x4");
        GUI.Button(new Rect(0, 210, 100, 40), "x5");
        GUI.Button(new Rect(0, 260, 100, 40), "x6");
        GUI.Button(new Rect(0, 310, 100, 40), "x7");

I think you overlooked the important part here:

 void Update(){
        scrollPosition.x += Input.GetAxis("Horizontal") * scrollSpeed;
        scrollPosition.y -= Input.GetAxis("Vertical") * scrollSpeed;
    }

noo,

i have added it

using UnityEngine;
using System.Collections;
 
public class BottleThrowPointController : MonoBehaviour
{
	
	
	
	void Start(){
		PlayerPrefs.DeleteAll();
		Load_powerup_1_1();
		Debug.Log(bought_toggle_val_powerup_1_1);
	}
	
	
        public bottleMovement[] bottles; // list of bottles to instantiate
	
	 public GUIStyle Inv_button;
	 Rect rect_inventory = new Rect(Screen.width*93/100, Screen.height*1/100, Screen.width/12, Screen.height/8);
	
	public GUIStyle inv_main_window_button; 
	public GUIStyle inv_main_window;
     //Rect rect_inv_main_window = new Rect(Screen.width*10/100, Screen.height*10/100, Screen.width-Screen.width*20/100, Screen.height-Screen.height*20/100);
	 Rect rect_inv_main_window = new Rect(100,100,200,200);
	//Rect rect_inv_main_window = new Rect(hosizontal position,vertical position,size-horizontal,size vertical);
	public Vector2 scrollPosition;
	float scrollSpeed = 3;
	
	
	 public GUIStyle toggle_style;
	private bool toggle_val = false;
	bool open_Inventory_Window = false;
	
	
	public static int toggle_val_powerup_1_1=10;
		public static bool bought_toggle_val_powerup_1_1=false;
	public static string save_powerup_1_1;
       
	
	void OnGUI(){
		
		//Debug.Log(toggle_val);
		 GUI.skin.button = Inv_button;
		 GUI.skin.window = inv_main_window;
		
		
		
		// this button is just used to show the image and nothing else
        if (GUI.Button(new Rect(Screen.width*93/100, Screen.height*1/100, Screen.width/12, Screen.height/8), "")){
            Debug.Log("Clicked the button with text");	
		}
		GUI.Box(rect_inventory, "someText");
		
		if(open_Inventory_Window==true)
			rect_inv_main_window = GUI.Window(2, rect_inv_main_window, DoMyWindow2, "");
		
		//main Inventory power up window
		
		
		
	}
//       
        // Update is called once per frame
        void Update ()
        {
		
		scrollPosition.x += Input.GetAxis("Horizontal") * scrollSpeed;
        scrollPosition.y -= Input.GetAxis("Vertical") * scrollSpeed;
		
		if (!rect_inventory.Contains( new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y + 1))) { 
			if (Input.GetMouseButtonDown(0) Time.timeScale!=0)
				 {
			
                        if ( Input.mousePosition.x > Screen.width/2 )
                                SpawnBottle( BottleDirection.RIGHT );
                        else
                                SpawnBottle( BottleDirection.LEFT );
				Debug.Log("outside rect"); 
                
        }
			
			
		}
		else if (rect_inventory.Contains( new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y + 1))) {
		if (Input.GetMouseButtonDown(0)){
				Time.timeScale = 0.0f; // paused the game
				open_Inventory_Window = true;
			Debug.Log("inside rect");   
			}
        }
		
		
//                if (Input.GetMouseButtonDown(0))
//                {
//			
//                        if ( Input.mousePosition.x > Screen.width/2 )
//                                SpawnBottle( BottleDirection.RIGHT );
//                        else
//                                SpawnBottle( BottleDirection.LEFT );
//                
//        }
		
	}
       
        void SpawnBottle( BottleDirection direction )
        {
                //as the prefab is of type BottleMovement, it'll return a BottleMovement
                bottleMovement newBottle = Instantiate(bottles[0],transform.position, Quaternion.identity)as bottleMovement;
               
                //set the side it shall move
                newBottle.direction = direction;
        }
	
	
	 void DoMyWindow2(int windowIDE) {
		// scrollPosition = GUI.BeginScrollView(rect_inv_main_window, scrollPosition, new Rect(Screen.width*5/100, Screen.height*5/100, Screen.width-Screen.width*15/100, Screen.height-Screen.height*15/100));
		
		scrollPosition = GUI.BeginScrollView(new Rect(10,20,rect_inv_main_window.width*0.9f, rect_inv_main_window.height - 20), scrollPosition, new Rect(0, 0, 50,Screen.height));
		//  scrollPosition = GUI.BeginScrollView(new Rect(10,20,rect_inv_main_window.width * 0.5f, rect_inv_main_window.height - 40), new Vector2(scrollPosition.x, MyTouchInput.touchPosition.y), new Rect(0, 0, 50,Screen.height));
		GUI.skin.button = inv_main_window_button;
		
	//		GUI.Button(new Rect(Screen.width*20/100, Screen.height*20/100+10, 20, 20), "x1");
	//		GUI.Button(new Rect(Screen.width*20/100, Screen.height*30/100+10, 20, 20), "x2");
	//		GUI.Button(new Rect(Screen.width*20/100, Screen.height*40/100+10, 20, 20), "x3");
	//		GUI.Button(new Rect(Screen.width*20/100, Screen.height*50/100+10, 20, 20), "x4");
	//		GUI.Button(new Rect(Screen.width*20/100, Screen.height*60/100+10, 20, 20), "x5");
	//		GUI.Button(new Rect(Screen.width*20/100, Screen.height*70/100+10, 20, 20), "x6");
	//		GUI.Button(new Rect(Screen.width*20/100, Screen.height*80/100+10, 20, 20), "x7");
	//		GUI.Button(new Rect(Screen.width*20/100, Screen.height*80/100+10, 20, 20), "x8");
		GUI.Button(new Rect(0, 10, 100, 40), "x1");
        GUI.Button(new Rect(0, 60, 100, 40), "x2");
        GUI.Button(new Rect(0, 110, 100, 40), "x3");
        GUI.Button(new Rect(0, 160, 100, 40), "x4");
        GUI.Button(new Rect(0, 210, 100, 40), "x5");
        GUI.Button(new Rect(0, 260, 100, 40), "x6");
        GUI.Button(new Rect(0, 310, 100, 40), "x7");

		GUI.EndScrollView();
		
		
		// close button
		if (GUI.Button(new Rect(Screen.width*75/100+10, Screen.height*70/100+10, 20, 20), "x")){
			open_Inventory_Window=false;
			Time.timeScale = 1.0f;
            Debug.Log("Clicked the button with text");	
		}
		// 114 - 129 code for toggle
//		GUI.skin.toggle = toggle_style;
//		
//		// step1::  disable it, and only enable it if highscore [total coins] value is greater than 200
//		GUI.enabled=false;
//		Debug.Log(bought_toggle_val_powerup_1_1);
//		if(scoreHandler._highscore>toggle_val_powerup_1_1  bought_toggle_val_powerup_1_1==false){
//		GUI.enabled=true;	
//			if(toggle_val==true){
//				Debug.Log(toggle_val);
//				bought_toggle_val_powerup_1_1=true;
//				GUI.enabled=false;
//				Save_powerup_1_1();
//			}
//		
//		}
//		  toggle_val = GUI.Toggle(new Rect(10, 10, 25, 25),toggle_val, "");
        
    }
	
//		public static void Save_powerup_1_1(){
//PlayerPrefs.SetInt("save_powerup_1_1", bought_toggle_val_powerup_1_1 ? 1 : 0);
//}
	

//	public static void Load_powerup_1_1(){
//		bought_toggle_val_powerup_1_1 = PlayerPrefs.GetInt("save_powerup_1_1")  == 1 ? true : false;
//	}
	
	  public static void SetBool(string Name, bool value)
    {
        PlayerPrefs.SetInt(Name, value ? 1 : 0);
    }

     public static bool GetBool(string Name, bool _default = false)
    {
        if (PlayerPrefs.HasKey(Name))
        {
            return PlayerPrefs.GetInt(Name) == 1;
        }
        return _default;
    }

public static void Save_powerup_1_1(){
SetBool("save_powerup_1_1", bought_toggle_val_powerup_1_1);
}
	
	public static void Load_powerup_1_1(){
		bought_toggle_val_powerup_1_1 = GetBool("save_powerup_1_1");
	}
}

Ok the problem is with Time.timeScale = 0 and GetAxis not working because of that.
Use GetAxisRaw instead.

So I am kind of getting what you are asking for.

Here is what I came up with using just the info from the reference. Using both a Button layout and a Grid layout. (mind you the grid layout is far easier) (press X to see a button window and Y to see a Grid window)

using UnityEngine;
using System.Collections;

public class Scroller : MonoBehaviour {
	
	
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(!ShowWindow1  Input.GetKeyDown(KeyCode.X)) ShowWindow1 = true;
		if(!ShowWindow2  Input.GetKeyDown(KeyCode.Y)) ShowWindow2 = true;
	}
	
	void OnGUI(){
		if(ShowWindow1) windowRect1 = GUI.Window(0, windowRect1, DoMyWindowButtons, "Button Window");
		if(ShowWindow2) windowRect1 = GUI.Window(0, windowRect1, DoMyWindowGrid, "Grid Window");
	}
	
	bool ShowWindow1 = false;
	Rect windowRect1 = new Rect(20, 20, 200, 400);
	Vector2 scrollPosition1 = new Vector2(0,0);
	
	void DoMyWindowButtons(int windowID) {
		int buttonCount = 20;
		Rect position = new Rect(0,20,windowRect1.width-10, windowRect1.height-50);
		Rect space = new Rect(0,0,position.width - 20,buttonCount * 20 + (buttonCount-1)*5 + 10);
		scrollPosition1 = GUI.BeginScrollView(position, scrollPosition1, space);
		//GUI.skin.button = inv_main_window_button;
		
		for(float i=0; i<20; i++){
			if(GUI.Button(new Rect(10, 5 + i*20 + 5 * (i), space.width-10, 20), "Button " + i.ToString())){
				Debug.Log("You clicked button " + i.ToString());
			}
		}
		
		GUI.EndScrollView();
	   
		// close button, keep this out of scroller
		if (GUI.Button(new Rect(5,windowRect1.height-25, windowRect1.width-10, 20), "x")){
			//open_Inventory_Window=false;
			Time.timeScale = 1.0f;
			Debug.Log("Clicked the Button Window Close Button");
			ShowWindow1 = false;
		}
		
		GUI.DragWindow (new Rect(0,0, Screen.width, Screen.height));
	}
	
	bool ShowWindow2 = false;
	int selGridInt = -1;
	string[] selStrings = new string[] {"Grid 1", "Grid 2", "Grid 3", "Grid 4", "Grid 5", "Grid 6", "Grid 7"};
	Rect windowRect2 = new Rect(20, 20, 200, 400);
	Vector2 scrollPosition2 = new Vector2(0,0);
	
	void DoMyWindowGrid(int windowID) {
		int buttonCount = 20;
		Rect position = new Rect(5,20,windowRect2.width-15, windowRect2.height-50);
		Rect space = new Rect(0,0,position.width - 20,buttonCount * 20 + (buttonCount-1)*5 + 10);
		scrollPosition2 = GUI.BeginScrollView(position, scrollPosition2, space);
		
		selGridInt = GUI.SelectionGrid (space, selGridInt, selStrings, 1);
		if(selGridInt>-1){
			Debug.Log("You clicked Grid " + selGridInt.ToString());
		}
		selGridInt=-1;
		GUI.EndScrollView();
		
		// close button, keep this out of scroller
		if (GUI.Button(new Rect(5,windowRect2.height-25, windowRect2.width-10, 20), "x")){
			//open_Inventory_Window=false;
			Time.timeScale = 1.0f;
			Debug.Log("Clicked the Grid Window Close Button");
			ShowWindow2 = false;
		}
		
		GUI.DragWindow (new Rect(0,0, Screen.width, Screen.height));
	}
}

OK, that being said. This whole thing would work better if you had a class to handle the actual entries. You then use a List to handle the actual classes then pull graphics or build them on the fly for your image array (in the grid) You could also use the same theory for the buttons, but I think the grid layout is much better in this case.