Unity 3D 电脑端和手机端都实现画线与清除功能

cs 复制代码
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class TouchDraw : MonoBehaviour
{
    [Header("绘制参数")]
    public float lineWidth = 0.15f;
    public float minDrawDistance = 0.05f;

    [Header("UI按钮")]
    public Button btnClear;
    public Button btnRed;
    public Button btnGreen;
    public Button btnBlue;

    private Color nowColor;
    private LineRenderer curLine;
    private List<Vector3> pointList = new List<Vector3>();
    private List<GameObject> allLineObjects = new List<GameObject>();
    private Material lineMat;

    void Start()
    {
        // 初始化材质,解决颜色异常
        lineMat = new Material(Shader.Find("Sprites/Default"));
        nowColor = Color.red;
        lineMat.color = nowColor;

        // 按钮绑定
        if (btnClear != null) btnClear.onClick.AddListener(ClearAllDraw);
        if (btnRed != null) btnRed.onClick.AddListener(() => SetColor(Color.red));
        if (btnGreen != null) btnGreen.onClick.AddListener(() => SetColor(Color.green));
        if (btnBlue != null) btnBlue.onClick.AddListener(() => SetColor(Color.blue));
    }

    void Update()
    {
        // 手机触摸判断UI
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
            {
                EndDraw();
                return;
            }

            if (touch.phase == TouchPhase.Began)
                BeginDraw();
            else if (touch.phase == TouchPhase.Moved)
                OnDrawing();
            else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
                EndDraw();
        }
        // 电脑鼠标判断UI
        else
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                EndDraw();
                return;
            }

            if (Input.GetMouseButtonDown(0))
                BeginDraw();
            else if (Input.GetMouseButton(0))
                OnDrawing();
            else if (Input.GetMouseButtonUp(0))
                EndDraw();
        }
    }

    void SetColor(Color color)
    {
        nowColor = color;
        lineMat.color = nowColor;
    }

    void BeginDraw()
    {
        pointList.Clear();
        GameObject lineGo = new GameObject("DrawLine");
        curLine = lineGo.AddComponent<LineRenderer>();

        curLine.useWorldSpace = true;
        curLine.startWidth = lineWidth;
        curLine.endWidth = lineWidth;
        curLine.positionCount = 0;
        curLine.material = lineMat;

        allLineObjects.Add(lineGo);
    }

    void OnDrawing()
    {
        if (curLine == null) return;

        Vector3 screenPos = Input.mousePosition;
        screenPos.z = 10f;
        Vector3 worldPos = Camera.main.ScreenToWorldPoint(screenPos);

        if (pointList.Count > 0 && Vector3.Distance(worldPos, pointList[pointList.Count - 1]) < minDrawDistance)
            return;

        pointList.Add(worldPos);
        curLine.positionCount = pointList.Count;
        curLine.SetPosition(pointList.Count - 1, worldPos);
    }

    void EndDraw()
    {
        curLine = null;
    }

    public void ClearAllDraw()
    {
        foreach (var go in allLineObjects)
        {
            if (go != null) Destroy(go);
        }
        allLineObjects.Clear();
        pointList.Clear();
        curLine = null;
    }

    private void OnDestroy()
    {
        if (lineMat != null) Destroy(lineMat);
    }
}
相关推荐
wulechun10 天前
打造你的专属机器宠物:Py-Apple低成本四足机器人开源项目深度解析与全流程DIY实战指南
智能手机
2601_9547064910 天前
云手机技术详解+Python实战调用|2026高稳云手机平台推荐
开发语言·python·智能手机
百度搜知知学社10 天前
贝格手机罗盘2.8版:精准导航与功能升级全解析
智能手机·功能升级·手机罗盘·导航应用·版本解析
xsc-xyc10 天前
用 Tailscale + Syncthing 实现手机、电脑与 NAS 的跨网络文件同步
linux·网络·网络安全·智能手机·电脑
叶帆10 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
探物 AI10 天前
【3D·感知】从PointNet到PointPillars:如何让自动驾驶汽车“实时“看见3D世界?
3d·自动驾驶·汽车
wulechun10 天前
打造全栈人工智能知识图谱:深入解析Ai-Learn开源学习路线与实战资源导航指南
智能手机
久数君10 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型
苏州邦恩精密10 天前
GOM三维扫描在制造中的真实价值:让“修模”从经验动作变成数据动作
人工智能·科技·机器学习·3d·自动化·制造
YHHLAI10 天前
CSS 3D 硬核解析:四个属性手写旋转立方体
前端·css·3d