private void UpdateCamera(ALayerCamera layerCamera) { OnFirstRun(); Vector3 deltaPos = mainLayerCamera.Position - mainLayerCamera.StartPosition; cameraXform.position = StartPosition + Vector3.Scale(deltaPos, parallaxScale); InvokeCallbacks(); }
/// <summary> /// 获取当前安全可视范围。 /// </summary> /// <param name="layerCamera">层摄像机。</param> /// <param name="minCoordinate">安全可视范围的最小坐标。</param> /// <param name="maxCoordinate">安全可视范围的最大坐标。</param> private void GetVisibleRange(ALayerCamera layerCamera, out float minCoordinate, out float maxCoordinate) { float halfLength = GetSafeVisibleAreaLength() * 0.5f; float camPosition = GetOneDCoordinate(layerCamera.Position); minCoordinate = camPosition - halfLength; maxCoordinate = camPosition + halfLength; }
/// <summary> /// 将 GameObject 的 layer 设置为 层摄像机可见的第一个 layer。 /// 如果层摄像机没有可见层则保留 GameObject 原先设置。 /// </summary> /// <param name="go">GameObject。</param> /// <param name="layerCamera">层摄像机。</param> protected void SetToCameraLayer(GameObject go, ALayerCamera layerCamera) { LayerMask layerMask = layerCamera.Camera.cullingMask; int layer = layerMask.GetFirstLayer(); if (layer != -1) { go.layer = layer; } }
/// <summary> /// 获取当前安全可视范围内(及向外扩展一个元素)元素最大索引和最小索引。 /// </summary> /// <param name="layerCamera">层摄像机。</param> /// <param name="nearElementIndex">近端场景元素索引。</param> /// <param name="farElementIndex">远端场景元素索引。</param> protected void GetVisibleElementsRange(ALayerCamera layerCamera, out int nearElementIndex, out int farElementIndex) { float minCoordinate; float maxCoordinate; GetVisibleRange(layerCamera, out minCoordinate, out maxCoordinate); nearElementIndex = GetNearElementIndex(minCoordinate, elementCoordinates); farElementIndex = GetFarElementIndex(maxCoordinate, elementCoordinates); }
public override void OnBecameInvisible(ALayerCamera layerCamera) { if (!IsVisible) { return; } Assert.IsNotNull(entity); entity.localScale = originalScale; PoolManager.Despawn(entity); entity = null; IsVisible = false; }
public override void OnBecameVisible(ALayerCamera layerCamera) { if (IsVisible) { return; } Assert.IsNull(entity); entity = PoolManager.Spawn(PoolName, Prefab, Position, Rotation); originalScale = entity.localScale; entity.localScale = Vector3.Scale(originalScale, RelativeScale); SetToCameraLayer(entity.gameObject, layerCamera); IsVisible = true; }
protected override void UpdateCulling(ALayerCamera layerCamera) { if (elementCoordinates.Count == 0) { return; } // 获取可见的最近和最远场景元素的索引值 int nearElementIndex; int farElementIndex; GetVisibleElementsRange(layerCamera, out nearElementIndex, out farElementIndex); // 设置可视范围内的元素为可见 for (int i = nearElementIndex; i <= farElementIndex; i++) { if (!elements[i].IsVisible) { elements[i].OnBecameVisible(layerCamera); } } // 回收身后的场景元素 if (positiveDirection == RelativeDirection.Positive) { int index = 0; while (index < nearElementIndex) { SceneElementPool.Delete(elements[0]); elements.RemoveAt(0); elementCoordinates.RemoveAt(0); index += 1; } } else { int index = elements.Count - 1; while (index > farElementIndex) { SceneElementPool.Delete(elements[index]); elements.RemoveAt(index); elementCoordinates.RemoveAt(index); index -= 1; } } }
/// <summary> /// 摄像机更新回调。 /// </summary> private void UpdateSpawn(ALayerCamera layerCamera) { float camViewPositiveDist; float camViewNegativeDist; GetCameraViewDistance(out camViewPositiveDist, out camViewNegativeDist); if (filledPositiveDistance < camViewPositiveDist) { filledPositiveDistance = SpawnToPositiveDistance(filledPositiveDistance, camViewPositiveDist, sceneCulling); } if (filledNegativeDistance > camViewNegativeDist) { filledNegativeDistance = SpawnToNegativeDistance(filledNegativeDistance, camViewNegativeDist, sceneCulling); } }
protected override void UpdateCulling(ALayerCamera layerCamera) { if (elementCoordinates.Count == 0) { return; } // 获取可见的最近和最远场景元素的索引值 int nearElementIndex; int farElementIndex; GetVisibleElementsRange(layerCamera, out nearElementIndex, out farElementIndex); // 设置可视范围内的元素为可见 for (int i = nearElementIndex; i <= farElementIndex; i++) { if (elements[i].IsVisible) { visibleElements.Remove(elements[i]); } else { elements[i].OnBecameVisible(layerCamera); } } // 设置之前可见当前不可见的场景元素为不可见 foreach (ASceneElement element in visibleElements) { element.OnBecameInvisible(layerCamera); } // 重新填充当前可见元素集合 visibleElements.Clear(); for (int i = nearElementIndex; i <= farElementIndex; i++) { visibleElements.Add(elements[i]); } }
/// <summary> /// 更新场景元素的可见性。 /// </summary> /// <param name="layerCamera"></param> abstract protected void UpdateCulling(ALayerCamera layerCamera);
/// <summary> /// 元素变得不可见时回调。 /// </summary> abstract public void OnBecameInvisible(ALayerCamera layerCamera);