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);
    }
}
相关推荐
PiaoKe___18 小时前
云手机技术解析:从 ARM 虚拟化到 Python 自动化实战
python·智能手机·自动化
JoyCong199818 小时前
ToDesk鸿蒙PC端又焕新!远控更稳、操作更准,跨端办公如本地
运维·华为·智能手机·harmonyos·远程工作
科技那些事儿20 小时前
旗舰游戏本 CPU 对决:锐龙 9 9955HX 对比酷睿 Ultra 9 275HX
游戏·智能手机·智能电视
云飞云共享云桌面1 天前
智能装备工厂研发部门多设计师共享一台SolidWorks服务器做设备装配建模
运维·服务器·网络·人工智能·3d·自动化·制造
Yyq130208696821 天前
3D磁编码应用-带屏按钮-KTH5701
3d
小雨爱测1 天前
3ds Max 入门到进阶:建模、材质、灯光与渲染全流程指南
3d·材质
FOORIR 客流统计1 天前
AI 3D客流统计技术拆解:从深度数据到越线计数
人工智能·3d
Jmyd01231 天前
从 0.02 毫米到 8192 像素:线上 3D 展览的数据链路怎么跑通?
3d·三维数字化·3d展览
平行云1 天前
建筑BIM/3D可视化轻量级实时评审与分享方案
unity·ue5·3dsmax·实时云渲染·云桌面·云游戏·建筑bim
OpenVINO 中文社区1 天前
英特尔 × ADG:从 Agent 落地到 3DGS 重建,GDPS 2026 空间计算实战工作坊技术复盘
人工智能·3d·空间计算