- Home /
Touch and slide GUI box & grid selected buttons cSharp
Hey there,
I've created a menu with a GUI.box and and grid selected menu. What I want to do is be able to touch and slide this menu out from the side. It only needs to move 100 pixels, so would have a clamp? on it I think.
I've looked at loads of posts and can't quite get my head around it as they mostly focus on object movement rather than gui movement.
Here's my gui code for the menu so far.
using UnityEngine;
using System.Collections;
public class SelectionGridTest : MonoBehaviour
{
public GUISkin leftnav;
public int selGridInt = 0;
public string[] selStrings = new string[] {"Grid 1", "Grid 2", "Grid 3", "Grid 4", "Grid 5", "Grid 6", "Grid 7", "Grid 8"};
//Instansiate object references
public GameObject TheObject;
void OnGUI()
{
GUI.skin = leftnav;
GUI.Box(new Rect(-90,14,100,320),"");
selGridInt = GUI.SelectionGrid(new Rect(40, 40, 70, 800), selGridInt, selStrings, 1);
if (selGridInt == 0)
{
GameObject objTheObject = (GameObject)Instantiate(TheObject, new Vector3(0,0,0), transform.rotation);
}
else if (selGridInt == 1)
{
// execute code for loading level //////Application.LoadLevel(1);
}
else if (selGridInt == 2)
{
// execute code for destroying tag
Destroy (GameObject.FindWithTag("TheObject"));
}
}
}
Any help would be really appreciated.
Rich
Answer by Jamora · Jul 23, 2013 at 12:27 AM
This works for a basic draggable menu:
private readonly int startPos = -90;
private readonly int endPos = 0;
private readonly int boxRectWidth = 100;
private Rect boxRect = new Rect(-90,14,100,320);
public GUISkin leftnav;
public int selGridInt = -1;
public string[] selStrings = new string[] {"Grid 1", "Grid 2", "Grid 3", "Grid 4", "Grid 5", "Grid 6", "Grid 7", "Grid 8"};
//Instansiate object references
public GameObject TheObject;
void OnGUI() {
GUI.skin = leftnav;
GUI.BeginGroup(boxRect, "","Box");
if(Input.GetMouseButtonDown(0) && boxRect.Contains(Input.mousePosition))
StartCoroutine(MoveMenuRect());
selGridInt = GUI.SelectionGrid(new Rect(0, 0, 70, 800), selGridInt, selStrings, 1);
GUI.EndGroup();
}
IEnumerator MoveMenuRect(){
Vector3 mousePos = Input.mousePosition;
while(Input.GetMouseButton(0)){
boxRect.xMin = Mathf.Clamp(boxRect.xMin-mousePos.x+Input.mousePosition.x,startPos,endPos);
boxRect.xMax = boxRect.xMin+boxRectWidth;
mousePos = Input.mousePosition;
yield return null;
}
}
I removed the if construct because it's not relevant to this behavior. I used Input.mousePosition, which has the y inverted, but if you are using touches, then that shouldn't be a problem, as you can get the correct position from them.
Thanks for looking at this. I replaced my code with your code, but 2 things:
The buttons from the selection grid don't appear at run time When I run on device, the panel doesn't swipe not sure where to put the code below for the actions
Do I need to add anything else to get this to work?
if (selGridInt == 0)
{
GameObject objTheObject = (GameObject)Instantiate(TheObject, new Vector3(0,0,0), transform.rotation);
}
else if (selGridInt == 1)
{
// execute code for loading level //////Application.LoadLevel(1);
}
Thanks again,
Rich
It all works on my PC.
I don't have my Unity setup to test the code on my mobile, but I would suspect it doesn't register touches because I use mouse input ins$$anonymous$$d of Touches. It could also be that the draggable area is too close to the edge of the screen so your screen doesn't register touches there. You could try making the initially visible area bigger.
You can put the code for the actions anywhere after the SelectionGrid, but immediately after is preferred, for logical and readability reasons.
Hey there,
THanks for you help on this. I have played with the numbers a bit and got the thing working (haven't tried 'touch yet'). Is there a way to reverse the placement of the menu. I wanted to be to the left first and then pull out to the right; so when launched, the start position is -90. Here's the amended code:
using UnityEngine;
using System.Collections;
public class LeftNavScript1 : $$anonymous$$onoBehaviour {
private readonly int startPos = 0;
private readonly int endPos = -90;
private readonly int boxRectWidth = 130;
private Rect boxRect = new Rect(0,14,100,800);
public GUISkin leftnav;
public int selGridInt = -1;
public string[] selStrings = new string[] {"Grid 1", "Grid 2", "Grid 3", "Grid 4", "Grid 5", "Grid 6", "Grid 7", "Grid 8", "Grid 9"};
//Instansiate object references
public GameObject TheObject;
void OnGUI() {
GUI.skin = leftnav;
GUI.BeginGroup(boxRect, "","Box");
if(Input.Get$$anonymous$$ouseButtonDown(0) && boxRect.Contains(Input.mousePosition))
StartCoroutine($$anonymous$$ove$$anonymous$$enuRect());
selGridInt = GUI.SelectionGrid(new Rect(14, 25, 100, 800), selGridInt, selStrings, 1);
GUI.EndGroup();
}
IEnumerator $$anonymous$$ove$$anonymous$$enuRect(){
Vector3 mousePos = Input.mousePosition;
while(Input.Get$$anonymous$$ouseButton(0)){
boxRect.x$$anonymous$$in = $$anonymous$$athf.Clamp(boxRect.x$$anonymous$$in-mousePos.x+Input.mousePosition.x,endPos,startPos);
boxRect.x$$anonymous$$ax = boxRect.x$$anonymous$$in+boxRectWidth;
mousePos = Input.mousePosition;
yield return null;
}
}
}
Also - which may be another topic, I wanted the menu to move on its own after a few seconds back to -90. Possible?
Thanks as always...
Rich
private Rect boxRect = new Rect(0,14,100,800); deter$$anonymous$$es the starting position of the draggable menu. You could initialize the Rect in Awake with startPos and endPos so you'll only need to change startPos and endPos to change the starting position.
It is possible to have it move automatically; you need a timer that starts when the menu is dragged far enough. After the time is up, you start a coroutine that Lerps the boxRect.x from the current position to startPos. I'll let you figure out how to translate that into C#.