Example #1
0
    //Generates a mesh along a spline. The mesh is defined in the settings at index i
    private void GenerateMesh(Transform splineParent, SplineSettings settings, int spline, int i)
    {
        GeneratedMesh meshSettings = settings.generated[i];
        string        name         = meshSettings.name;

        if (name.Length == 0)
        {
            name = string.Concat("Generated Mesh ", i.ToString("D2"));
        }
        name = string.Concat(i.ToString("D2"), "-", name);
        Transform newGenerated = splineParent.Find(name);

        //Create a new GameObject if there is none in the current scene
        if (newGenerated == null)
        {
            newGenerated               = new GameObject().transform;
            newGenerated.parent        = splineParent;
            newGenerated.localPosition = Vector3.zero;
            newGenerated.localRotation = Quaternion.identity;
            newGenerated.localScale    = Vector3.one;
            newGenerated.name          = name;
            newGenerated.gameObject.AddComponent <MeshFilter>();
            newGenerated.gameObject.AddComponent <MeshRenderer>();
        }

        //Update mesh and material
        newGenerated.GetComponent <MeshFilter>().mesh       = meshSettings.Generate(splines[spline]);
        newGenerated.GetComponent <MeshRenderer>().material = meshSettings.material;
    }