|
|
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class PlayerJoinUI : MonoBehaviour
- {
-
- [SerializeField]
- private PlayerList m_ConnectedPlayers;
-
- [SerializeField]
- private RectTransform m_prefab;
-
- [SerializeField]
- private List<RectTransform> m_zones;
-
- private int count;
-
-
- private void OnEnable()
- {
- m_ConnectedPlayers.OnPlayerJoin += OnPlayerJoin;
- }
-
- private void OnDisable()
- {
- m_ConnectedPlayers.OnPlayerJoin -= OnPlayerJoin;
- }
-
-
- private void OnPlayerJoin(object sender, PlayerData data)
- {
- RectTransform parent = m_zones[count++];
- count = count % m_zones.Count;
-
-
- RectTransform newPoster = Instantiate(m_prefab);
- newPoster.parent = parent;
-
- RectTransform[] children = parent.GetComponentsInChildren<RectTransform>();
-
- for (int i = 0; i < 10; i++)
- {
- float randomX = Random.Range(parent.rect.xMin + newPoster.rect.width / 2, parent.rect.xMax - newPoster.rect.width / 2);
- float randomY = Random.Range(parent.rect.yMin + newPoster.rect.height / 2, parent.rect.yMax - newPoster.rect.height / 2);
-
- newPoster.localPosition = new Vector3(randomX, randomY, 0.0f);
-
- newPoster.Rotate(Vector3.forward, Random.Range(-20.0f, 20.0f));
-
- if (Random.Range(0, 2) > 0)
- newPoster.transform.localScale = new Vector3(-newPoster.transform.localScale.x, newPoster.transform.localScale.y, newPoster.transform.localScale.z);
-
-
-
- bool isOverlapping = false;
- foreach (RectTransform child in children)
- {
- if (child == parent)
- continue;
-
- if (child.rect.Overlaps(newPoster.rect))
- {
- isOverlapping = true;
- Debug.Log($"Overlapping with: {child.name} [{i}]");
- }
- }
- if (!isOverlapping)
- break;
- }
-
-
-
- newPoster.GetComponent<PlayerJoinIcon>().Initialise(data);
- newPoster.gameObject.SetActive(true);
-
- }
-
-
- }
|