// We use the user data to save a string that represent the referenced lit material // so we can keep reference during serialization public static void InitializeMaterialLayers(Material material, ref Material[] layers) { AssetImporter materialImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(material.GetInstanceID())); if (materialImporter.userData != string.Empty) { SerializeableGUIDs layersGUID = JsonUtility.FromJson <SerializeableGUIDs>(materialImporter.userData); if (layersGUID.GUIDArray.Length > 0) { layers = new Material[layersGUID.GUIDArray.Length]; for (int i = 0; i < layersGUID.GUIDArray.Length; ++i) { layers[i] = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(layersGUID.GUIDArray[i]), typeof(Material)) as Material; } } } }
public static void InitializeMaterialLayers(AssetImporter materialImporter, ref Material[] layers, ref bool[] withUV) { if (materialImporter.userData != string.Empty) { SerializeableGUIDs layersGUID = JsonUtility.FromJson <SerializeableGUIDs>(materialImporter.userData); if (layersGUID.GUIDArray.Length > 0) { layers = new Material[layersGUID.GUIDArray.Length]; for (int i = 0; i < layersGUID.GUIDArray.Length; ++i) { layers[i] = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(layersGUID.GUIDArray[i]), typeof(Material)) as Material; } } if (layersGUID.withUV != null && layersGUID.withUV.Length > 0) { withUV = new bool[layersGUID.withUV.Length]; for (int i = 0; i < layersGUID.withUV.Length; ++i) { withUV[i] = layersGUID.withUV[i]; } } } else { if (layers != null) { for (int i = 0; i < layers.Length; ++i) { layers[i] = null; } } if (withUV != null) { for (int i = 0; i < withUV.Length; ++i) { withUV[i] = true; } } } }