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);
    }
}
相关推荐
小贺儿开发13 小时前
Unity3D 串口通信上位机联调系统
unity·串口·协议·数据·通信·传输·互动
草木深雨纷纷16 小时前
星露谷物语整合包手机版下载2026最新版下载分享(里奇赛德村+东斯卡普|拖拉机|自动化+主题美化+功能+汉化+SVE)
游戏·智能手机·游戏程序
草木深雨纷纷17 小时前
mt管理器手机版下载2026最新版更新下载分享
linux·运维·网络·智能手机
wanhengidc21 小时前
显卡服务器都有哪些功能
运维·服务器·人工智能·科技·智能手机·云计算
元让_vincent21 小时前
论文 Review:Trick-GS | ICASSP 2025 | 面向端侧部署的高效 3D Gaussian Splatting “技巧组合包”
3d·性能提升·3dgs
2601_954706491 天前
2026 云手机横评:傲晨云 VS 红手指 VS 雷电云,稳定性 / 性能 / 适配深度测评
大数据·智能手机
2601_954706491 天前
2026 云手机深度测评:傲晨、六边云、蜂窝云谁更适合长期挂机与多开运营?
智能手机
RoboWizard1 天前
DIY移动硬盘?2230能否堪大任!
数据库·人工智能·智能手机·性能优化·负载均衡
一路向北he1 天前
如何获取手机唤醒AI的提示音
智能手机
wanhengidc1 天前
云手机与正常手机的区别
大数据·运维·服务器·人工智能·智能手机