EnderNicky
EnderNicky

Reputation: 322

Unity 5: How can I detect a touch on a custom button and object

I am trying to make a game for Android and iPhone where if the player clicks a 3d chest, he will recieve some items. I have done the item giving, but I cannot detect a touch properly. Also it has to have a support for multiple touches, so the payer can keep pressing the move button, while opening the chest.

I have not done neither the detect on button touch, nor the detect on object touch?

Can each of them have their own script or will they be all in one script with if statements?

Upvotes: 0

Views: 1097

Answers (1)

Berke Cagkan Toptas
Berke Cagkan Toptas

Reputation: 1034

You can implement IPointerClickHandler interface to your chest object script something like that :

class ChestObject : MonoBehaviour, IPointerClickHandler
{
    // Your other functions ...

    void OnPointerClick(PointerEventData eventData)
    {
        //The program will run this function when you click the chest
    }
}

Depends on your needs, you can also implement IPointerDownHandler, IPointerUpHandler, IDragHandler, IDropHandler etc.. These interfaces came with the new UI system and they are quite useful.

Upvotes: 3

Related Questions