最后一人离开重置

大厅空了就把场地复位(重置比分、关灯、收回道具)。用玩家加入/离开事件统计人数。

脚本

using UnityEngine;
using Cilbox;
using Aeloria;
using Aeloria.Scripts.Networking.NetworkedAvatar;

[Cilboxable]
public class ArenaReset : MonoBehaviour
{
    AeloriaNetworkShim net;
    int players;

    void Start()
    {
        net = GetComponent<AeloriaNetworkShim>();
        net.PlayerJoined += OnJoined;
        net.PlayerLeft += OnLeft;
    }

    void OnDestroy()
    {
        if (net == null) return;
        net.PlayerJoined -= OnJoined;
        net.PlayerLeft -= OnLeft;
    }

    void OnJoined(AeloriaNetworkPlayer player) { players++; }

    void OnLeft(AeloriaNetworkPlayer player)
    {
        players--;
        if (players <= 0) ResetArena();
    }

    void ResetArena()
    {
        // 在这里复位场地
    }
}
  • 加入时也会回调一次,所以计数从第一个玩家开始就是对的;
  • 如果只在”服务器/主机”上复位,加一个 net.IsOwnedLocallyOnServer 判断即可;
  • 复位逻辑里如果涉及共享状态,记得用「网络同步」里的写法广播出去。