using System.Collections; using System.Collections.Generic; using UnityEngine; [RequireComponent(typeof(SpriteRenderer))] public class Tiling : MonoBehaviour { public int offsetX = 2; public int offsetY = 2; public bool hasRightBuddy = false; public bool hasLeftBuddy = false; public bool hasTopBuddy = false; public bool hasBottomBuddy = false; public bool reverseScale = false; private float spriteWidth = 0f; private float spriteHeight = 0f; private Camera cam; private Transform myTransform; private void Awake() { cam = Camera.main; myTransform = transform; } // Start is called before the first frame update void Start() { SpriteRenderer spriteRenderer = GetComponent(); spriteWidth = spriteRenderer.sprite.bounds.size.x; spriteHeight = spriteRenderer.sprite.bounds.size.y; //GenerateBackground(); } // Update is called once per frame void Update() { if (!hasLeftBuddy || !hasRightBuddy) { float camHorizontalExtend = cam.orthographicSize * Screen.width / Screen.height; float edgeVisiblePositionRight = (transform.position.x + spriteWidth / 2) - camHorizontalExtend; float edgeVisiblePositionLeft = (transform.position.x - spriteWidth / 2) + camHorizontalExtend; if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && !hasRightBuddy) { MakeNewRightOrLeftBuddy(1); hasRightBuddy = true; } else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && !hasLeftBuddy) { MakeNewRightOrLeftBuddy(-1); hasLeftBuddy = true; } } } private void MakeNewRightOrLeftBuddy(int rightOrLeft) { Vector3 newPosition = new Vector3(myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z); Transform newBuddy = Instantiate(myTransform, newPosition, myTransform.rotation); if (reverseScale) { newBuddy.localScale = new Vector3(newBuddy.localScale.x * -1, newBuddy.localScale.y, newBuddy.localScale.z); } newBuddy.parent = myTransform.parent; newBuddy.name = myTransform.name; if (rightOrLeft > 0) { newBuddy.GetComponent().hasLeftBuddy = true; } else if (rightOrLeft < 0) { newBuddy.GetComponent().hasRightBuddy = true; } } }