Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Ghislo · Dec 04, 2015 at 10:46 AM · unity5touchdragscrollview

Handling drag of an UI element in a scroll view over the whole canvas

I'm trying to create a game for mobile devices. I have to say i searched a lot through the net and found a lot of things, but they were all confusing and different one from the other, so i tried myself to write a sort of code from what i learned.

I want to create an inventory that is stored in the content of a scrollView, the touch event of the scrollview is actually working perfectly, but now i want to handle also the touch & drag for elements inside it. NB. there are two images composing a single element, the slot and the item, i want to drag only the item.

here is a pic to make it clearer

![alt text][1] [1]: /storage/temp/59282-q-itslot.png

I initially created a script and added to the single item element trying to use the box collider to check my touch, i learned then that i needed to convert the touch from the screen to the space with the raycast and check if the ray actually hit the layer of the object i want to drag, so i tried with the layer and came up with a code like this :

 using UnityEngine;
 using System.Collections;
 
 public class ItemTouchHandler : MonoBehaviour {
 
     public Transform itemT;
    // Collider itemcollider;
     LayerMask itemlayer;
 
     // Use this for initialization
     void Start () {
      //   itemcollider = itemT.GetComponent<Collider>() as Collider;
         itemlayer = itemT.GetComponent <LayerMask>();
     }
     
     // Update is called once per frame
     void Update () {
       
         
         if (Input.touchCount > 0)
         {
             Ray ray = GetComponent<Camera>().ScreenPointToRay(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0));
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, Mathf.Infinity, itemlayer ))
             {
              //   if (itemcollider.bounds.Contains(Input.GetTouch(0).position))
               //  {
 
                     if (Input.GetTouch(0).phase == TouchPhase.Began)
                     {
                         Debug.Log("Touch Began");
                     }
                     else if (Input.GetTouch(0).phase == TouchPhase.Moved)
                     {
                         Debug.Log("Touch Moved");
                     }
                     else if (Input.GetTouch(0).phase == TouchPhase.Ended)
                     {
                         Debug.Log("Touch Ended");
                     }
 
                // }
             }
         }
 
     }
 }
 

but i get the error that is shown down below in the image i posted before.

Now, how should i modify the code to make the item move when i touch & drag it? and i also thought that when the touch begin is more than a time && hit the item element, I should disable the scrollmovement handler and enable the itemdrag one, but i'm stuck before that. I think i'm missing something important about the layer and the raycasting and that the scrollview touch handler is somehow making my touch&drag not work.

Oh i forgot! i want to be able to drag the item also outside the content, the scrollview and also outside the panel, actually i want to be able to drag it in the whole canvas and in future to check if it's been dragged in particular panels

Thank you for your answers!

Help me please!!

q-itslot.png (176.7 kB)
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
0

Answer by GameDevBryant · Dec 28, 2016 at 06:02 PM

@Ghislo, I know this is about a year late but the "LayerMask" you are referring to is not a component, that is related to collision/lighting layers set on objects. In order to set a LayerMask in code, you have to convert it to an int and bit shift it like this:

 itemlayer = 1 << (int)LayerMask.NameToLayer("YOUR_LAYER_NAME_HERE");

Then you pass 'itemLayer' into your raycast just like you are doing and it should only hit the objects that are set on the "YOUR_LAYER_NAME_HERE" layer (be sure this is named exactly like what is set in your TagManager!)

Hope this helps you, or someone with a similar issue.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Touch Controls, Drag and DoubleTap 1 Answer

unity2D touch drag 0 Answers

Detecting onDrag over UI images 1 Answer

Multitouch drag object 0 Answers

How can i implement a scroll speed for a scroll view/rect so that my content travels slower when I drag? 1 Answer


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