构建Player时出错,因为脚本在编辑器中存在编译错误

时间:2017-07-19 22:29:14

标签: c# unity3d unity5

我在构建游戏时遇到问题。它一直说'错误构建播放器,因为脚本在编辑器中有编译错误'

我按照this提示,仍然没有任何工作。

朋友建议我添加

public static class EditorUtil

{

#if UNITY_EDITOR
    //Editr code here
    #endif 
}

并且仍然会继续出现相同的错误。

这是完整的代码:

 using UnityEngine;

 using System.Collections.Generic;

#if UNITY_EDITOR
using UnityEditor;
#endif



public static class EditorUtil
 {


#if UNITY_EDITOR
    //Editr code here

    static string m_EditorResourcesPath = string.Empty;
    private static string path = string.Empty;

    internal static string editorResourcesPath
    {



        get
        {
            if (string.IsNullOrEmpty(m_EditorResourcesPath))
            {
                string path;

                if (SearchForEditorResourcesPath(out path))
                    m_EditorResourcesPath = path;
                else
                    Debug.LogError("Unable to locate editor resources. Make sure the PostProcessing package has been installed correctly.");
            }

            return m_EditorResourcesPath;
        }
    }

    static bool SearchForEditorResourcesPath(out string path)
    {
        path = string.Empty;

        string searchStr = EditorUtil.path;
        string str = null;

        foreach (var assetPath in AssetDatabase.GetAllAssetPaths())
        {
            if (assetPath.Contains(searchStr))
            {
                str = assetPath;
                break;
            }
        }

        if (str == null)
            return false;

        path = str.Substring(0, str.LastIndexOf(searchStr) + searchStr.Length);
        return true;
    }

    internal static T Load<T>(string path, string name) where T : Object
    {
        EditorUtil.path = path;
        return AssetDatabase.LoadAssetAtPath<T>(editorResourcesPath + name);
    }

    private static List<string> layers;
    private static string[] layerNames;

    public static LayerMask LayerMaskField(string label, LayerMask selected)
    {
        if (layers == null) 
        {
            layers = new List<string>();
            layerNames = new string[4];
        }
        else 
        {
            layers.Clear ();
        }

        int emptyLayers = 0;
        for (int i = 0; i < 32; i++) 
        {
            string layerName = LayerMask.LayerToName (i);

            if (layerName != "") 
            { 
                layers.Add (layerName);
            } 
            else
            {
                emptyLayers++;
            }
        }

        if (layerNames.Length != layers.Count)
        {
            layerNames = new string[layers.Count];
        }

        for (int i=0; i < layerNames.Length; i++) 
            layerNames[i] = layers[i];

        selected.value =  EditorGUILayout.MaskField (label, selected.value, layerNames);
        return selected;
    }

    public static Rect GetCurrentRect (float fieldSize = 18)
    {
        return GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.Height(fieldSize));
    }

    public static GameObject GetSelectedGameObject ()
    {
        return Selection.activeGameObject;
    }



    public static AnimationClip GetAnimationClipFromAnimator(Animator animator, string name)
    {
        if (animator == null)
            return null;

        foreach (AnimationClip animClip in animator.runtimeAnimatorController.animationClips)
        {
            if (animClip.name == name)
                return animClip;
        }
        return null;
    }

#endif 
}

这是一个解决方案吗?我已经在这里和其他论坛搜索了很多帖子,但我仍然遇到同样的错误。

example error

修改:second pic

2 个答案:

答案 0 :(得分:1)

从截图中判断,您将脚本放入

资产/叛徒/脚本/ utils的

但是,必须将Editor脚本放入Editor文件夹,而不是放入Assets文件夹。

请参阅:https://docs.unity3d.com/Manual/SpecialFolders.html

Editor文件夹外的脚本无法访问UnityEditor程序集。 #if UNITY_EDITOR预处理器指令没有解决这个问题,因为当脚本在Unity Editor中运行时,它的计算结果为true。

答案 1 :(得分:1)

由于以上脚本为using UnityEditor,您必须将此脚本放在名为编辑器的文件夹中,否则您必须使用定义#if UNITY_Editor。你所做的也是正确的,但我认为你可能从某个地方调用这些方法,也许这就是为什么它仍然给出编译错误。它的作用是使这些方法仅在编辑器中存在,它不会在构建中存在。因此,我只使用#if UNITY_EDITOR作为代码块而不是整个方法。

using UnityEngine;

using System.Collections.Generic;

#if UNITY_EDITOR
using UnityEditor;
#endif



public static class EditorUtil
{



    static string m_EditorResourcesPath = string.Empty;
    private static string path = string.Empty;

    internal static string editorResourcesPath {



        get {
            if (string.IsNullOrEmpty (m_EditorResourcesPath)) {
                string path;

                if (SearchForEditorResourcesPath (out path))
                    m_EditorResourcesPath = path;
                else
                    Debug.LogError ("Unable to locate editor resources. Make sure the PostProcessing package has been installed correctly.");
            }

            return m_EditorResourcesPath;
        }
    }

    static bool SearchForEditorResourcesPath (out string path)
    {
        path = string.Empty;

        string searchStr = EditorUtil.path;
        string str = null;
        #if UNITY_EDITOR
        foreach (var assetPath in AssetDatabase.GetAllAssetPaths()) {
            if (assetPath.Contains (searchStr)) {
                str = assetPath;
                break;
            }
        }
        #endif
        if (str == null)
            return false;

        path = str.Substring (0, str.LastIndexOf (searchStr) + searchStr.Length);
        return true;
    }

    internal static T Load<T> (string path, string name) where T : Object
    {
        #if UNITY_EDITOR
        EditorUtil.path = path;
        return AssetDatabase.LoadAssetAtPath<T> (editorResourcesPath + name);
        #else
    return null
        #endif
    }

    private static List<string> layers;
    private static string[] layerNames;

    public static LayerMask LayerMaskField (string label, LayerMask selected)
    {
        if (layers == null) {
            layers = new List<string> ();
            layerNames = new string[4];
        } else {
            layers.Clear ();
        }

        int emptyLayers = 0;
        for (int i = 0; i < 32; i++) {
            string layerName = LayerMask.LayerToName (i);

            if (layerName != "") { 
                layers.Add (layerName);
            } else {
                emptyLayers++;
            }
        }

        if (layerNames.Length != layers.Count) {
            layerNames = new string[layers.Count];
        }

        for (int i = 0; i < layerNames.Length; i++)
            layerNames [i] = layers [i];
        #if UNITY_EDITOR
        selected.value = EditorGUILayout.MaskField (label, selected.value, layerNames);
        #endif
        return selected;
    }

    public static Rect GetCurrentRect (float fieldSize = 18)
    {
        return GUILayoutUtility.GetRect (GUIContent.none, GUIStyle.none, GUILayout.Height (fieldSize));
    }

    public static GameObject GetSelectedGameObject ()
    {
        #if UNITY_EDITOR
        return Selection.activeGameObject;
        #else
    return null
        #endif
    }



    public static AnimationClip GetAnimationClipFromAnimator (Animator animator, string name)
    {
        if (animator == null)
            return null;

        foreach (AnimationClip animClip in animator.runtimeAnimatorController.animationClips) {
            if (animClip.name == name)
                return animClip;
        }
        return null;
    }

}