gpt4 book ai didi

c# - Unity3D 中可能存在错误?

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:57 27 4
gpt4 key购买 nike

显然,在 Unity3D 更新后,我无法再从一个场景切换到另一个场景。我不知道这是否是由于更新导致的问题,但目前当我点击一个应该让我返回到另一个场景的按钮时,我收到了这个错误。

ArgumentException: method return type is incompatible
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:190)
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:276)
UnityEngineInternal.NetFxCoreExtensions.CreateDelegate (System.Reflection.MethodInfo self, System.Type delegateType, System.Object target) (at /Users/builduser/buildslave/unity/build/Runtime/Export/WinRT/NetFxCoreExtensions.cs:11)
UnityEngine.Events.InvokableCall..ctor (System.Object target, System.Reflection.MethodInfo theFunction) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:149)
UnityEngine.Events.PersistentCall.GetRuntimeCall (UnityEngine.Events.UnityEventBase theEvent) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:447)
UnityEngine.Events.PersistentCallGroup.Initialize (UnityEngine.Events.InvokableCallList invokableList, UnityEngine.Events.UnityEventBase unityEventBase) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:610)
UnityEngine.Events.UnityEventBase.RebuildPersistentCallsIfNeeded () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:776)
UnityEngine.Events.UnityEventBase.PrepareInvoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:812)
UnityEngine.Events.UnityEvent.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()

目前错误发生在2018.1.0f2版本,不幸的是我不记得我以前使用的版本。

这是与负责场景切换的脚本相关的代码,它工作正常:

using UnityEngine.SceneManagement;
using UnityEngine;

public class MainEngine : MonoBehaviour {

// Use this for initialization
void Start () {
DontDestroyOnLoad(this.gameObject);
SceneManager.activeSceneChanged += ChangedActiveScene;
SceneManager.LoadScene("Introduction");
}

// Update is called once per frame
void Update () {

}

private void ChangedActiveScene(Scene current, Scene next)
{
string currentName = current.name;

if (currentName == null)
{
// Scene1 has been removed
currentName = "Replaced";
}

Debug.Log("Scenes: " + currentName + ", " + next.name);
}
}

但以下是“有罪”代码:

using UnityEngine.SceneManagement;
using UnityEngine;
using System.Collections;

public class InstructionsScene : MonoBehaviour {

IEnumerator PlayNow()
{
Debug.Log("scene should be changed");

float fadeTime = GameObject.Find("SceneEngine").GetComponent<Fading>().BeginFade(1);
yield return new WaitForSeconds(fadeTime);
SceneManager.LoadScene("Instructions", LoadSceneMode.Single);
}

}

最佳答案

按钮(通常可能是 UI 元素)的回调必须是返回类型 void。您的是返回类型 IEnumerator(如果 PlayNow 是您从按钮调用的函数)。

解决方法是从 PlayNow 中启动协程:

public void PlayNow()
{
Debug.Log("scene should be changed");

StartCoroutine(PlayNowCoroutine());
}

IEnumerator PlayNowCoroutine()
{
float fadeTime = GameObject.Find("SceneEngine").GetComponent<Fading>().BeginFade(1);
yield return new WaitForSeconds(fadeTime);
SceneManager.LoadScene("Instructions", LoadSceneMode.Single);
}

关于c# - Unity3D 中可能存在错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51495793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com