ShuaXIn.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class ShuaXIn : MonoBehaviour
  6. {
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. }
  11. private void OnEnable() {
  12. StartCoroutine(yc());
  13. }
  14. private void OnDisable() {
  15. StopAllCoroutines();
  16. }
  17. IEnumerator yc()
  18. {
  19. while (true)
  20. {
  21. //refcsf(this.transform);
  22. yield return new WaitForSeconds(0.2f);
  23. refsize();
  24. }
  25. }
  26. public void refcsf(Transform t)
  27. {
  28. ContentSizeFitter[] csf1 = t.transform.GetComponentsInChildren<ContentSizeFitter>();
  29. for (int i = csf1.Length-1;i>0;i--)
  30. {
  31. if(this.transform!=csf1[i].transform)
  32. {
  33. refcsf(csf1[i].transform);
  34. }
  35. csf1[i].SetLayoutHorizontal();
  36. csf1[i].GetComponent<VerticalLayoutGroup>()?.SetLayoutHorizontal();
  37. csf1[i].SetLayoutVertical();
  38. csf1[i].GetComponent<VerticalLayoutGroup>()?.SetLayoutVertical();
  39. }
  40. this.GetComponent<VerticalLayoutGroup>()?.SetLayoutHorizontal();
  41. this.GetComponent<VerticalLayoutGroup>()?.SetLayoutVertical();
  42. this.GetComponent<ContentSizeFitter>()?.SetLayoutHorizontal();
  43. this.GetComponent<ContentSizeFitter>()?.SetLayoutVertical();
  44. }
  45. public void refsize()
  46. {
  47. float y =0;
  48. ContentSizeFitter[] csf1 = this.transform.GetComponentsInChildren<ContentSizeFitter>();
  49. for (int i = csf1.Length-1;i>=0;i--)
  50. {
  51. if(csf1[i].gameObject.activeSelf)
  52. {
  53. if(this.transform!=csf1[i].transform&&csf1[i].transform.parent==this.transform)
  54. {
  55. y+=csf1[i].GetComponent<RectTransform>().sizeDelta.y;
  56. }
  57. }
  58. }
  59. this.GetComponent<RectTransform>().sizeDelta = new Vector2(this.GetComponent<RectTransform>().sizeDelta.x,y);
  60. }
  61. }