using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class DrawingController : MonoBehaviour { public GameObject linePrefab; private LineRenderer currentLineRenderer; private List points = new List(); void Update() { #if UNITY_EDITOR || UNITY_STANDALONE HandleMouseInput(); #elif UNITY_ANDROID || UNITY_IOS HandleTouchInput(); #endif } void HandleMouseInput() { if (Input.GetMouseButtonDown(0)) StartLine(Camera.main.ScreenToWorldPoint(Input.mousePosition)); if (Input.GetMouseButton(0)) ContinueLine(Camera.main.ScreenToWorldPoint(Input.mousePosition)); } void HandleTouchInput() { if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); Vector2 pos = Camera.main.ScreenToWorldPoint(touch.position); if (touch.phase == TouchPhase.Began) StartLine(pos); else if (touch.phase == TouchPhase.Moved) ContinueLine(pos); } } void StartLine(Vector2 startPos) { GameObject newLine = Instantiate(linePrefab); newLine.tag = "Line"; currentLineRenderer = newLine.GetComponent(); currentLineRenderer.startWidth = 20.0f; currentLineRenderer.endWidth = 20.0f; points.Clear(); points.Add(startPos); points.Add(startPos); currentLineRenderer.positionCount = 2; currentLineRenderer.SetPosition(0, startPos); currentLineRenderer.SetPosition(1, startPos); } void ContinueLine(Vector2 pos) { if (Vector2.Distance(pos, points[points.Count - 1]) > 0.05f) { points.Add(pos); currentLineRenderer.positionCount++; currentLineRenderer.SetPosition(currentLineRenderer.positionCount - 1, pos); } } public void ClearDrawing() { foreach (var line in GameObject.FindGameObjectsWithTag("Line")) Destroy(line); } }