public void InstantiateObjects() { CleanInstances(); if (parallaxPrefab == null) { return; } Vector2 min = new Vector2(float.MaxValue, float.MaxValue); Vector2 max = new Vector2(float.MinValue, float.MinValue); Vector3 instanceSize = Vector2.zero; for (int i = 0; i < CopyAmount; i++) { Vector3 position = GetPositionFromMatrix(GetModulatedIndex(i)); Parallaxable instance = Instantiate <Parallaxable>(parallaxPrefab, position, Quaternion.identity, transform); instantiatedObjects.Add(instance); instance.gameObject.SetActive(true); Bounds instanceBounds = instance.GetBounds(); min = Vector2.Min(min, instanceBounds.min); max = Vector2.Max(max, instanceBounds.max); instanceSize = instanceBounds.size; } parallaxBounds = new Bounds((Vector3)offset, (Vector3)(max - min) - instanceSize / 2.0f); UpdatePositions(); }
public void UpdatePositions() { for (int i = 0; i < instantiatedObjects.Count; i++) { Parallaxable paralaxable = instantiatedObjects[i]; Vector2 modulatedIndex = GetModulatedIndex(i); Vector3 position = GetPositionFromMatrix(modulatedIndex) + (Vector3)(Vector2.Scale(parallaxRatio, parallaxScroll)); if (wrap.x || wrap.y) { Vector2 paralaxableSize = paralaxable.GetBounds().size; Vector2 size = (Vector2)parallaxBounds.size + paralaxableSize / 2.0f; position = new Vector3(wrap.x? position.x - parallaxBounds.min.x : position.x, wrap.y? position.y - parallaxBounds.min.y : position.y, position.z); if (wrap.x) { position.x = position.x < 0? size.x + position.x % size.x : position.x; position.x = (position.x) % size.x + parallaxBounds.min.x; position.x += transform.position.x; } if (wrap.y) { position.y = position.y < 0? size.y + position.y % size.y : position.y; position.y = (position.y) % size.y + parallaxBounds.min.y; position.y += transform.position.y; Func <float, float> maxRet = arg => arg + (modulatedIndex.y - 1) * paralaxableSize.y; Func <float, float> minRet = arg => copyLayout.y * paralaxableSize.y + maxRet(arg); } } else { position += transform.position; } paralaxable.transform.position = position; } }