Search Unity

Unity UI Unity UI Image touch detects problem...

Discussion in 'UGUI & TextMesh Pro' started by tosturan, Apr 2, 2017.

  1. tosturan

    tosturan

    Joined:
    Apr 15, 2015
    Posts:
    10
    Hello. i am new unity member. i am trying to make a drum kit smilator. ive got 4*3 grid and ive got images streched in it as below...





    when i click on a sprite image ive got a script on it as below

    public AudioSource MyAud;
    public void OnPointerEnter(PointerEventData eventData)
    {
    if (MyAud.isPlaying)
    MyAud.Stop();
    MyAud.Play();
    }

    i attached all this sprite images to script and attached to MyAud variables the sounds of drum kit. on the runtime its working when i press the images the sound is playing. but i feel some thing wrong. i am using some times keyboard battery or other android battery games they are response my touches more quick. in my project touches responses are a little bit delaying. about 0.5 sec. so its not beeing good for catch the ryhtm. is my way wrong , my code something wrong ?. or what is the best way to do this ?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hey there. I wasn't even aware of the fact that OnPointerEnter would even be called on mobile. Neat to learn that.
    If you implement IPointerClickHandler and put your code in that interface's method, does it respond better/faster for you?
    (That's if you want to tap.. if you want it to play just on 'down press', use IPointerDownHandler).
    Edit: This is just a guess. Just curious if it helps :)
     
  3. tosturan

    tosturan

    Joined:
    Apr 15, 2015
    Posts:
    10
    this is my last tried code example. i tried before as you thoug onpointerdown and click. down and click has delay too. but i feel onpointerenter is response my touches more quickly. also i can press my finger and move around without up my finger. its catch all of touches on components. but the problem is delaying. its about 0.5 sec. delay. its not a good experiance for playing a drumkit.i need this answer. maybe my logic is wrong need to find true logic.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, it was worth a shot. Beyond that, I'm not sure what else could be wrong. Hope you find a working solution :)
     
  5. tosturan

    tosturan

    Joined:
    Apr 15, 2015
    Posts:
    10
    Thnx for try your helpes :). if some other guys interest with my problem i want to say that;
    i tried a different way for the solution as below but the result the same. it has the delay too. may it be a cronic problem for unity touches detect ? maybe its work slow.

    at update method;

    if (Input.touchCount > 0)
    {
    for (int i = 0; i < Input.touchCount; i++)
    {
    if (Input.GetTouch(i).phase == TouchPhase.Began)
    {

    PointerEventData pointerData = new PointerEventData(EventSystem.current);

    pointerData.position = Input.touches.position;

    List<RaycastResult> results = new List<RaycastResult>();
    EventSystem.current.RaycastAll(pointerData, results);

    if (results.Count > 0)
    {
    string message = results[0].gameObject.name;
    if (message == "1")//meaning the name wich is "1" sprite image in the canvas....
    {
    if (crash2.isPlaying)
    crash2.Stop();
    crash2.Play();
    } else
    if (message == "2")
    {

    } else
    if (message == "3")
    {
    if (r1.isPlaying)
    r1.Stop();
    r1.Play();
    } else
    if (message == "4")
    {
    if (r1.isPlaying)
    r1.Stop();
    r1.Play();
    } else
    if (message == "5")
    {
    if (hh1.isPlaying)
    hh1.Stop();
    hh1.Play();
    } else
    if (message == "6")
    {
    if (tom1.isPlaying)
    tom1.Stop();
    tom1.Play();
    } else
    if (message == "7")
    {
    if (tom1.isPlaying)
    tom1.Stop();
    tom1.Play();
    } else
    if (message == "8")
    {
    if (tom0.isPlaying)
    tom0.Stop();
    tom0.Play();
    } else
    if (message == "9")
    {
    if (snare1.isPlaying)
    snare1.Stop();
    snare1.Play();
    } else
    if (message == "10")
    {
    if (kick.isPlaying)
    kick.Stop();
    kick.Play();
    } else
    if (message == "11")
    {
    if (kick.isPlaying)
    kick.Stop();
    kick.Play();
    } else
    if (message == "12")
    {
    if (tom0.isPlaying)
    tom0.Stop();
    tom0.Play();
    }
    }
    }
    }
     
  6. tosturan

    tosturan

    Joined:
    Apr 15, 2015
    Posts:
    10
    no one know anything about this issue ?
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hey there. Your post was a while back and hadn't seen any updates with regard to it.
    Can you try one thing for me, add a script like this:
    Code (CSharp):
    1.  
    2. using UnityEngine.EventSystems;
    3. public class TestSound : MonoBehaviour, IPointerClickHandler {
    4.  
    5. // setup audio source/clip
    6.  
    7. public void OnPointerClick(EventSystems.PointerEventData eventData) {
    8. if(tom0.isPlaying) tom0.Stop();
    9. tom0.Play();
    10. }
    11. // where tom0 is whatever you wanna call it.
    12.  
    Some days ago, you told me that you tried this, but when you told me so, I didn't envision you having tried it in an update loop with all that other stuff going on.
    So, I was thinking you can try this and see how it plays
     
  8. tosturan

    tosturan

    Joined:
    Apr 15, 2015
    Posts:
    10
    i had abondon hope from this topic, cause any one didnt interest with it :). so i saw your question yet. i will try and write the effects here. thnx for answer.
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, let me know how it goes.
     
  10. tosturan

    tosturan

    Joined:
    Apr 15, 2015
    Posts:
    10


    how can i know here which gameobject i touched. i need to touch tom0 gameobject for play it.
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You want it to play 1 audio source , but anytime a new audio source is played, you want the previous one to stop, if it's still being played, is that correct?
    If the 'TestSound' class has a static variable for audioSource, what you can do is put this TestSound (or whatever you call it) on every object that you can play as an instrument.
    You then can have an audiosource/clip for each script on each instrument.
    When you tap it, check if the static variable is null, and if it's not check if it's playing and stop it
    then play your current one (you just clicked), and also assign it to the static variable for reference.
    Does that make sense?

    One other thing, did you try the script on just 1 object and did it respond well for you? Before your post was about the responsiveness being poor. So, I'm curious if it's improved, otherwise this is not really relevant lol