- Home /
Scroll view using Mesh
How to make a scroll view like GUI Scroll view GUI.BeginScrollView using mesh ?
For example in GUI Scroll view we can scroll the other GUI components. How to do same thing using thing to scroll other objects like plane, Quad ?
yes, scroll view with objects like plane , Quad etc..
Answer by LijuDeveloper · Nov 29, 2013 at 11:19 AM
I got solution .
Use This scripts in ScrollButton (Mesh)
Script
using UnityEngine;
using System.Collections;
public class ScrollController : MonoBehaviour
{
public GameObject go_scrollContentObj;
public Camera scrollViewCam;
private float mouseXpos;
private float mouseYpos;
private float scrollPos ;
public float mouseScrollPos = -2.5f ;
public float scrollViewSpeed = 3f;
public float maxScrollHeight= 1.5f;
public float minScrollHeight= -1.5f;
private Vector3 screenPoint;
private Vector3 offset;
private Vector3 scrollScreenPoint;
private Vector3 scrollOffset;
private float screenHeight;
private float screenWidth;
private bool boolMouseDrag;
private bool boolMouseDown;
public Vector3 mousePosInScreen;
void Start()
{
transform.position =new Vector3 (transform.position.x,maxScrollHeight - transform.position.x,transform.position.z);// scrollButtonStartPosition;
}
void OnMouseDown()
{
boolMouseDown = true;
screenPoint = Camera.main.WorldToScreenPoint(transform.position );
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(screenPoint.x, Input.mousePosition.y, screenPoint.z));//new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void Update()
{
screenHeight = Screen.height;
screenWidth = Screen.width;
mousePosInScreen =scrollViewCam.ScreenToViewportPoint(Input.mousePosition) ;
if( transform.position .y >= maxScrollHeight )
{
transform.position = new Vector3 (transform.position.x,maxScrollHeight - 0.01f,transform.position.z);
}
else if( transform.position .y <= minScrollHeight )
{
transform.position = new Vector3 (transform.position.x,minScrollHeight + 0.01f,transform.position.z);
}
go_scrollContentObj.transform.position = new Vector3(go_scrollContentObj.transform.position.x,-transform.position.y * scrollViewSpeed ,go_scrollContentObj.transform.position.z);
scrollPos = Mathf.Clamp (scrollPos, minScrollHeight ,maxScrollHeight );
if( mousePosInScreen.x >= 0 && mousePosInScreen.x <= 1 && mousePosInScreen.y >=0 && mousePosInScreen.y <= 1)
{
if(boolMouseDown == false && boolMouseDrag == false )
{
mouseScrollPos = transform.position.y;
mouseScrollPos =mouseScrollPos + Input.GetAxis("Mouse ScrollWheel");
mouseScrollPos = Mathf.Clamp (mouseScrollPos, minScrollHeight ,maxScrollHeight );
transform.position = new Vector3 (transform.position.x ,mouseScrollPos ,transform.position.z);
}
}
if(Input.GetMouseButtonUp(0))
{
boolMouseDown = false;
boolMouseDrag =false;
}
}
void OnMouseDrag()
{
boolMouseDrag = true;
Vector3 curScreenPoint = new Vector3(screenPoint.x, Input.mousePosition.y, screenPoint.z);//
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
I developed this code from Crazydadz's idea
Answer by Crazydadz · Nov 07, 2013 at 04:13 AM
A simple way is to use 3 planes. One fixed plane covering the top of the screen, one fixed plane covering the bottom of the screen and one in the middle. Place the middle one a little bit behind the 2 others. This plane will be the scrollable mesh. To scroll it, use an empty fixed gameobject with a collider on it with a script like so:
public Transform ScrollingObject; // Put the middle plane in the inspector in here
private void Update()
{
// Cast a raycast to detect if your mouse is over the empty fixed GameObject collider with Physics.Raycast and Camera.ScreenPointToRay(Input.mousePosition)
_scrollPos += Input.GetAxis("Mouse ScrollWheel") * Speed;
_scrollPos = Mathf.Clamp(_scrollPos, MinPositionToScroll, MaxPositionToScroll);
ScrollingObject.position = new Vector3(0f, _scrollPos, ScrollingObject.position.z);
}
That is just an idea. I wrote it on the fly. I know the idea is working because I used it recently in my last Game. Unfortunately, with this idea, you need to cover all the top and the bottom of the screen, ifnot you see the middle scrollable plane behind the top and bottom plan and it is ugly.
if you need more info to accomplish this, just ask. I ll answer you tomorrow at work.
Ive done some search to avoid creating a scrollview covering all the screen. Check this. I can't test something right now but taking this idea with $$anonymous$$e can create more nice looking scrollview with meshes.