I had some trouble figuring out the unity event system and how to add touch in unity when developing where’s the line. I stumbled upon lot of tutorials but they seemed outdated. Hence adding one incase it helps someone else.

If you are interested in looking at my game here are the app store links

I also wrote a blog post on the game development and planning process if you are interested.

Version

2019.2.17f1

Setup

I am assuming you have the following setup on your project

  1. A scene
  2. Camera with “physics 2D raycaster” component attached
  3. A game object (dot in this case) which will react to drag or touch.
  4. For this tutorial I have created a game object which has line renderer as another component. Hence every time you initiate the drag event, a new line is drawn from the dot to the mouse or touch position

Tutorial

Adding Event system to the scene

  1. First add a new game object and name it “EventSystemGameObject”
  2. Add an “Event System” component to the EventSystemGameObject
  3. Click on “Add Default Input Module”

At the end of this step this is how my EventSystemGameObject looks

Adding Event Trigger to the game object

  1. Select the game object on which you want to add the drag event
  2. You need to add some sort of collider on the game object. For this tutorial I have added “Circle collider 2d”
  3. Make sure to select “Is Trigger” option
  4. Add a new script (behavior) to the project and attach it to the game object. Add the following code to the behavior of the game object

Note: My behavior also had reference to another object which was the “line” prefab I created.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DotBehaviour : MonoBehaviour
{
    /// <summary>
    /// Holds the reference to line renderer prefab
    /// </summary>
    public GameObject lineRendererPrefab;
    /// <summary>
    /// After the line is drawn add it to this list so it does not disappear
    /// </summary>
    private List<LineRenderer> lineRenderers = new List<LineRenderer>();
    /// <summary>
    /// reference to current line renderer that is created when drag begins
    /// </summary>
    private GameObject currentLineRenderer;
    private int lineCount = 0;
    /// <summary>
    /// Callback function on DragBegin
    /// </summary>
    /// <param name="eventData"> Event data passed</param>
    public void OnDragBegin(PointerEventData eventData)
    {
        if (currentLineRenderer != null)
        {
            throw new InvalidProgramException("line renderer is already active");
        }
        currentLineRenderer = Instantiate(lineRendererPrefab, this.transform);
        currentLineRenderer.name = "line_" + lineCount;
        currentLineRenderer.GetComponent<LineRenderer>().SetPosition(0, this.transform.position);
        ChangeEndPosition(this.transform.position);
    }
    /// <summary>
    /// Callback function on Drag continuation
    /// </summary>
    /// <param name="pointerEventData"></param>
    public void OnDrag(PointerEventData pointerEventData)
    {
        if (currentLineRenderer == null)
        {
            throw new InvalidProgramException("currentLineRenderer cannot be null");
        }
        // change the position of the end of the line
        ChangeEndPosition(Camera.main.ScreenToWorldPoint(pointerEventData.position));
    }
    /// <summary>
    /// Callback function on Drag End
    /// </summary>
    /// <param name="pointerEventData"></param>
    public void OnDragEnd(PointerEventData pointerEventData)
    {
        if (currentLineRenderer == null)
        {
            throw new InvalidProgramException("currentLineRenderer cannot be null");
        }
        // change the position of the end of the line
        ChangeEndPosition(Camera.main.ScreenToWorldPoint(pointerEventData.position));
        lineRenderers.Add(currentLineRenderer.GetComponent<LineRenderer>());
        currentLineRenderer = null;
        ++lineCount;
    }
    private void ChangeEndPosition(Vector3 newPosition)
    {
        // change the position of the end of the line
        currentLineRenderer.GetComponent<LineRenderer>().SetPosition(1, newPosition);
    }
}
  1. Add “Event Trigger” Component.
  2. Click on “Add New Event Type” and add “Begin Drag”, “Drag” and “End Drag”.
  3. Click on “+” to add a new listener on “Begin Drag”
  4. Add the gameobject as reference and add the select the “listener” function “OnBeginDrag”
  5. For each of the functions, add the game object reference and select the drag functions defined above. The game object inspector look as below

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>