Two or more players
Spawn a character when a phone joins; remove it when the player is really gone.
Video: Two or more players (2:04)
Add the Players script below next to the Host
using System.Collections.Generic;
using PadPlay;
using UnityEngine;
public class Players : MonoBehaviour
{
[SerializeField] PadPlayHost host;
[SerializeField] GameObject playerPrefab;
readonly Dictionary<int, GameObject> _bySlot = new Dictionary<int, GameObject>();
void OnEnable()
{
host.PlayerReady += OnReady;
host.PlayerRemoved += OnRemoved;
}
void OnDisable()
{
host.PlayerReady -= OnReady;
host.PlayerRemoved -= OnRemoved;
}
void OnReady(PadPlayPlayer p)
{
if (_bySlot.ContainsKey(p.PlayerSlot))
return; // came back: keep the same character
_bySlot[p.PlayerSlot] = Instantiate(playerPrefab);
}
void OnRemoved(PadPlayPlayerRemoved r)
{
if (_bySlot.Remove(r.PlayerSlot, out GameObject character))
Destroy(character);
}
}Turn off the second phone's Wi-Fi: the character stays. It goes after 300 seconds, or when the player presses Disconnect in the app.