void OnEnable() { mMeshFilter = GetComponent <MeshFilter>(); mMesh = mMeshFilter.sharedMesh; if (!mMesh) { mMesh = new Mesh(); mMesh.hideFlags = HideFlags.HideAndDontSave; mMesh.name = "CurvyMesh"; mMeshFilter.sharedMesh = mMesh; } if (!Application.isPlaying) { if (Spline && !Spline.IsInitialized) { Spline.SetDirtyAll(); Spline.Refresh(); } Refresh(); } }
/// <summary> /// Rebuilds the path. /// </summary> /// <param name="force">If true, all existing clones will be destroyed, otherwise they will be reused</param> /// <remarks>If you change the Source array by code, call Refresh(true)</remarks> public void Refresh(bool force) { if (Spline == null || !Spline.IsInitialized) { return; } // we need a spline length if (Spline.Length == 0) { Spline.SetDirtyAll(); } checkSources(); if (Source.Length == 0) { Clear(); return; } // get size of clones and calculate number of clones needed float totaldepth; float[] depths = getSourceDepths(out totaldepth); int count = 0; if (!Mathf.Approximately(0, totaldepth)) { switch (Mode) { case SplinePathCloneBuilderMode.CloneGroup: count = Mathf.FloorToInt(Spline.Length / totaldepth) * Source.Length; break; default: // Individual float d = Spline.Length; int i = 0; while (d > 0 && count < MAXCLONES) { d -= depths[i++] + Gap; count++; if (i == Source.Length) { i = 0; } } if (count != MAXCLONES) { count--; } break; } } // Constrain max clones if (count >= MAXCLONES) { Debug.LogError("SplinePathCloneBuilder: MAXCLONES reached, ensure to have proper colliders in place! If you really want to clone more than " + MAXCLONES + " objects, increase MAXCLONES in SplinePathCloneBuilder.cs (Line 15)!"); } else { // Clear if (force) { Clear(); // Clear all clones } else { Clear(count); // Smart Clear only unneeded } int idx = 0; float distance = 0; int current = -1; int existing = ObjectCount; while (++current < count) { float tf = Spline.DistanceToTF(distance + depths[idx] / 2); if (current < existing) { Transform T = mTransform.GetChild(current); if (UseWorldPosition) { T.position = Spline.Interpolate(tf); } else { T.localPosition = Spline.Interpolate(tf); } T.rotation = Spline.GetOrientationFast(tf) * Source[idx].transform.rotation; } else { GameObject clone; if (OnGetClone != null) { clone = OnGetClone(this, Source[idx]); } else { clone = CloneObject(Source[idx]); } if (clone) { Transform T = clone.transform; T.parent = transform; clone.name = string.Format("{0:0000}", current) + clone.name; if (UseWorldPosition) { T.position = Spline.Interpolate(tf); } else { T.localPosition = Spline.Interpolate(tf); } T.rotation = Spline.GetOrientationFast(tf) * Source[idx].transform.rotation; } } distance += depths[idx] + Gap; if (++idx == Source.Length) { idx = 0; } } } }