Beispiel #1
0
        /// <summary>
        /// Scroll the background horizontally to the left by a certain number of screens.
        /// </summary>
        /// <param name="screens">The number of screens to scroll left.</param>
        /// <param name="speed">The speed at which to scroll the background.</param>
        /// <param name="movingTransforms">An array of transforms that should move with the background.</param>
        /// <param name="ScrollCompleteAction">An action to perform once the scroll has completed.</param>
        private IEnumerator ScrollBackground(int screens, float speed, Transform[] movingTransforms, Action ScrollCompleteAction)
        {
            if (screens <= 0)
            {
                yield break;
            }

            IsScrolling = true;
            OnScrollStart?.Invoke(this, EventArgs.Empty);
            int screensScrolled = 0;

            do
            {
                // Move background
                transform.Translate(Vector2.left * speed * Time.deltaTime);

                // Loop background when it passes the start position
                if (transform.position.x < -startPosition.x)
                {
                    Vector2 newPosition = startPosition;
                    newPosition.x     -= (-startPosition.x - transform.position.x);
                    transform.position = newPosition;
                    screensScrolled++;
                }

                // Move objects with background
                foreach (Transform movingTransform in movingTransforms)
                {
                    movingTransform.Translate(Vector2.left * speed * Time.deltaTime);
                }

                yield return(null);
            } while (screensScrolled < screens);

            // Make sure background is pixel perfect
            transform.position = pixelPerfectCamera.RoundToPixel(transform.position);

            IsScrolling = false;
            OnScrollComplete?.Invoke(this, EventArgs.Empty);
            ScrollCompleteAction?.Invoke();
        }
Beispiel #2
0
 ///<summary>
 // Invokes the OnScrollComplete event
 ///</summary>
 public void InvokeOnScrollComplete <T>(T sender, HorizontalBar bar)
 {
     Debug.Log($"[{Time.time}] OnScrollComplete Invoked by {sender.ToString()}");
     OnScrollComplete?.Invoke(bar);
 }