Search Unity

Why doesn't Selection.activeObject return a TextAsset?

Discussion in 'Immediate Mode GUI (IMGUI)' started by majeric, Nov 7, 2015.

  1. majeric

    majeric

    Joined:
    Aug 17, 2010
    Posts:
    89
    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?



    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System;
    4. using System.Collections;
    5. using System.Xml;
    6. using System.IO;
    7.  
    8. public class XMLUtility : Editor {
    9.  
    10.     [MenuItem("Assets/xml/Check XML File")]
    11.     private static void CheckXMLFile()
    12.     {
    13.         //Selection.activeObject
    14.        
    15.         string filepath = AssetDatabase.GetAssetPath(Selection.activeObject);
    16.         //hdrpath = Path.ChangeExtension(filepath,".xml");
    17.         //string extension  = Path.GetExtension(filepath);
    18.         Debug.Log("XMLUtility:" + filepath);
    19.         Debug.Log("XMLUtility:" + Selection.activeObject.GetType());
    20.        
    21.         //TextAsset ta = Selection.activeObject as TextAsset;
    22.         TextAsset ta = AssetDatabase.LoadMainAssetAtPath(filepath) as TextAsset;
    23.        
    24.        
    25.         if (ta != default(TextAsset))
    26.         {
    27.             Test(ta.text);
    28.         }
    29.         else
    30.         {
    31.             Debug.LogWarning("XMLUtility: No TextAsset selected");
    32.         }
    33.        
    34.     }
    35.    
    36.     static void Test(string xml) {
    37.         using (XmlReader xr = XmlReader.Create(
    38.             new StringReader(xml))) {
    39.             try {
    40.                 while (xr.Read()) { }
    41.                 Debug.Log("XMLUtility: XML Is Valid");
    42.             } catch (Exception ex) {
    43.                 Debug.LogError("XMLUtility: XML is not Valid: " + ex.Message);
    44.             }
    45.         }
    46.     }
    47.    
    48.     // Note that we pass the same path, and also pass "true" to the second argument.
    49.     [MenuItem("Assets/xml/Check XML File", true)]
    50.     private static bool CheckXMLFileValidation()
    51.     {
    52.         // This returns true when the selected object is a Texture2D (the menu item will be disabled otherwise).
    53.         if ( Selection.activeObject.GetType() == typeof(DefaultAsset))
    54.         {
    55.             string filepath = AssetDatabase.GetAssetPath(Selection.activeObject);
    56.             string extension  = Path.GetExtension(filepath);
    57.             //Debug.Log("XMLUtility: CheckXMLFileValidation" + extension);
    58.             if (extension == ".xml") {
    59.                 return true;
    60.             }  
    61.         }
    62.         return false;
    63.     }
    64.  
    65. }
     
  2. malosal

    malosal

    Joined:
    Jun 22, 2013
    Posts:
    151
    This is a real problem that nobody has really resolved, as far as I know. I've searched for days on the correct way to get the text of an InputField, and even saw others complaining of this, but I guess this is not a priority at the moment.
     
  3. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    What exactly isn't working for you? i just tried your code, and except for the wrong validating method, it works fine. Selection.activeObject does hold a reference to the selected TextAsset and you can use it. Also, activeObject already holds the actual loaded object, there's no need to load that again:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System;
    4. using System.Collections;
    5. using System.Xml;
    6. using System.IO;
    7.  
    8. public class XMLUtility : Editor {
    9.  
    10.     [MenuItem("Assets/xml/Check XML File")]
    11.     private static void CheckXMLFile()
    12.     {
    13.         string filepath = AssetDatabase.GetAssetPath(Selection.activeObject);
    14.  
    15.         Debug.Log("XMLUtility:" + filepath);
    16.         Debug.Log("XMLUtility:" + Selection.activeObject.GetType());
    17.  
    18.         TextAsset ta = Selection.activeObject as TextAsset;
    19.  
    20.         if (ta != null)
    21.         {
    22.             Test(ta.text);
    23.         }
    24.         else
    25.         {
    26.             Debug.LogWarning("XMLUtility: No TextAsset selected");
    27.         }
    28.     }
    29.  
    30.     static void Test(string xml) {
    31.         using (XmlReader xr = XmlReader.Create(
    32.             new StringReader(xml))) {
    33.             try {
    34.                 while (xr.Read()) { }
    35.                 Debug.Log("XMLUtility: XML Is Valid");
    36.             } catch (Exception ex) {
    37.                 Debug.LogError("XMLUtility: XML is not Valid: " + ex.Message);
    38.             }
    39.         }
    40.     }
    41.  
    42.     // Note that we pass the same path, and also pass "true" to the second argument.
    43.     [MenuItem("Assets/xml/Check XML File", true)]
    44.     private static bool CheckXMLFileValidation()
    45.     {
    46.             string filepath = AssetDatabase.GetAssetPath(Selection.activeObject);
    47.             string extension  = Path.GetExtension(filepath);
    48.            
    49.         if (extension == ".xml") {
    50.                 return true;
    51.         }
    52.  
    53.         return false;
    54.     }
    55. }