I’m just writing a simple XML well-formed utility and Selection.activeObject isn’t passing back TextAsset that I can grab the text content from. Why is this the case? It would seem fairly intuitive for it to function in this way.
Am I missing something? Are TextAssets only for runtime?
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Xml;
using System.IO;
public class XMLUtility : Editor {
[MenuItem("Assets/xml/Check XML File")]
private static void CheckXMLFile()
{
//Selection.activeObject
string filepath = AssetDatabase.GetAssetPath(Selection.activeObject);
//hdrpath = Path.ChangeExtension(filepath,".xml");
//string extension = Path.GetExtension(filepath);
Debug.Log("XMLUtility:" + filepath);
Debug.Log("XMLUtility:" + Selection.activeObject.GetType());
//TextAsset ta = Selection.activeObject as TextAsset;
TextAsset ta = AssetDatabase.LoadMainAssetAtPath(filepath) as TextAsset;
if (ta != default(TextAsset))
{
Test(ta.text);
}
else
{
Debug.LogWarning("XMLUtility: No TextAsset selected");
}
}
static void Test(string xml) {
using (XmlReader xr = XmlReader.Create(
new StringReader(xml))) {
try {
while (xr.Read()) { }
Debug.Log("XMLUtility: XML Is Valid");
} catch (Exception ex) {
Debug.LogError("XMLUtility: XML is not Valid: " + ex.Message);
}
}
}
// Note that we pass the same path, and also pass "true" to the second argument.
[MenuItem("Assets/xml/Check XML File", true)]
private static bool CheckXMLFileValidation()
{
// This returns true when the selected object is a Texture2D (the menu item will be disabled otherwise).
if ( Selection.activeObject.GetType() == typeof(DefaultAsset))
{
string filepath = AssetDatabase.GetAssetPath(Selection.activeObject);
string extension = Path.GetExtension(filepath);
//Debug.Log("XMLUtility: CheckXMLFileValidation" + extension);
if (extension == ".xml") {
return true;
}
}
return false;
}
}