using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawLineByMouse : MonoBehaviour {
//画线组件预制体
public Transform gestureOnScreenPrefab;
private int strokeId = -1;
private Vector3 virtualKeyPosition = Vector2.zero;
private Rect drawArea;
private RuntimePlatform platform;
private int vertexCount = 0;
private List<LineRenderer> gestureLinesRenderer = new List<LineRenderer>();
private LineRenderer currentGestureLineRenderer;
//GUI
private string message="what is this ?";
private bool recognized;
private string newGestureName = "";
void Start()
{
platform = Application.platform;
drawArea = new Rect(0, 0, Screen.width - Screen.width / 3, Screen.height);
}
void Update()
{
if (platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer)
{
if (Input.touchCount > 0)
{
virtualKeyPosition = new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y);
}
}
else
{
if (Input.GetMouseButton(0))
{
virtualKeyPosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y);
}
}
if (drawArea.Contains(virtualKeyPosition))
{
if (Input.GetMouseButtonDown(0))
{
++strokeId;
// Transform tmpGesture = Instantiate(gestureOnScreenPrefab, transform.position, transform.rotation) as Transform;
// currentGestureLineRenderer = tmpGesture.GetComponent<LineRenderer>();
GameObject go = new GameObject("LineRenderer");
go.transform.position = Camera.main.transform.position;
go.transform.rotation = Camera.main.transform.rotation;
currentGestureLineRenderer = go.AddComponent<LineRenderer>();
currentGestureLineRenderer.startWidth = 0.1f;
gestureLinesRenderer.Add(currentGestureLineRenderer);
vertexCount = 0;
}
if (Input.GetMouseButton(0))
{
currentGestureLineRenderer.SetVertexCount(++vertexCount);
currentGestureLineRenderer.SetPosition(vertexCount - 1, Camera.main.ScreenToWorldPoint(new Vector3(virtualKeyPosition.x, virtualKeyPosition.y, 10)));
}
}
}
void OnGUI()
{
GUI.Box(drawArea, "Draw Area");
GUI.Label(new Rect(10, Screen.height - 40, 500, 50), message);
if (GUI.Button(new Rect(Screen.width - 100, 10, 100, 30), "clear line"))
{
foreach (LineRenderer lineRenderer in gestureLinesRenderer)
{
lineRenderer.SetVertexCount(0);
Destroy(lineRenderer.gameObject);
}
gestureLinesRenderer.Clear();
recognized = true;
}
}
}