类型或命名空间`UnityEditor'找不到

10

请帮我纠正这个问题。

Assets/Menu.cs(97,73): 警告 CS0618: UnityEditor.EditorUtility.GetAssetPath(UnityEngine.Object)' 已过时:请使用 AssetDatabase.GetAssetPath'

构建玩家失败,因为脚本存在编译错误。

Assets/Menu.cs(2,7): 错误 CS0246: 无法找到类型或命名空间名称`UnityEditor'。 是否缺少 using 指令或程序集引用?

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System;
using System.Linq;

struct ObjMaterial
{
    public string name;
    public string textureName;
}

public class Menu : MonoBehaviour { 

    public int window; 

    void Start () { 
        window = 1;

    } 

    private static int vertexOffset = 0;
    private static int normalOffset = 0;
    private static int uvOffset = 0;


    //User should probably be able to change this. It is currently left as an excercise for
    //the reader.
    private static string targetFolder = "ExportedObj";


    private static string MeshToString(Component mf, Dictionary<string, ObjMaterial> materialList) 
    {



        Mesh m;
        Material[] mats;

        if(mf is MeshFilter)
        {
            m = (mf as MeshFilter).mesh;
            mats = mf.GetComponent<Renderer>().sharedMaterials;
        }
        else if(mf is SkinnedMeshRenderer)
        {
            m = (mf as SkinnedMeshRenderer).sharedMesh;
            mats = (mf as SkinnedMeshRenderer).sharedMaterials;
        }
        else
        {
            return "";
        }

        StringBuilder sb = new StringBuilder();

        sb.Append("g ").Append(mf.name).Append("\n");
        foreach(Vector3 lv in m.vertices) 
        {
            Vector3 wv = mf.transform.TransformPoint(lv);

            //This is sort of ugly - inverting x-component since we're in
            //a different coordinate system than "everyone" is "used to".
            sb.Append(string.Format("v {0} {1} {2}\n",-wv.x,wv.y,wv.z));
        }
        sb.Append("\n");

        foreach(Vector3 lv in m.normals) 
        {
            Vector3 wv = mf.transform.TransformDirection(lv);

            sb.Append(string.Format("vn {0} {1} {2}\n",-wv.x,wv.y,wv.z));
        }
        sb.Append("\n");

        foreach(Vector3 v in m.uv) 
        {
            sb.Append(string.Format("vt {0} {1}\n",v.x,v.y));
        }

        for (int material=0; material < m.subMeshCount; material ++) {
            sb.Append("\n");
            sb.Append("usemtl ").Append(mats[material].name).Append("\n");
            sb.Append("usemap ").Append(mats[material].name).Append("\n");

            //See if this material is already in the materiallist.
            try
            {
                ObjMaterial objMaterial = new ObjMaterial();

                objMaterial.name = mats[material].name;


                objMaterial.textureName = EditorUtility.GetAssetPath(mats[material].mainTexture);
                //else 
                    //objMaterial.textureName = null;

                materialList.Add(objMaterial.name, objMaterial);
            }
            catch (ArgumentException)
            {
                //Already in the dictionary
            }


            int[] triangles = m.GetTriangles(material);
            for (int i=0;i<triangles.Length;i+=3) 
            {
                //Because we inverted the x-component, we also needed to alter the triangle winding.
                sb.Append(string.Format("f {1}/{1}/{1} {0}/{0}/{0} {2}/{2}/{2}\n", 
                    triangles[i]+1 + vertexOffset, triangles[i+1]+1 + normalOffset, triangles[i+2]+1 + uvOffset));
            }
        }

        vertexOffset += m.vertices.Length;
        normalOffset += m.normals.Length;
        uvOffset += m.uv.Length;

        return sb.ToString();
    }

    private static void Clear()
    {
        vertexOffset = 0;
        normalOffset = 0;
        uvOffset = 0;
    }

    private static Dictionary<string, ObjMaterial> PrepareFileWrite()
    {
        Clear();

        return new Dictionary<string, ObjMaterial>();
    }

