简体   繁体   English

Unity 5.4中的ScriptableObject.ctor和LoadAssetAtPath错误

[英]ScriptableObject.ctor and LoadAssetAtPath error in unity 5.4

I am getting these three errors each time i playing my scene. 每次播放场景时都会出现这三个错误。 There error making no problem but i am unable to understand that why i am getting this. 有没有问题的错误,但我不明白为什么我得到了这个。

1 1

ScriptableObject.ctor is not allowed to be called from a ScriptableObject constructor (or instance field initializer), call it in OnEnable instead. 不允许从ScriptableObject构造函数(或实例字段初始化程序)中调用ScriptableObject.ctor,而应在OnEnable中调用它。 Called from ScriptableObject 'Find_Object_by_component'. 从ScriptableObject'Find_Object_by_component'调用。 See "Script Serialization" page in the Unity Manual for further details. 有关更多详细信息,请参见《 Unity手册》中的“脚本序列化”页面。 UnityEditor.SceneView:.ctor() Find_Object_by_component:.ctor() UnityEditor.SceneView:.ctor()Find_Object_by_component:.ctor()

2 2

LoadAssetAtPath is not allowed to be called from a ScriptableObject constructor (or instance field initializer), call it in OnEnable instead. 不允许从ScriptableObject构造函数(或实例字段初始化程序)中调用LoadAssetAtPath,而应在OnEnable中调用它。 Called from ScriptableObject 'Find_Object_by_component'. 从ScriptableObject'Find_Object_by_component'调用。 See "Script Serialization" page in the Unity Manual for further details. 有关更多详细信息,请参见《 Unity手册》中的“脚本序列化”页面。 UnityEditor.SceneView:.ctor() Find_Object_by_component:.ctor() UnityEditor.SceneView:.ctor()Find_Object_by_component:.ctor()

3 3

UnityException: LoadAssetAtPath is not allowed to be called from a ScriptableObject constructor (or instance field initializer), call it in OnEnable instead. UnityException:不允许从ScriptableObject构造函数(或实例字段初始值设定项)调用LoadAssetAtPath,而应在OnEnable中调用它。 Called from ScriptableObject 'Find_Object_by_component'. 从ScriptableObject'Find_Object_by_component'调用。 See "Script Serialization" page in the Unity Manual for further details. 有关更多详细信息,请参见《 Unity手册》中的“脚本序列化”页面。 UnityEditor.EditorGUIUtility.Load (System.String filename, System.Type type) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:470) UnityEditor.EditorGUIUtility.Load (System.String path) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:464) UnityEditor.EditorGUIUtility.LoadGeneratedIconOrNormalIcon (System.String name) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:182) UnityEditor.EditorGUIUtility.LoadIconForSkin (System.String name, Int32 skinIndex) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:206) UnityEditor.EditorGUIUtility.LoadIcon (System.String name) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:177) UnityEditor.EditorGUIUtility.LoadIconRequired (System.String name) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIU UnityEditor.EditorGUIUtility.Load(System.String文件名,System.Type类型)(位于C:/buildslave/unity/build/artifacts/Generated/common/editor/EditorGUIUtilityBindings.gen.cs:470)UnityEditor.EditorGUIUtility.Load(系统.String路径)(在C:/buildslave/unity/build/artifacts/Generated/common/editor/EditorGUIUtilityBindings.gen.cs:464)UnityEditor.EditorGUIUtility.LoadGeneratedIconOrNormalIcon(System.String名称)(在C:/ buildslave / unity /build/artifacts/generation/common/editor/EditorGUIUtilityBindings.gen.cs:182)UnityEditor.EditorGUIUtility.LoadIconForSkin(System.String名称,Int32 skinIndex)(在C:/ buildslave / unity / build / artifacts / Generated / common / editor / EditorGUIUtilityBindings.gen.cs:206)UnityEditor.EditorGUIUtility.LoadIcon(System.String名称)(位于C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:177)UnityEditor。 EditorGUIUtility.LoadIconRequired(System.String name)(在C:/ buildslave / unity / build / artifacts / generated / common / editor / EditorGUIU tilityBindings.gen.cs:167) UnityEditor.EditorGUIUtility.TextContentWithIcon (System.String textAndTooltip, System.String icon) (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:117) UnityEditor.EditorWindow.GetLocalizedTitleContentFromType (System.Type t) (at C:/buildslave/unity/build/Editor/Mono/EditorWindow.cs:119) UnityEditor.EditorWindow.GetLocalizedTitleContent () (at C:/buildslave/unity/build/Editor/Mono/EditorWindow.cs:102) UnityEditor.SceneView.OnEnable () (at C:/buildslave/unity/build/Editor/Mono/SceneView/SceneView.cs:350) UnityEditor.SceneView:.ctor() Find_Object_by_component:.ctor() tilityBindings.gen.cs:167)UnityEditor.EditorGUIUtility.TextContentWithIcon(System.String textAndTooltip,System.String图标)(位于C:/buildslave/unity/build/artifacts/generated/common/editor/EditorGUIUtilityBindings.gen.cs:117 )UnityEditor.EditorWindow.GetLocalizedTitleContentFromType(System.Type t)(在C:/buildslave/unity/build/Editor/Mono/EditorWindow.cs:119)UnityEditor.EditorWindow.GetLocalizedTitleContent()(在C:/ buildslave / unity / build /Editor/Mono/EditorWindow.cs:102)UnityEditor.SceneView.OnEnable()(在C:/buildslave/unity/build/Editor/Mono/SceneView/SceneView.cs:350)UnityEditor.SceneView:.ctor()Find_Object_by_component :.ctor()

