Created
June 2, 2019 21:39
-
-
Save dmbfm/41f25a23f17daa242daa70c9b18f7484 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.Experimental.AssetImporters; | |
using System.IO; | |
using System.Xml; | |
using System.Collections.Generic; | |
[ScriptedImporter(1, "kml")] | |
public class KMLPathImporter : ScriptedImporter | |
{ | |
// public float m_Scale = 1; | |
public override void OnImportAsset(AssetImportContext ctx) | |
{ | |
XmlDocument xmlDoc = new XmlDocument(); | |
xmlDoc.LoadXml(File.ReadAllText(ctx.assetPath)); | |
XmlNodeList nodes = xmlDoc.GetElementsByTagName("coordinates"); | |
Debug.Log("ok: " + nodes[0].InnerText); | |
char[] splitters = { ',', ' ' }; | |
var coordStringArray = nodes[0].InnerText.Split(splitters, System.StringSplitOptions.RemoveEmptyEntries); | |
var coordStringList = new List<string>(); | |
for (int i = 0; i < coordStringArray.Length; i++) | |
{ | |
if (coordStringArray[i].Trim() != "") | |
{ | |
coordStringList.Add(coordStringArray[i]); | |
Debug.Log(i + ": " + coordStringList[i]); | |
} | |
} | |
int pathCount = coordStringList.Count / 3; | |
Location[] locations = new Location[pathCount]; | |
Debug.Log("coordStringLength: " + coordStringList.Count + "; pathCount = " + pathCount); | |
var j = 0; | |
for (int i = 0; i < coordStringList.Count; i+=3) | |
{ | |
locations[j] = new Location( | |
double.Parse(coordStringList[i + 1], System.Globalization.CultureInfo.InvariantCulture), | |
double.Parse(coordStringList[i], System.Globalization.CultureInfo.InvariantCulture), | |
double.Parse(coordStringList[i + 2], System.Globalization.CultureInfo.InvariantCulture) | |
); | |
Debug.Log("Loc: " + j + ": " + locations[j].ToString()); | |
j++; | |
} | |
var path = new LocationPath(); | |
path.locations = locations; | |
ctx.AddObjectToAsset("main obj", path); | |
ctx.SetMainObject(path); | |
Debug.Log(ctx.assetPath); | |
var f = ctx.assetPath.Split('.'); | |
var finalName = f[0] + ".asset"; | |
AssetDatabase.CreateAsset(path, finalName); | |
//var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); | |
//var position = JsonUtility.FromJson<Vector3>(File.ReadAllText(ctx.assetPath)); | |
//cube.transform.position = position; | |
//cube.transform.localScale = new Vector3(m_Scale, m_Scale, m_Scale); | |
//// 'cube' is a a GameObject and will be automatically converted into a prefab | |
//// (Only the 'Main Asset' is elligible to become a Prefab.) | |
//ctx.AddObjectToAsset("main obj", cube); | |
//ctx.SetMainObject(cube); | |
//var material = new Material(Shader.Find("Standard")); | |
//material.color = Color.red; | |
//// Assets must be assigned a unique identifier string consistent across imports | |
//ctx.AddObjectToAsset("my Material", material); | |
//// Assets that are not passed into the context as import outputs must be destroyed | |
//var tempMesh = new Mesh(); | |
//DestroyImmediate(tempMesh); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment