简体   繁体   中英

Unity throws an error “CS0246: The type or namespace name ‘SharpKml’ could not be found

I am researching how to generate environments in Unity using GIS data in KML format. I came across the SharpKML plugin and it seems to be ideal for my needs.

However, I am experiencing a strange error in that Unity throws an error

“CS0246: The type or namespace name 'SharpKml' could not be found (are you missing a using directive or an assembly reference?)”

The reference is added to VS and I have using SharpKML.Dom and using SharpKML.Engine entries which compile in VS with no problems.

But Unity still throws the error.

I have installed via NuGet and have also downloaded the SharpKML source code and rebuilt the dll on my machine and referenced directly with no change. VS also seems to drop the reference intermittently.

Have you come across this problem before or have any idea what is causing it?

The version of Unity is 2019.1.4f1 and the the version of VS is 2017 running framework 4.7.03062

I have recreated the project on a different machine on a different network and experience the same problem.

using UnityEngine;
using System.IO;
using System.Linq;
using SharpKml.Dom;
using SharpKml.Engine;

public class RenderKML : MonoBehaviour
{
    public string KLMPath;

    // Start is called before the first frame update
    void Start()
    {
        string kmlPth = "Assets\\kml";
        GetKMLFiles(kmlPth);
    }

    private void GetKMLFiles(string pth)
    {
        if (pth != null)
        {
            DirectoryInfo dir = new DirectoryInfo(pth);
            FileInfo[] info = dir.GetFiles("*.kml");
            foreach (FileInfo f in info)
            {
                print(f.FullName);
                GetKMLData(f);
            }

        }
    }

    private void GetKMLData(FileInfo fI)
    {
       // This will read a Kml file into memory.
        Stream fs = new FileStream(fI.FullName, FileMode.Open);
        KmlFile file = KmlFile.Load(fs);

        Kml kml = file.Root as Kml;
        if (kml != null)
        {
            foreach (var placemark in kml.Flatten().OfType<Placemark>())
            {
                print(placemark.Name);
            }
        }
    }
}

Every time you press "Run", unity rewrites project files. You cant simply use nuget or add references from external projects. You should download all SharpKML dll files and put them in Assets folder manually. See this for more information: https://answers.unity.com/questions/458300/how-to-use-a-external-dll.html

You need to use the Plugin folder in Unity3D . Download your Package or DLL and put it there.

This link can be helpful

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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