public void CheckForNewChunks(TrackChunk chunk) { currentChunk = trackChuncks.IndexOf(chunk); if (currentChunk > -1) { int i = currentChunk; while ((i > -1) && (i > (currentChunk - NumberOfActiveChunksBehind))) { trackChuncks[i].Activate(); i--; } if (--i > -1) { trackChuncks[i].Deactivate(); } i = currentChunk; while ((i < trackChuncks.Count) && (i <= (currentChunk + NumberOfActiveChunksAhead))) { trackChuncks[i].Activate(); i++; } } else { throw new UnityException("Chunk " + chunk + " is not registred in chunkCollection.index=" + currentChunk); } }
public void GenerateChunks() { Debug.Log("GenereateChunks()"); Vector3 trackSrart = Vector3.zero; int chunkCount = 0; TrackAbstract track = TrackAbstract.GetInstance(); if (null == track) { throw new UnityException("The Track is not found in scene"); } else { float fullSize = track.GetComponent <Collider>().bounds.size.z; trackSrart = new Vector3(track.collider.bounds.center.x, track.collider.bounds.max.y, track.collider.bounds.min.z); chunkCount = Mathf.CeilToInt(fullSize / ChunkSize); } Debug.Log("chunkCount:" + chunkCount); trackChuncks = new List <TrackChunk>(chunkCount); List <IPoolable> Poolables; MonoBehaviour[] behaviours = FindObjectsOfType(typeof(MonoBehaviour)) as MonoBehaviour[]; if (behaviours != null) { Poolables = behaviours.OfType <IPoolable>().ToList(); } else { Poolables = new List <IPoolable>(0); } for (int j = 0; j < chunkCount; j++) { Vector3 startPoint = trackSrart + new Vector3(0, 0, ChunkSize * j); GameObject newObjChunk = new GameObject { name = "chunk" + j }; UnityEditor.GameObjectUtility.SetStaticEditorFlags(newObjChunk, UnityEditor.StaticEditorFlags.BatchingStatic); newObjChunk.transform.parent = gameObject.transform; TrackChunk newChunk = newObjChunk.AddComponent <TrackChunk>(); newChunk.Init(startPoint, Poolables, ChunkSize); newObjChunk.layer = newObjChunk.transform.parent.gameObject.layer; trackChuncks.Add(newChunk); Debug.Log("trackChunks.Count: " + trackChuncks.Count); } try { foreach (IPoolable poolable in Poolables) { if (poolable != null) { if (poolable.GetGameObject != null) { DestroyImmediate(poolable.GetGameObject); } } } } catch (Exception) { Debug.Log("MissingReferenceException: The object of type 'Obstacle' has been destroyed but you are still trying to access it."); } EditorUtility.SetDirty(this); }