Simple tutorial on how to draw lines with mouse in 2D.

Firstly, create new material and name it “Line” (right click in Assets window→Create→Material). Change it’s Shader to Sprites→Default.

After that it should look like this:

Now create new GameObject in Hierarchy and name it “LineCreator” (right click in Hierarchy window and choose Create Empty). Attach “LineCreator” script to it. To do that click Add Component and choose New Script from the list. Name the script “LineCreator”, edit it by clicking twice on it in Assets window or by clicking little gear over it in Inspector window and choosing Edit Script option. Change it, so it looks like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// © 2018 TheFlyingKeyboard and released under MIT License 
// theflyingkeyboard.net 

public class LineCreator : MonoBehaviour {
  [SerializeField] private GameObject line;
  private Vector2 mousePosition;

  private void Update () {
    if (Input.GetMouseButtonDown(0)) //Or use GetKeyDown with key defined with mouse button
    {
      mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
      Instantiate(line, mousePosition, Quaternion.Euler(0.0f, 0.0f, 0.0f));
    }
  }
}

After that create second GameObject and name it “LineDrawer”. Attach LineRenderer to it – click Add Component and type in the search window “Line Renderer”, choose it and change it’s properties to match the following:

Remember to fill the Materials list field with material that we created. After that add new script to it, named “LineDrawer”:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// © 2018 TheFlyingKeyboard and released under MIT License 
// theflyingkeyboard.net 

public class LineDrawer : MonoBehaviour {
  private LineRenderer line;
  private Vector2 mousePosition;

  [SerializeField] private bool simplifyLine = false;
  [SerializeField] private float simplifyTolerance = 0.02f;

  private void Start () {
    line = GetComponent<LineRenderer>();
  }

  private void Update () {
    if (Input.GetMouseButton(0)) //Or use GetKey with key defined with mouse button
    {
      mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

      line.positionCount++;
      line.SetPosition(line.positionCount - 1, mousePosition);
    }

    if (Input.GetMouseButtonUp(0))
    {
      if (simplifyLine)
      {
        line.Simplify(simplifyTolerance);
      }

      enabled = false; //Making this script stop
    }
  }
}

Note that, the higher the “simplifyTolerance” is the less points the line will have. Next step is to save the “LineDrawer” GameObject as a prefab (drag it from Hierarchy window to Assets window, it should change font color to blue in the Hierarchy) and delete it from scene. After the object was saved fill the Line field in the “LineCreator” GameObject in the Hierarchy using the prefab that was created. 

Everything should be working now.

Thank you for reading 🙂.

Unity 2D C# Drawing Lines With Mouse
Tagged on:         

Leave a Reply

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