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);
    }
}
相关推荐
西红柿炖牛腩35412 小时前
数字孪生大屏场景模型几个 G,Draco 3D 模型压缩和减面该怎么排顺序
3d·大屏端
3D可视化大侠12 小时前
DPE 工程怎么交付到自有系统:GLB 导出与 SDK 集成实践
3d
开开心心就好13 小时前
视频里的图片怎么提取?双击一下就导出
java·前端·人工智能·spring·智能手机·intellij-idea·excel
总要学点什么13 小时前
第47篇:三维特效-雷达波效果——波纹一圈圈往外推,一半却埋在地下
3d·前端框架·gis·webgl·cesium
Shirley~~1 天前
Three.js的基础概念
前端·3d
Zldaisy3d1 天前
GAMS 2026: 增材制造无国界
3d·制造
浦信仿真大讲堂2 天前
用好 CST View Options:把3D操作环境调到更顺手,也更高效
3d·cst软件·达索软件
loulanyue_2 天前
Qwen Intelligence手机智能底座:读阿里副总裁许主洪2026云栖演讲
人工智能·智能手机·移动端
oioihoii2 天前
Pascal Editor 3D建筑编辑:墙体门窗、2D与3D视图及WebGPU建模
3d
千殇华来2 天前
3d打印的材料选择
3d·材料工程