unity实现点线动画 免费下载

 

微信号:ITComputerGraph

unity实现点线动画 免费下载_第1张图片

更多精彩内容,关注公众号《IT木子李》

 

unity实现点线动画 免费下载_第2张图片

主要应用LineRenderer组件实现连线效果,根据距离判断是否需要连接,并设置透明度;

直接上代码;

using UnityEngine;

public class DrawLine : MonoBehaviour
{
    /// 最大距离
    float maxDistance;
    /// 最小距离
    float minDistance;
    /// 点的标签
    int pointTag;

    LineRenderer line;
    SphereInfo localInfo;
    Collider col;
    Material localMaterial;

    Color colorLine = Color.white;
    SphereInfo tempInfo;
    Collider[] sphereRayToColliders;
    float tempDistance;

    void Start()
    {
        maxDistance = PointClampData.Instance.maxDistance;
        minDistance = PointClampData.Instance.minDistance;
        pointTag = LayerMask.GetMask(PointClampData.Instance.pointTag);

        line = GetComponent();
        localInfo = GetComponent();
        col = GetComponent();
        localMaterial = GetComponent().material;
    }

    void Update()
    {
        ConnectMulti();
        UpdateActiveConnectionMulti();
        UpdatePassiveConnectionMulti();
        SetVisible();
    }

    /// 发起连接
    void ConnectMulti()
    {
        if (localInfo.connectTarget != null)
        {
            tempInfo = localInfo.connectTarget.GetComponent();
            if (tempInfo.connectSourceOne != col && tempInfo.connectSourceTwo != col && tempInfo.connectSourceThree != col)
            {
                localInfo.connectTarget = null;
            }
            else
            {
                return;
            }
        }

        sphereRayToColliders = Physics.OverlapSphere(transform.position, maxDistance, pointTag);
        if (sphereRayToColliders == null || sphereRayToColliders.Length <= 0) return;

        for (int i = 0; i < sphereRayToColliders.Length; i++)
        {
            tempInfo = sphereRayToColliders[i].GetComponent();

            if (tempInfo == localInfo) continue;
            if (tempInfo.connectSourceOne != null && tempInfo.connectSourceTwo != null && tempInfo.connectSourceThree != null) continue;
            if (col == tempInfo.connectTarget) continue;

            line.positionCount = 2;
            line.SetPosition(0, transform.position);
            line.SetPosition(1, sphereRayToColliders[i].transform.position);
            localInfo.connectTarget = sphereRayToColliders[i];

            colorLine.a = 1;
            localMaterial.color = colorLine;

            if (tempInfo.connectSourceOne == null)
                tempInfo.connectSourceOne = col;
            else if (tempInfo.connectSourceTwo == null)
                tempInfo.connectSourceTwo = col;
            else if (tempInfo.connectSourceThree == null)
                tempInfo.connectSourceThree = col;
            return;
        }
    }

    /// 更新主动连接
    void UpdateActiveConnectionMulti()
    {
        if (localInfo.connectTarget == null) return;

        tempDistance = Vector3.Distance(transform.position, localInfo.connectTarget.transform.position);

        if (tempDistance > maxDistance)
        {
            line.positionCount = 0;
            localInfo.connectTarget = null;
        }
        else
        {
            if (tempDistance < minDistance)
            {
                colorLine.a = 1;
            }
            else
            {
                colorLine.a = 1 - (tempDistance / maxDistance);
            }
            line.SetPosition(0, transform.position);
            line.material.color = colorLine;
        }
    }

    /// 更新被动连接
    void UpdatePassiveConnectionMulti()
    {
        if (localInfo.connectSourceOne == null && localInfo.connectSourceTwo == null && localInfo.connectSourceThree == null) return;

        if (localInfo.connectSourceOne != null)
        {
            PassiveConnectionData(ref localInfo.connectSourceOne);
        }
        if (localInfo.connectSourceTwo != null)
        {
            PassiveConnectionData(ref localInfo.connectSourceTwo);
        }
        if (localInfo.connectSourceThree != null)
        {
            PassiveConnectionData(ref localInfo.connectSourceThree);
        }
    }

    void PassiveConnectionData(ref Collider collider)
    {
        tempDistance = Vector3.Distance(transform.position, collider.transform.position);

        if (tempDistance > maxDistance)
        {
            collider = null;
        }
        else
        {
            LineRenderer temp = collider.GetComponent();
            if (temp.positionCount != 0)
                temp.SetPosition(1, transform.position);
        }
    }

    /// 设置小球显隐
    void SetVisible()
    {
        if (localInfo.connectTarget == null && localInfo.connectSourceOne == null && localInfo.connectSourceTwo == null && localInfo.connectSourceThree == null)
        {
            colorLine.a = 0;
            localMaterial.color = colorLine;
        }
    }

}



 

你可能感兴趣的:(Unity,U3D)