Unity点击生成节点连线

Unity点击生成节点连线

  1. 效果
    在这里插入图片描述
    2.主要代码
    Test_Line 控制类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class Test_Line : MonoBehaviour
{
    public GameObject qiu_prefab;
    public List<GameObject> spheres;
    public bool istrue = true;
    public GameObject qr_im;
    private void Update()
    {
        if (Input.GetMouseButtonDown(0) && istrue && !EventSystem.current.IsPointerOverGameObject())
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                GameObject obj = hit.collider.gameObject;
                if (spheres.Count > 2)
                {
                    if (obj == spheres[0])
                    {
                        qr_im.SetActive(true);
                        Debug.Log("是否闭合");
                    }
                }
                if (obj.layer != LayerMask.NameToLayer("JieDian"))
                {
                    GameObject jiedian = Instantiate(qiu_prefab, hit.point, new Quaternion());
                    spheres.Add(jiedian);
                    if (spheres.Count > 1)
                    {
                        spheres[spheres.Count - 2].GetComponent<DragObjMove>().nextObj = spheres[spheres.Count - 1];
                        spheres[spheres.Count - 2].GetComponent<DragObjMove>().StartTest();
                    }
                }
            }
        }
    }
    public void Bihe()
    {
        istrue = false;
        spheres[spheres.Count - 1].GetComponent<DragObjMove>().nextObj = spheres[0];
        spheres[spheres.Count - 1].GetComponent<DragObjMove>().StartTest();
    }
}

DragObjMove节点控制类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragObjMove : MonoBehaviour
{
    private Vector3 offset;
    public GameObject nextObj;

    private LineRenderer lineRenderer;
    private Transform startPoint;
    private Transform endPoint;
    public Material[] materials;
    public void StartTest()
    {
        if (nextObj != null)
        {
            startPoint = transform;
            endPoint = nextObj.transform;
            lineRenderer = GetComponent<LineRenderer>();
            lineRenderer.positionCount = 2;
        }
    }
    private void Update()
    {
        // 确保起点和终点已赋值且LineRenderer组件存在
        if (nextObj != null && lineRenderer != null)
        {
            lineRenderer.SetPosition(0, startPoint.position); // 设置线段的起始位置
            lineRenderer.SetPosition(1, endPoint.position);   // 设置线段的结束位置
        }
    }

    private void OnMouseEnter()
    {
        transform.GetComponent<MeshRenderer>().material = materials[1];

    }
    private void OnMouseDown()
    {
        offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(transform.position).z));
    }
    private void OnMouseDrag()
    {
        Vector3 newPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.WorldToScreenPoint(transform.position).z)) + offset;
        transform.position = newPosition;
    }
    private void OnMouseExit()
    {
        transform.GetComponent<MeshRenderer>().material = materials[0];
    }
}

  1. 资源包下载链接:下载地址
  2. 补充:导入资源包后,需将节点小球预制体的Layer改成“JieDian”。

相关推荐

  1. vue3 防止按钮的连续

    2024-07-20 04:12:02       23 阅读
  2. Unity 无效的问题

    2024-07-20 04:12:02       26 阅读
  3. unity防止ui事件被子物体拦截

    2024-07-20 04:12:02       36 阅读
  4. Unity基于EventSystem让SpriteRenderer支持事件

    2024-07-20 04:12:02       30 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 04:12:02       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 04:12:02       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 04:12:02       45 阅读
  4. Python语言-面向对象

    2024-07-20 04:12:02       55 阅读

热门阅读

  1. C++ 前向声明

    2024-07-20 04:12:02       18 阅读
  2. Python-数据爬取(爬虫)

    2024-07-20 04:12:02       16 阅读
  3. 深入理解 Vue 3 组件通信

    2024-07-20 04:12:02       22 阅读
  4. 参考网站总结

    2024-07-20 04:12:02       21 阅读
  5. Spring注解开发

    2024-07-20 04:12:02       20 阅读
  6. C++ 数据结构

    2024-07-20 04:12:02       18 阅读
  7. PYQT按键长按机制

    2024-07-20 04:12:02       16 阅读
  8. godot使用ws

    2024-07-20 04:12:02       18 阅读
  9. mysql(四)

    2024-07-20 04:12:02       16 阅读
  10. Kubernetes Service 之 LoadBalancer

    2024-07-20 04:12:02       18 阅读