You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Photon Fusion BR200示例WebGL构建CS0234错误求助

Unity Fusion BR200示例构建WebGL时遭遇CS0234错误

错误信息

Assets\TPSBR\Scripts\Networking\Multiplay\MultiplayManager.cs(22,22): error CS0234: The type or namespace name 'Multiplay' does not exist in the namespace 'Unity.Services' (are you missing an assembly reference?)

相关源码(MultiplayManager.cs)

#if (UNITY_ANDROID || UNITY_IOS ) && !UNITY_EDITOR
using System;
using UnityEngine;

namespace TPSBR
{
    /// <summary>
    /// MultiplayManager works only on server
    /// </summary>
    public sealed class MultiplayManager : MonoBehaviour
    {
        public async void StartMultiplay(SessionRequest sessionRequest, StandaloneConfiguration configuration)
        {
            throw new NotSupportedException();
        }
    }
}
#else
using System.Threading.Tasks;
using Unity.Services.Core;
using Unity.Services.Matchmaker.Models;
using Unity.Services.Multiplay;
using UnityEngine;

namespace TPSBR
{
    using System;

    public sealed class MultiplayManager : MonoBehaviour
    {
        // PUBLIC MEMBERS

        public bool QueryProtocol = false;
        public bool Matchmaking = false;
        public bool Backfill = false;
        public int MaxPlayers;
        public MatchmakingResults MatchmakingResults;

        // PRIVATE MEMBERS

        private MultiplayEventCallbacks _multiplayEventCallbacks;
        private IServerEvents _serverEvents;
        private IServerQueryHandler _serverQueryHandler;
        private SessionRequest _sessionRequest;

        private bool _sqpInitialized = false;

        // PUBLIC METHODS

        public async void StartMultiplay(SessionRequest sessionRequest, StandaloneConfiguration configuration)
        {
            _sessionRequest = sessionRequest;
            QueryProtocol = configuration.QueryProtocol;
            Matchmaking = configuration.Matchmaking;
            Backfill = configuration.Backfill;
            MaxPlayers = configuration.MaxPlayers;

            LogServerConfig();

            // Setup allocations
            _multiplayEventCallbacks = new MultiplayEventCallbacks();
            _multiplayEventCallbacks.Allocate += OnAllocate;
            _multiplayEventCallbacks.Deallocate += OnDeallocate;
            _multiplayEventCallbacks.Error += OnError;
            _serverEvents = await MultiplayService.Instance.SubscribeToServerEventsAsync(_multiplayEventCallbacks);

            // It's possible this is a cold start allocation (no event will fire)
            if (!string.IsNullOrEmpty(MultiplayService.Instance.ServerConfig.AllocationId))
            {
                await StartGame();
            }
        }

        // MonoBehaviour INTERFACE

        private async void Awake()
        {
            if (UnityServices.State == ServicesInitializationState.Uninitialized)
            {
                await UnityServices.InitializeAsync();
            }
        }

        private void Update()
        {
            if (QueryProtocol)
            {
                UpdateSqp();
            }
        }

        // PRIVATE METHODS

        private void UpdateSqp()
        {
            if (!_sqpInitialized) InitializeSqp();
            _serverQueryHandler.CurrentPlayers = 1;// (ushort)Global.Networking.PeerCount;
            _serverQueryHandler.UpdateServerCheck();
        }

        private async void InitializeSqp()
        {
            _serverQueryHandler = await MultiplayService.Instance.StartServerQueryHandlerAsync(
                (ushort)_sessionRequest.MaxPlayers,
                _sessionRequest.DisplayName,
                _sessionRequest.GameplayType.ToString().ToLowerInvariant(),
                Application.version,
                "mapname");
            _sqpInitialized = true;
        }

        private void OnError(MultiplayError error)
        {
            LogServerConfig();
            throw new NotImplementedException();
        }

        private void OnDeallocate(MultiplayDeallocation deallocation)
        {
            Debug.Log("Deallocated");
            LogServerConfig();

            MatchmakingResults = null;

            // Hack for now, just exit the application on deallocate
            Application.Quit();
        }

        private async void OnAllocate(MultiplayAllocation allocation)
        {
            Debug.Log("Allocated");
            LogServerConfig();

            await StartGame();
        }

        private async Task StartGame()
        {
            if (!Matchmaking)
            {
                Debug.Log("Matchmaking not enabled, just starting a game");
                Global.Networking.StartGame(_sessionRequest);
                return;
            }

            MatchmakingResults = await MultiplayService.Instance.GetPayloadAllocationFromJsonAs<MatchmakingResults>();
            Debug.Log($"Game produced by matchmaker generator {MatchmakingResults.GeneratorName}, Queue {MatchmakingResults.QueueName}, Pool {MatchmakingResults.PoolName}, BackfillTicketId {MatchmakingResults.BackfillTicketId}");

            _sessionRequest.SessionName = "mm-" + MatchmakingResults.MatchId;
            Global.Networking.StartGame(_sessionRequest);

            while(!Global.Networking.IsConnected)
            {
                await Task.Delay(250);
            }

            if(QueryProtocol)
            {
                Debug.Log("IMultiplayService.ReadyServerForPlayersAsync()");
                await MultiplayService.Instance.ReadyServerForPlayersAsync();
            }
        }

        private void LogServerConfig()
        {
            var serverConfig = MultiplayService.Instance.ServerConfig;
            Debug.Log($"Server ID[{serverConfig.ServerId}], AllocationId[{serverConfig.AllocationId}], Port[{serverConfig.Port}], QueryPort[{serverConfig.QueryPort}], LogDirectory[{serverConfig.ServerLogDirectory}]");
        }
    }
}
#endif

已尝试的解决方法

  • 搜索相关问题仅找到无答案的讨论帖
  • 修改预处理器指令为#if (UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR,但问题未解决

解决思路

  1. 确认Multiplay平台支持性:Unity Multiplay服务仅用于服务器端部署,完全不支持WebGL客户端,WebGL构建时必须让编译器完全跳过Multiplay相关的命名空间和代码逻辑。
  2. 修正预处理器分支逻辑:
    确保WebGL平台进入抛出NotSupportedException的分支,同时检查是否有其他代码路径在WebGL下调用了MultiplayManager的StartMultiplay方法,若有需同步添加平台判断,避免执行到不支持的逻辑。
  3. 清理Multiplay相关包引用:
    在Package Manager中检查是否安装了Unity Multiplay相关包,WebGL客户端不需要这些包,直接移除;若需保留服务器端使用,可在包的Platform Settings中取消勾选WebGL平台,避免WebGL构建时包含这些包。
  4. 检查程序集定义设置:
    若项目使用程序集定义,找到包含Multiplay相关代码的程序集,在其平台设置中取消WebGL的勾选,确保WebGL构建时不会加载该程序集。
  5. 清理构建缓存并重建:
    执行File -> Build Settings -> Clean Build,清除旧的编译缓存后重新生成WebGL项目,避免缓存导致的编译残留问题。

内容的提问来源于stack exchange,提问作者user24314113

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 01:15:56