Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 RichCoggin · Jul 22, 2013 at 01:55 PM · guimovementtouch

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

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

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.

Comment
Add comment · Show 6 · 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 RichCoggin · Jul 23, 2013 at 11:29 AM 0
Share

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

avatar image Jamora · Jul 23, 2013 at 11:48 AM 0
Share

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.

avatar image RichCoggin · Jul 23, 2013 at 02:29 PM 0
Share

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

avatar image Jamora · Jul 23, 2013 at 02:53 PM 0
Share

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#.

avatar image RichCoggin · Jul 23, 2013 at 03:18 PM 0
Share

Thanks! You've been a great help.

Show more comments

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

16 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

Related Questions

Gradually decrease camera pan speed when touch has ended? (Touch input) 0 Answers

Assigning special Keyboard to GUI.TextField 0 Answers

How to touch, move and rotate 3D objects in XZ? 1 Answer

Hello, I'm having a problem with the character's touch, when playing to jump it is falling and leaving the scene, What should I do? 0 Answers

How to Convert each 2D Array to GameObject 2 Answers


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