public static Material GetMaterialFromGUIDEditor(string guid, string basePath) { AssetSchematic schematic = new AssetSchematic(); if (schematicLookup.TryGetValue(guid, out schematic)) { try { if (schematic != null && schematic.origin_and_description != null && schematic.origin_and_description.mcs_id != null && schematic.origin_and_description.mcs_id.Equals(guid)) { string matPath = MCS_Utilities.Paths.ConvertRelativeToAbsolute(basePath, schematic.stream_and_path.generated_path); //fix for maya matPath = matPath.Replace(":", "_"); Material m = AssetDatabase.LoadAssetAtPath <Material>(matPath); return(m); } } catch (Exception e) { UnityEngine.Debug.LogException(e); } } else { string[] paths = Directory.GetFiles(basePath, "*.mon", SearchOption.AllDirectories); foreach (string path in paths) { try { schematic = AssetSchematic.CreateFromJSON(File.ReadAllText(path)); if (schematic != null && schematic.origin_and_description != null && schematic.origin_and_description.mcs_id != null && schematic.origin_and_description.mcs_id.Equals(guid)) { string monDir = MCS_Utilities.Paths.ConvertFileToDir(path); string matPath = MCS_Utilities.Paths.ConvertRelativeToAbsolute(monDir, schematic.stream_and_path.generated_path); Material m = AssetDatabase.LoadAssetAtPath <Material>(matPath); return(m); } } catch (Exception e) { UnityEngine.Debug.LogException(e); } } } UnityEngine.Debug.LogWarning("Failed to locate material for GUID: " + guid); return(null); }
/// <summary> /// Deserializes the mon file. Path needs to be within the assets folder /// </summary> /// <returns>The mon file.</returns> /// <param name="path">Path.</param> public AssetSchematic[] DeserializeMonFile(string path) { string revisedPath = path.Replace("Assets", ""); string result = ""; try { string streamPath = Application.dataPath + revisedPath; using (FileStream fs = new FileStream(streamPath, FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { string line; while ((line = sr.ReadLine()) != null) { result += line; } } } } catch (Exception e) { Debug.LogError("Caught exception while processing mon file: " + path); Debug.LogException(e); } AssetSchematic[] schematics = null; if (result != null) { //try a multi one first try { schematics = AssetSchematic.CreateArrayFromJSON(result); if (schematics != null && schematics.Length <= 0) { schematics = null; } } catch (Exception e) { Debug.Log("Exception caught while processing schematic."); Debug.LogException(e); } if (schematics == null) { //try a single one try { AssetSchematic schematic = AssetSchematic.CreateFromJSON(result); schematics = new AssetSchematic[1]; schematics[0] = schematic; } catch (Exception e) { UnityEngine.Debug.LogError("Unable to parse mon file: " + path); UnityEngine.Debug.LogException(e); } } } return(schematics); }