Ciao. Potresti semplificare il seguente codice per Unity? Con semplificare intendo renderlo più snello ed efficente, magari semplificando dei passaggi e/o eliminando passaggi non necessari. Preciso che il codice funziona correttamente.
 Ecco il codice in C#:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
using System.Linq;
using NaughtyAttributes;

[ExecuteInEditMode]
public class testMono2 : MonoBehaviour
{
    public Vector3[] spline;
    public bool loop;

    [Header("Line Render")]
    public Material lineMaterial;
    public float width = 0.1f;

    [Button("Import Spline")]
    void import()
    {
        if (transform.childCount > 0)
        {
            List<Vector3> pos = new List<Vector3>();

            for (int i = 0; i < transform.childCount; i++)
            {
                pos.Add(transform.GetChild(i).position);
            }

            spline = pos.ToArray();
        }

        LineRenderer lr;

        if(!(lr = GetComponent<LineRenderer>()))
        {
            lr = gameObject.AddComponent<LineRenderer>();
        }

        lr.startWidth = width;
        lr.endWidth = width;
        
        if(lineMaterial)
        {
            lr.sharedMaterial = lineMaterial;
        }

        lr.positionCount = spline.Length;
        lr.SetPositions(spline);
        lr.loop = loop;
    }

    public Vector3 _ClosestPoint(Vector3 point)
    {
		Vector3 position = new Vector3();
        Vector3 p1 = new Vector3();
        Vector3 p2 = new Vector3();

        float distance = Mathf.Infinity;
        int index = 0;

        for (int i = 0; i < spline.Count; i++)
        {
            float d = Vector3.Distance(point, spline[i]);

            if(d < distance)
            {
                distance = d;
                index = i;
            }
        }

        p1 = spline[index];

        if(!loop && (index == spline.Count - 1 || index == 0))
        {
            p2 = spline[(index == 0) ? 1 : spline.Count - 2];
        }
        else
        {
            Vector3 a1 = new Vector3();
            Vector3 a2 = new Vector3();

            if(loop && index == spline.Count - 1)
            {
                a1 = spline[spline.Count - 2];
                a2 = spline[0];
            }
            else if (loop && index == 0)
            {
                a1 = spline[spline.Count - 1];
                a2 = spline[index + 1];
            }
            else
            {
                a1 = spline[index - 1];
                a2 = spline[index + 1];
            }

            float d1 = Vector3.Distance(point, a1);
            float d2 = Vector3.Distance(point, a2);

            p2 = (d1 < d2) ? a1 : a2;
        }

        position = point.NearestPointOnLine(p1, p2);

        return position;    
	}

    public Vector3 _findNormPosition(float time)
    {
        List<Tuple<Vector3, Vector3, float, float>> scale = new List<Tuple<Vector3, Vector3, float, float>>();
        float totalDistance = 0;
        Vector3 _position = Vector3.zero;
        time = Mathf.Clamp(time, 0, 0.999f);

        #region create scale

        for (int i = 0; i < spline.Length - 1; i++)
        {
            Vector3 a = spline[i];
            Vector3 b = spline[i + 1];

            float dist = Vector3.Distance(a, b);
            totalDistance += dist;

            scale.Add(new Tuple<Vector3, Vector3, float, float>(a, b, dist, totalDistance));
        }

        if (loop)
        {
            Vector3 a = spline[spline.Length - 1];
            Vector3 b = spline[0];

            float dist = Vector3.Distance(a, b);
            totalDistance += dist;

            scale.Add(new Tuple<Vector3, Vector3, float, float>(a, b, dist, totalDistance));
        }

        #endregion

        float distPosition = time * totalDistance;

        for (int i = 0; i < scale.Count; i++)
        {
            if (distPosition < scale[i].Item4)
            {
                float subPosition = (i == 0) ? distPosition : distPosition - scale[i - 1].Item4;
                float normPos = subPosition / scale[i].Item3;

                _position = Vector3.Lerp(scale[i].Item1, scale[i].Item2, normPos);

                break;
            }
        }

        return _position;
    }
}