    private static void MaterialsToFile(Dictionary<string, ObjMaterial> materialList, string folder, string filename)
    {
        using (StreamWriter sw = new StreamWriter(folder + "/" + filename + ".mtl")) 
        {
            foreach( KeyValuePair<string, ObjMaterial> kvp in materialList )
            {
                sw.Write("\n");
                sw.Write("newmtl {0}\n", kvp.Key);
                sw.Write("Ka  0.6 0.6 0.6\n");
                sw.Write("Kd  0.6 0.6 0.6\n");
                sw.Write("Ks  0.9 0.9 0.9\n");
                sw.Write("d  1.0\n");
                sw.Write("Ns  0.0\n");
                sw.Write("illum 2\n");

                if (kvp.Value.textureName != null)
                {
                    string destinationFile = kvp.Value.textureName;


                    int stripIndex = destinationFile.LastIndexOf('/');//FIXME: Should be Path.PathSeparator;

                    if (stripIndex >= 0)
                        destinationFile = destinationFile.Substring(stripIndex + 1).Trim();


                    string relativeFile = destinationFile;

                    destinationFile = folder + "/" + destinationFile;

                    Debug.Log("Copying texture from " + kvp.Value.textureName + " to " + destinationFile);

                    try
                    {
                        //Copy the source file
                        File.Copy(kvp.Value.textureName, destinationFile);
                    }
                    catch
                    {

                    }   


                    sw.Write("map_Kd {0}", relativeFile);
                }

                sw.Write("\n\n\n");
            }
        }
    }

    private static void MeshToFile(Component mf, string folder, string filename) 
    {
        Dictionary<string, ObjMaterial> materialList = PrepareFileWrite();

        using (StreamWriter sw = new StreamWriter(folder +"/" + filename + ".obj")) 
        {
            sw.Write("mtllib ./" + filename + ".mtl\n");

            sw.Write(MeshToString(mf, materialList));
        }

        MaterialsToFile(materialList, folder, filename);
    }

    private static void MeshesToFile(Component[] mf, string folder, string filename) 
    {
        Dictionary<string, ObjMaterial> materialList = PrepareFileWrite();

        using (StreamWriter sw = new StreamWriter(folder +"/" + filename + ".obj")) 
        {
            sw.Write("mtllib ./" + filename + ".mtl\n");

            for (int i = 0; i < mf.Length; i++)
            {
                sw.Write(MeshToString(mf[i], materialList));
            }
        }

        MaterialsToFile(materialList, folder, filename);
    }

    private static bool CreateTargetFolder()
    {
        try
        {
            System.IO.Directory.CreateDirectory(targetFolder);
        }
        catch
        {
            //EditorUtility.DisplayDialog("Error!", "Failed to create target folder!", "");
            return false;
        }

        return true;
    }



    void OnGUI () { 
        GUI.BeginGroup (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 100, 200, 200)); 
        if(window == 1) 
        { 
            if(GUI.Button (new Rect (10,30,180,30), "Экспортировать")) 
            { 

                if (!CreateTargetFolder())
                    return;

                //GameObject[] gos = GameObject.FindGameObjectsWithTag("Boat");
                //Selection.objects = gos;

                GameObject[] selection = GameObject.FindGameObjectsWithTag("Boat");
                //Transform[] selection = Selection.GetTransforms(SelectionMode.Editable | SelectionMode.ExcludePrefab);

                if (selection.Length == 0)
                {
                    //EditorUtility.DisplayDialog("No source object selected!", "Please select one or more target objects", "");
                    return;
                }

                int exportedObjects = 0;

                ArrayList mfList = new ArrayList();

                for (int i = 0; i < selection.Length; i++)
                {
                    Component[] meshfilter = selection[i].GetComponentsInChildren(typeof(MeshFilter)).Concat(selection[i].GetComponentsInChildren(typeof(SkinnedMeshRenderer))).ToArray();

                    for (int m = 0; m < meshfilter.Length; m++)
                    {
                        exportedObjects++;
                        mfList.Add(meshfilter[m]);
                    }
                }

                if (exportedObjects > 0) 
                {
                    Component[] mf = new Component[mfList.Count];

                    for (int i = 0; i < mfList.Count; i++) {
                        mf [i] = (Component)mfList [i];
                    }

                    string filename = /*EditorApplication.currentScene +*/ "_" + exportedObjects;

                    int stripIndex = filename.LastIndexOf ('/');//FIXME: Should be Path.PathSeparator

                    if (stripIndex >= 0)
                        filename = filename.Substring (stripIndex + 1).Trim ();

                    MeshesToFile (mf, targetFolder, filename);

                }

            } 

            if(GUI.Button (new Rect (10,150,180,30), "Выход")) 
            { 
                window = 5; 
            } 
        } 

