当前位置:   article > 正文

Unity点击生成节点连线

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();
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  1. 资源包下载链接:下载地址
  2. 补充:导入资源包后,需将节点小球预制体的Layer改成“JieDian”。
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/878019
推荐阅读
相关标签
  

闽ICP备14008679号