PlayersScrollPanelScript.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class PlayersScrollPanelScript : MonoBehaviour
  6. {
  7. public GameObject PlayerSettingPrefab;
  8. private int playerCount = 0;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. updateScrollViewCount(4);
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. }
  18. public void updateScrollViewCount(float value) {
  19. playerCount = GetComponentsInChildren<PlayerSetting>().Length;
  20. if (playerCount < value) {
  21. for (int i = playerCount; i < value; i++) {
  22. GameObject go = Instantiate(PlayerSettingPrefab, new Vector2(0, 0), Quaternion.identity) as GameObject;
  23. PlayerSetting ps = go.GetComponent<PlayerSetting>();
  24. ps.SetPlayerText(i + 1);
  25. ps.SetPlayerName(i + 1);
  26. ps.transform.SetParent(GameObject.Find("NewLocalGamePlayersContent").transform, false);
  27. ps.transform.SetSiblingIndex(i + 1);
  28. }
  29. } else if (playerCount > value) {
  30. for (int i = playerCount; i > value; i--) {
  31. PlayerSetting toDestroy = GameObject.Find("NewLocalGamePlayersContent").transform.GetChild(i - 1).GetComponent<PlayerSetting>();
  32. Destroy(toDestroy.gameObject);
  33. }
  34. }
  35. gameObject.GetComponent<ScrollRect>().verticalNormalizedPosition = 0f;
  36. playerCount = GetComponentsInChildren<PlayerSetting>().Length;
  37. }
  38. }