        if(window == 5) 
        { 
            GUI.Label(new Rect(50, 10, 180, 30), "Вы уже выходите?");   
            if(GUI.Button (new Rect (10,40,180,30), "Да")) 
            { 
                Application.Quit(); 
            } 
            if(GUI.Button (new Rect (10,80,180,30), "Нет")) 
            { 
                window = 1; 
            } 
        } 
        GUI.EndGroup (); 
    } 
} 

看起来非常奇怪,你会收到一个特定于命名空间的类的警告和找不到命名空间的错误。但是,考虑到这个警告,你为什么不使用AssetDatabase呢? - Jon Skeet
1
http://answers.unity3d.com/questions/576746/build-error-the-type-or-namespace-name-unityeditor.html - Vivek Nuna
但是我可以使用什么函数来替代EditorUtility.GetAssetPath呢? - Ray Rove
@RayRove AssetDatabase.GetAssetPath,也许可以吗? - user2299169
4个回答

18

在使用任何Unity API之前,非常重要的是检查API命名空间。如果命名空间来自于UnityEditor,则它仅适用于编辑器。这是用来制作编辑器插件的。您不能在构建中使用它,而且在为任何平台构建时会引发错误。

根据文档,AssetDatabaseEditorUtility 类都来自于UnityEditor命名空间。

enter image description here

您必须重新设计游戏以使其在没有GetAssetPath函数的情况下工作。您绝对可以制作没有该函数的游戏。我不知道您在做什么,但您应该研究Resources类。这将帮助您在运行时加载GameObject。

要解决当前的问题,

请替换

using UnityEditor;

随着

#if UNITY_EDITOR
using UnityEditor;
#endif

然后替换

objMaterial.textureName = EditorUtility.GetAssetPath(mats[material].mainTexture);

        objMaterial.textureName = "";
#if UNITY_EDITOR
        objMaterial.textureName = EditorUtility.GetAssetPath(mats[material].mainTexture);
#endif

你也可以将菜单(Menu)脚本放在Assets/Editor目录下的一个文件夹中,但请理解这并不能解决您的代码无法在构建中工作的问题。它只能使您的项目能够在不报错的情况下构建。来自UnityEditor命名空间的类仅用于编辑器插件。


1
我正在使用资源,但是它似乎无法正常工作。 - Ray Rove

6
这是因为使用UnityEditor的类需要放置在一个名为Editor的文件夹下:Assets/Editor。如果你这样做,问题就会消失。

1
当我尝试编写一个实现了 UnityEditor.Build.IPostprocessBuildWithReport 接口的类时,我遇到了问题(该接口严格用于在编译 Unity 应用程序后执行微调操作,并不包含在输出构建中)。这解决了我的问题。 - koyae
1
真的吗?为什么会这样?不过还是谢谢你的回答,它节省了我很多时间。 - miljon

1
如果您正在使用程序集定义(dll),这也可能会发生。包含编辑器内容的任何 dll 都不会被包含在构建中。最好为编辑器内容制作一个单独的 dll。

Picture of assembly definition settings


0
在我的情况下,这一颗毒瘤般的代码行不小心被加了进来,可能是某个时候误点了一些设置。
既然我的代码中实际上并不需要Unity编辑器,所以解决方法就是:
  1. 在控制台查看完整的错误信息,找出导致错误的.cs文件:

using UnityEditor.Experimental.GraphView;
  1. 删除或注释该行代码。

  2. 必要时重复上述步骤。

  3. 构建并运行项目,确认一切正常。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接