A ScriptableObject constructor should not be used for anything, unless you know exactly what you're doing and why you need to break the rules. 除非您确切知道自己在做什么以及为什么需要打破规则,否则ScriptableObject构造函数不应用于任何事情。 The reason is that ScriptableObjects aren't constructed in the main Unity thread, thus you can't really use the Unity API. 原因是ScriptableObjects不是在Unity主线程中构造的,因此您不能真正使用Unity API。 ( Debug.Log is one of the few Unity functions you can use in any thread.) Debug.Log是可以在任何线程中使用的少数Unity函数之一。)

Use the Awake or OnEnable functions for initialization. 使用AwakeOnEnable函数进行初始化。 If it's a MonoBehaviour , you can also use Start . 如果是MonoBehaviour ,则还可以使用Start These are called automatically by the game engine. 这些由游戏引擎自动调用。

If you really can't find the offending class, you can use find/replace to change all the function calls from AssetDatabase.LoadAssetAtPath to MyDebugClass.LoadAssetAtPath . 如果您确实找不到令人讨厌的类,则可以使用find / replace将所有函数调用从AssetDatabase.LoadAssetAtPathMyDebugClass.LoadAssetAtPath Next, define: 接下来,定义:

#line hidden
public static class MyDebugClass
{
  public static Object LoadAssetAtPath(string assetPath, Type type)
  {
    Debug.Log("LoadAssetAtPath called: " + assetPath);
    return AssetDatabase.LoadAssetAtPath(assetPath, type);
  }
}
#line default

This hack will show you each caller, and the message directly before the error will point to the culprit. 此hack将向您显示每个呼叫者,错误之前的消息将直接指向罪魁祸首。 (Not an elegant technique, but it will give you a fast answer. Though I can't check the syntax at the moment.) (这不是一种优雅的技术,但是它将为您提供快速的答案。尽管目前我无法检查语法。)

Note: #line hidden means this function won't show up in the stack trace. 注意: #line hidden表示此功能不会显示在堆栈跟踪中。 So the top line of your stack trace will be the line you're interested in, rather than a line inside MyDebugClass . 因此,堆栈跟踪的第一行将是您感兴趣的行,而不是MyDebugClass内的MyDebugClass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM