/// <summary> /// DO NOT USE: Working on, where every texture is grabbed from the object /// </summary> public static void ExportTextureFromAMesh(MeshRenderer meshRenderer, string materialFolder, string fullTextureFolder) { // 1. Need to copy the texture to the new path // Go through all materials in the mesh renderer and export them as some materials int length = meshRenderer.sharedMaterials.Length; for (int materialIndex = 0; materialIndex < length; materialIndex++) { Material currentMaterial = meshRenderer.sharedMaterials[materialIndex]; Shader shader = currentMaterial.shader; int propertyCount = ShaderUtil.GetPropertyCount(shader); // Copies all the textures to the file for (int i = 0; i < propertyCount; i++) { ShaderUtil.ShaderPropertyType propType = ShaderUtil.GetPropertyType(shader, i); if (propType == ShaderUtil.ShaderPropertyType.TexEnv) { Texture texture = currentMaterial.GetTexture(ShaderUtil.GetPropertyName(shader, i)); if (texture != null) { string texturePath = UtiltiesHiFi.GetAssetPathFolder() + AssetDatabase.GetAssetOrScenePath(texture); string newTextureName = UtiltiesHiFi.GetFileName(texturePath); string newTexturePathAndName = fullTextureFolder + newTextureName; File.Copy(texturePath, newTexturePathAndName); } } } } }
private static bool SerializeOneTexture( GameObject gameObj, string newPath, Material material, string materialName, int materialId, bool copyTextures, string unityExtension, string textureType, out string objects, out string connections) { string texturesFolderName = @"/Textures/"; string texturesFolderNameNoFrontSlash = @"Textures/"; texturesFolderName = ""; texturesFolderNameNoFrontSlash = ""; StringBuilder objectsSb = new StringBuilder(); StringBuilder connectionsSb = new StringBuilder(); Texture texture = material.GetTexture(unityExtension); if (texture == null) { objects = ""; connections = ""; return(false); } string originalAssetPath = AssetDatabase.GetAssetPath(texture); string fullDataFolderPath = Application.dataPath; string textureFilePathFullName = originalAssetPath; string textureName = Path.GetFileNameWithoutExtension(originalAssetPath); string textureExtension = Path.GetExtension(originalAssetPath); // TODO - We have to change this to relative to the file being exported // if(copyTextures) { textureFilePathFullName = UtiltiesHiFi.GetFolderFromPath(newPath) + texturesFolderNameNoFrontSlash + textureName + unityExtension + textureExtension; } // copy the textures to the new folder went want to copy it to string fullOriginalPath = UtiltiesHiFi.GetAssetPathFolder() + originalAssetPath; if (Directory.Exists(UtiltiesHiFi.GetFolderFromPath(newPath) + texturesFolderName) == false) { Directory.CreateDirectory(UtiltiesHiFi.GetFolderFromPath(newPath) + texturesFolderName); } // TODO - prevent overwrites of different files File.Copy(fullOriginalPath, textureFilePathFullName, true); long textureReference = HighFidelityFBXExporter.GetRandomFBXId(); // TODO - test out different reference names to get one that doesn't load a _MainTex when importing. objectsSb.AppendLine("\tTexture: " + textureReference + ", \"Texture::" + materialName + unityExtension + "\", \"\" {"); objectsSb.AppendLine("\t\tType: \"TextureVideoClip\""); objectsSb.AppendLine("\t\tVersion: 202"); objectsSb.AppendLine("\t\tTextureName: \"Texture::" + materialName + unityExtension + "\""); objectsSb.AppendLine("\t\tProperties70: {"); objectsSb.AppendLine("\t\t\tP: \"CurrentTextureBlendMode\", \"enum\", \"\", \"\",0"); objectsSb.AppendLine("\t\t\tP: \"UVSet\", \"KString\", \"\", \"\", \"map1\""); objectsSb.AppendLine("\t\t\tP: \"UseMaterial\", \"bool\", \"\", \"\",1"); objectsSb.AppendLine("\t\t}"); objectsSb.AppendLine("\t\tMedia: \"Video::" + materialName + unityExtension + "\""); // Sets the absolute path for the copied texture objectsSb.Append("\t\tFileName: \""); objectsSb.Append(textureFilePathFullName); objectsSb.AppendLine("\""); objectsSb.AppendLine("\t\tRelativeFilename: \"" + texturesFolderNameNoFrontSlash + textureName + unityExtension + textureExtension + "\""); objectsSb.AppendLine("\t\tModelUVTranslation: 0,0"); // TODO: Figure out how to get the UV translation into here objectsSb.AppendLine("\t\tModelUVScaling: 1,1"); // TODO: Figure out how to get the UV scaling into here objectsSb.AppendLine("\t\tTexture_Alpha_Source: \"None\""); // TODO: Add alpha source here if the file is a cutout. objectsSb.AppendLine("\t\tCropping: 0,0,0,0"); objectsSb.AppendLine("\t}"); connectionsSb.AppendLine("\t;Texture::" + textureName + ", Material::" + materialName + "\""); connectionsSb.AppendLine("\tC: \"OP\"," + textureReference + "," + materialId + ", \"" + textureType + "\""); connectionsSb.AppendLine(); objects = objectsSb.ToString(); connections = connectionsSb.ToString(); return(true); }