Created
December 8, 2015 09:03
-
-
Save ahmadnaser/71a092f7d91d34c094e9 to your computer and use it in GitHub Desktop.
How to draw a single Line in c# and Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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