如何修复 "Cannot implicitly convert type 'void' to 'UnityEngine.AsyncOperation"?

How to fix "Cannot implicitly convert type 'void' to 'UnityEngine.AsyncOperation"?

我试过将通常的单人游戏加载条码改成适用于使用 PHOTON 的多人游戏的条码。下面是我试过的代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.SceneManagement;

public class Launcher : MonoBehaviourPunCallbacks
{
    [SerializeField]
    public Button CreateNameButton;
    
    [SerializeField]
    private Slider LoadingSlider;

    void Awake()
    {
     PhotonNetwork.AutomaticallySyncScene = true;
    }

    public void loadLevels(int sceneIndex)
    {
        StartCoroutine(loadSelectedScene(sceneIndex));
    }

    IEnumerator loadSelectedScene(int sceneIndex)
    {
        
        AsyncOperation async = PhotonNetwork.LoadLevel(sceneIndex);

        while (!async.isDone){
            float progress = Mathf.Clamp01(async.progress / .9f);
            LoadingSlider.value = async.progress;
            yield return null;

       }
     }
}

我收到错误:

Cannot implicitly convert type 'void' to 'UnityEngine.AsyncOperation'

您不能将 PhotonNetwork.LoadLevel 分配给 AsyncOperation。所以,你需要使用

AsyncOperation asyncOperation = PhotonNetwork.LoadLevelAsync(sceneIndex);

但是,在 PUN 2 Unity 中,LoadLevelAsync 已被删除,因此您可以使用 PhotonNetwork.LoadLevel()