comprimir al maximo este codigo:"using System; using System.Collections.Generic; using…
- User
comprimir al maximo este codigo:"using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using DG.Tweening; using Random = UnityEngine.Random;
public class ButtonMover : MonoBehaviour { public Transform[] buttons; // asigna los 4 botones en el inspector public float moveTime = 1f; // el tiempo que tardan en moverse public float flushTime = 5f; // el tiempo que tardan en intercambiarse private bool isPlaying = false; private int totalTweens = 0;
private float minX; private float maxX; private float minY; private float maxY; private Vector2 randomPosition; void Update() { if (!isPlaying) { isPlaying = true; foreach (var button in buttons) { if (Random.Range(0, 2) == 0) MoveToRandomPosition(button); } } //every 60 frames, check if there are any tweens playing if (Time.frameCount % 30 == 0) { //log total active tweens int newTotalTweens = DOTween.TotalPlayingTweens(); if (newTotalTweens != totalTweens) { totalTweens = newTotalTweens; Debug.Log("Active Tweens: " + totalTweens); if (totalTweens == 0) { isPlaying = false; } } } } private void FlushButtons() { List<int> usedButtonIndices = new List<int>(); // recorre cada botón y le asigna la posición aleatoria de otro botón foreach (var t in buttons) { // elige un botón aleatorio que no sea el mismo y que no haya sido elegido antes Transform otherButton; do { otherButton = buttons[Random.Range(0, buttons.Length)]; } while (otherButton == t || usedButtonIndices.Contains(otherButton.GetSiblingIndex())); Debug.Log("name other: " + otherButton.name); Debug.Log("--------------------"); usedButtonIndices.Add(otherButton.GetSiblingIndex()); // move to the other button position with a smooth ease t.DOMove(otherButton.position, flushTime).SetEase(Ease.InOutSine).SetId(t); Debug.Log("name Tween: " + t.name); } foreach (var i in usedButtonIndices) { Debug.Log("usedButtonIndices: " + i); } //log separator Debug.Log("----------"); } private void MoveToRandomPosition(Transform button) { AudioSource audioSource; audioSource = button.GetComponent<AudioSource>(); //Obtenemos el tamaño de la pantalla en unidades de Unity float screenHeight = Camera.main.orthographicSize * 2; float screenWidth = screenHeight * Screen.width / Screen.height; Debug.Log("screenWidth: " + screenWidth); Debug.Log("screenHeight: " + screenHeight); float buttonWidth = button.GetComponent<SpriteRenderer>().bounds.size.x; float buttonHeight = button.GetComponent<SpriteRenderer>().bounds.size.y; minX = -screenWidth / 2 + buttonWidth / 2; maxX = screenWidth / 2 - buttonWidth / 2; minY = -screenHeight / 2 + buttonHeight / 2; maxY = screenHeight / 2 - buttonHeight / 2; float randomX = Random.Range(minX, maxX); float randomY = Random.Range(minY, maxY); //Creamos un vector 2D con las coordenadas aleatorias generadas randomPosition = new Vector2(randomX, randomY); //Comprobamos que la posición no se salga de la pantalla randomPosition.x = Mathf.Clamp(randomPosition.x, -screenWidth / 2 , screenWidth / 2 ); randomPosition.y = Mathf.Clamp(randomPosition.y, -screenHeight / 2 , screenHeight / 2 ); Ease easeRandom = (Ease) Random.Range(0, 30); Ease easeRandom2 = (Ease) Random.Range(0, 30); Debug.Log("easeRandom: " + easeRandom); //button.DOMove(randomPosition, flushTime).SetEase(easeRandom).SetId(button); //move and rotate to the random position with a random ease Sequence sequence = DOTween.Sequence(); sequence.Append(button.transform.DOMove(randomPosition, moveTime).SetEase(easeRandom).SetId(button)); sequence.Join(button.transform.DORotate(new Vector3(0, 360, 0), moveTime, RotateMode.FastBeyond360) .SetEase(easeRandom2).SetId(button)) .OnPlay(() => audioSource.Play()) .OnComplete(() => audioSource.Stop()); }}"
- Assistant
using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using DG.Tweening; using Random = UnityEngine.Random;
public class ButtonMover : MonoBehaviour { public Transform[] buttons; public float moveTime = 1f; public float flushTime = 5f; private bool isPlaying = false; private int totalTweens = 0;
private float minX, maxX, minY, maxY; private Vector2 randomPosition; void Update() { if (!isPlaying) { isPlaying = true; foreach (var button in buttons) if (Random.Range(0, 2) == 0) MoveRandom(button); } if (Time.frameCount % 30 == 0) { int newTotalTweens = DOTween.TotalPlayingTweens(); if (newTotalTweens != totalTweens) { totalTweens = newTotalTweens; Debug.Log("Active Tweens: " + totalTweens); if (totalTweens == 0) isPlaying = false; } } } private void FlushButtons() { List<int> usedButtonIndices = new List<int>(); foreach (var t in buttons) { Transform otherButton; do { otherButton = buttons[Random.Range(0, buttons.Length)]; } while (otherButton == t || usedButtonIndices.Contains(otherButton.GetSiblingIndex())); usedButtonIndices.Add(otherButton.GetSiblingIndex()); t.DOMove(otherButton.position, flushTime).SetEase(Ease.InOutSine).SetId(t); } } private void MoveRandom(Transform button) { AudioSource audioSource = button.GetComponent<AudioSource>(); float screenHeight = Camera.main.orthographicSize * 2; float screenWidth = screenHeight * Screen.width / Screen.height; float buttonWidth = button.GetComponent<SpriteRenderer>().bounds.size.x; float buttonHeight = button.GetComponent<SpriteRenderer>().bounds.size.y; minX = -screenWidth / 2 + buttonWidth / 2; maxX = screenWidth / 2 - buttonWidth / 2; minY = -screenHeight / 2 + buttonHeight / 2; maxY = screenHeight / 2 - buttonHeight / 2; float randomX = Random.Range(minX, maxX); float randomY = Random.Range(minY, maxY); randomPosition = new Vector2(randomX, randomY); randomPosition.x = Mathf.Clamp(randomPosition.x, -screenWidth / 2 , screenWidth / 2 ); randomPosition.y = Mathf.Clamp(randomPosition.y, -screenHeight / 2 , screenHeight / 2 ); Ease easeRandom = (Ease) Random.Range(0, 30); Ease easeRandom2 = (Ease) Random.Range(0, 30); Sequence sequence = DOTween.Sequence(); sequence.Append(button.DOMove(randomPosition, moveTime).SetEase(easeRandom).SetId(button)); sequence.Join(button.DORotate(new Vector3(0, 360, 0), moveTime, RotateMode.FastBeyond360) .SetEase(easeRandom2).SetId(button)) .OnPlay(() => audioSource.Play()) .OnComplete(() => audioSource.Stop()); }}