Skip to content

Instantly share code, notes, and snippets.

@ahmadnaser
Created December 8, 2015 09:03
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ahmadnaser/71a092f7d91d34c094e9 to your computer and use it in GitHub Desktop.
How to draw a single Line in c# and Unity
using UnityEngine;
using System.Collections;
public class SingleLine : MonoBehaviour {
public GameObject circlePointPrefab;
public GameObject currentLineRenderer;
public GameObject lineRendererPrefab;
public Material drawingMaterial;
private Vector3 previousPosition,currentPostion;
private bool clickStarted;
private int numberOfPoints;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(currentLineRenderer==null){
currentLineRenderer=(GameObject)Instantiate(lineRendererPrefab);
}
Vector3 currentPosition=Camera.main.ScreenToWorldPoint(Input.mousePosition);
currentPosition.z=-5.0f;
if (Input.GetMouseButtonDown (0)) {
InstatiateCirclePoint(currentPosition,currentLineRenderer.transform);
clickStarted=true;
}else if(clickStarted){
TouchSpaceHandle(currentPosition,currentLineRenderer);
}
if (Input.GetMouseButtonUp (0)) {
clickStarted=false;
}
}
private void TouchSpaceHandle(Vector3 pos,GameObject currentLineRendererGO){
LineRenderer currentLineRenderer = currentLineRendererGO.GetComponent<LineRenderer> ();
numberOfPoints++;
currentLineRenderer.SetVertexCount (numberOfPoints);
currentPostion = pos;
currentLineRenderer.SetPosition (numberOfPoints-1,currentPostion);
}
private void InstatiateCirclePoint(Vector3 pos,Transform parent){
GameObject currentCircle=(GameObject)Instantiate(circlePointPrefab);
currentCircle.transform.parent = parent;
currentCircle.GetComponent<Renderer> ().material = drawingMaterial;
currentCircle.transform.position = pos;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment