using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ObjectsRect : MonoBehaviour { public List rects; RectTransform thisRect; Text txtStatus; bool adjusting; public float space; float lastCheckpoint; // Start is called before the first frame update void Start() { for (int i = 0; i < transform.childCount; i++) { rects.Add(transform.GetChild(i).GetComponent()); } thisRect = GetComponent(); lastCheckpoint = thisRect.anchoredPosition.x; txtStatus = GameObject.Find("Status").GetComponent(); } IEnumerator AdjustToCenter() { Vector2 laspos = thisRect.anchoredPosition; float distanceLast = 99; while (distanceLast > 15) { yield return new WaitForSecondsRealtime(.15f); distanceLast = Vector2.Distance(laspos, thisRect.anchoredPosition); laspos = thisRect.anchoredPosition; } float yFactor = thisRect.anchoredPosition.y; float speed = 1; adjusting = true; if (Vector2.Distance(thisRect.anchoredPosition, new Vector2(lastCheckpoint, yFactor)) > (space / 2)) { if (thisRect.anchoredPosition.x > lastCheckpoint) { AdvanceObj(true); } else { AdvanceObj(false); } } yield return new WaitForEndOfFrame(); while (adjusting && thisRect.anchoredPosition.x != lastCheckpoint) { Vector2 posNew = Vector2.MoveTowards(thisRect.anchoredPosition, new Vector2(lastCheckpoint, yFactor), speed); thisRect.anchoredPosition = posNew; if (speed < 9) { speed += (8 * Time.deltaTime); } else { speed = 9; } yield return new WaitForFixedUpdate(); } adjusting = false; yield break; } // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { adjusting = false; } if (Input.GetMouseButtonUp(0)) { StartCoroutine(AdjustToCenter()); } float positionNow = thisRect.anchoredPosition.x; if (!adjusting) { if (positionNow > (lastCheckpoint + space)) { AdvanceObj(true); } else if (positionNow < (lastCheckpoint - space)) { AdvanceObj(false); } } } void AdvanceObj(bool positive) { if (positive) { RectTransform lastRect = rects[rects.Count - 1]; Vector2 poslastRect = lastRect.anchoredPosition; lastRect.anchoredPosition = new Vector2(poslastRect.x - (space * rects.Count), poslastRect.y); rects.RemoveAt(rects.Count - 1); rects.Insert(0, lastRect); lastCheckpoint += space; } else { RectTransform firstrect = rects[0]; Vector2 poslastRect = firstrect.anchoredPosition; firstrect.anchoredPosition = new Vector2(poslastRect.x + (space * rects.Count), poslastRect.y); rects.RemoveAt(0); rects.Add(firstrect); lastCheckpoint -= space; } switch (rects[3].gameObject.name) { case "Conversation": txtStatus.text = "Conversation"; break; case "Configs": txtStatus.text = "Settings"; break; case "AppClass": txtStatus.text = "Go to class"; break; case "EditZoe": txtStatus.text = "Customize"; break; case "Gaming": txtStatus.text = "Games"; break; } } }