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);
    }
}
相关推荐
HH‘HH10 小时前
Unity引擎界面各个功能面板详细介绍
unity·游戏引擎
℡枫叶℡11 小时前
Unity 2D资产命名常用缩写
unity·资产命名缩写
Bobolink_11 小时前
手机端和电脑端的网络环境配置有什么不同?
网络·智能手机·电脑·网络环境
3D小将12 小时前
3D格式转换之SOLIDWORKS 转 CATIA 标准化转换技术
3d·solidworks模型·rhino模型·sketchup模型·igs模型
数字孪生家族13 小时前
3DGS 数据编辑单体化技术:将高斯重建转化为结构化数字空间资产
数据库·3d·高斯泼溅·空间智能应用
智塑未来14 小时前
数字人3D角色怎么生成?用V2Fun从形象设定做到可动原型
3d
丁小未14 小时前
Unity AssetBundle商业化项目资源方案
unity·游戏引擎·assetbundle·unity框架
苏州邦恩精密17 小时前
蔡司3D扫描仪厂家服务解析:制造企业如何实现精准检测
人工智能·机器学习·3d·自动化·制造
SHARE赛尔17 小时前
从点云复核到 Scan To CAD:赛尔 SHARE3DCAM 建博会现场工作流回顾
3d
极客猴子17 小时前
苹果手机怎么快速总结长文本?iOS录音APP实测对比
ios·智能手机