30using System.Collections.Generic;
51 public static void GetAllMaterialsToString(GameObject gameObj,
string newPath,
bool copyMaterials,
bool copyTextures, out Material[] materials, out
string matObjects, out
string connections)
53 StringBuilder tempObjectSb =
new StringBuilder();
54 StringBuilder tempConnectionsSb =
new StringBuilder();
59 Renderer[] meshRenders = gameObj.GetComponentsInChildren<Renderer>();
61 List<Material> uniqueMaterials =
new List<Material>();
64 for(
int i = 0; i < meshRenders.Length; i++)
66 for(
int n = 0; n < meshRenders[i].sharedMaterials.Length; n++)
68 Material mat = meshRenders[i].sharedMaterials[n];
70 if(uniqueMaterials.Contains(mat) ==
false && mat !=
null)
72 uniqueMaterials.Add(mat);
77 for (
int i = 0; i < uniqueMaterials.Count; i++)
79 Material mat = uniqueMaterials[i];
82 string materialName = mat.name;
84 materialName = gameObj.name +
"_" + mat.name;
86 int referenceId = Mathf.Abs(mat.GetInstanceID());
90 if (mat.HasProperty(
"_Color"))
96 color1 = mat.GetColor(
"_Diffusecolor");
99 tempObjectSb.AppendLine();
100 tempObjectSb.AppendLine(
"\tMaterial: " + referenceId +
", \"Material::" + materialName +
"\", \"\" {");
101 tempObjectSb.AppendLine(
"\t\tVersion: 102");
102 tempObjectSb.AppendLine(
"\t\tShadingModel: \"phong\"");
103 tempObjectSb.AppendLine(
"\t\tMultiLayer: 0");
104 tempObjectSb.AppendLine(
"\t\tProperties70: {");
105 tempObjectSb.AppendFormat(
"\t\t\tP: \"Diffuse\", \"Vector3D\", \"Vector\", \"\",{0},{1},{2}",
FE.FBXFormat(color1.r),
FE.FBXFormat(color1.g),
FE.FBXFormat(color1.b));
106 tempObjectSb.AppendLine();
107 tempObjectSb.AppendFormat(
"\t\t\tP: \"DiffuseColor\", \"Color\", \"\", \"A\",{0},{1},{2}",
FE.FBXFormat(color1.r),
FE.FBXFormat(color1.g),
FE.FBXFormat(color1.b));
108 tempObjectSb.AppendLine();
121 if(mat.HasProperty(
"_SpecColor"))
123 Color color = mat.GetColor(
"_SpecColor");
124 tempObjectSb.AppendFormat(
"\t\t\tP: \"Specular\", \"Vector3D\", \"Vector\", \"\",{0},{1},{2}",
FE.FBXFormat(color.r),
FE.FBXFormat(color.g),
FE.FBXFormat(color.r));
125 tempObjectSb.AppendLine();
126 tempObjectSb.AppendFormat(
"\t\t\tP: \"SpecularColor\", \"ColorRGB\", \"Color\", \" \",{0},{1},{2}",
FE.FBXFormat(color.r),
FE.FBXFormat(color.g),
FE.FBXFormat(color.b));
127 tempObjectSb.AppendLine();
130 if(mat.HasProperty(
"_Mode"))
134 switch((
int)mat.GetFloat(
"_Mode"))
145 color = mat.GetColor(
"_Color");
147 tempObjectSb.AppendFormat(
"\t\t\tP: \"TransparentColor\", \"Color\", \"\", \"A\",{0},{1},{2}",
FE.FBXFormat(color.r),
FE.FBXFormat(color.g),
FE.FBXFormat(color.b));
148 tempObjectSb.AppendLine();
149 tempObjectSb.AppendFormat(
"\t\t\tP: \"Opacity\", \"double\", \"Number\", \"\",{0}",
FE.FBXFormat(color.a));
150 tempObjectSb.AppendLine();
154 color = mat.GetColor(
"_Color");
156 tempObjectSb.AppendFormat(
"\t\t\tP: \"TransparentColor\", \"Color\", \"\", \"A\",{0},{1},{2}",
FE.FBXFormat(color.r),
FE.FBXFormat(color.g),
FE.FBXFormat(color.b));
157 tempObjectSb.AppendLine();
158 tempObjectSb.AppendFormat(
"\t\t\tP: \"Opacity\", \"double\", \"Number\", \"\",{0}",
FE.FBXFormat(color.a));
159 tempObjectSb.AppendLine();
165 if(mat.HasProperty(
"_EmissionColor"))
167 Color color = mat.GetColor(
"_EmissionColor");
169 tempObjectSb.AppendFormat(
"\t\t\tP: \"Emissive\", \"Vector3D\", \"Vector\", \"\",{0},{1},{2}",
FE.FBXFormat(color.r),
FE.FBXFormat(color.g),
FE.FBXFormat(color.b));
170 tempObjectSb.AppendLine();
172 float averageColor = (color.r + color.g + color.b) / 3f;
174 tempObjectSb.AppendFormat(
"\t\t\tP: \"EmissiveFactor\", \"Number\", \"\", \"A\",{0}",
FE.FBXFormat(averageColor));
175 tempObjectSb.AppendLine();
185 tempObjectSb.AppendLine(
"\t\t}");
186 tempObjectSb.AppendLine(
"\t}");
188 string textureObjects;
189 string textureConnections;
191 SerializedTextures(gameObj, newPath, mat, materialName, copyTextures, out textureObjects, out textureConnections);
193 tempObjectSb.Append(textureObjects);
194 tempConnectionsSb.Append(textureConnections);
197 materials = uniqueMaterials.ToArray<Material>();
199 matObjects = tempObjectSb.ToString();
200 connections = tempConnectionsSb.ToString();
211 private static void SerializedTextures(GameObject gameObj,
string newPath, Material material,
string materialName,
bool copyTextures, out
string objects, out
string connections)
219 StringBuilder objectsSb =
new StringBuilder();
220 StringBuilder connectionsSb =
new StringBuilder();
222 int materialId = Mathf.Abs(material.GetInstanceID());
224 Texture mainTexture = material.GetTexture(
"_MainTex");
226 string newObjects =
null;
227 string newConnections =
null;
230 if(mainTexture !=
null)
232 SerializeOneTexture(gameObj, newPath, material, materialName, materialId, copyTextures,
"_MainTex",
"DiffuseColor", out newObjects, out newConnections);
233 objectsSb.AppendLine(newObjects);
234 connectionsSb.AppendLine(newConnections);
237 if(SerializeOneTexture(gameObj, newPath, material, materialName, materialId, copyTextures,
"_BumpMap",
"NormalMap", out newObjects, out newConnections))
239 objectsSb.AppendLine(newObjects);
240 connectionsSb.AppendLine(newConnections);
243 connections = connectionsSb.ToString();
244 objects = objectsSb.ToString();
247 private static bool SerializeOneTexture(GameObject gameObj,
253 string unityExtension,
256 out
string connections)
258 StringBuilder objectsSb =
new StringBuilder();
259 StringBuilder connectionsSb =
new StringBuilder();
261 Texture texture = material.GetTexture(unityExtension);
269 string originalAssetPath =
"";
272 originalAssetPath = AssetDatabase.GetAssetPath(texture);
274 Debug.LogError(
"Unity FBX Exporter can not serialize textures at runtime (yet). Look in FBXUnityMaterialGetter around line 250ish. Fix it and contribute to the project!");
279 string fullDataFolderPath = Application.dataPath;
280 string textureFilePathFullName = originalAssetPath;
281 string textureName = Path.GetFileNameWithoutExtension(originalAssetPath);
282 string textureExtension = Path.GetExtension(originalAssetPath);
287 int indexOfAssetsFolder = fullDataFolderPath.LastIndexOf(
"/Assets");
288 fullDataFolderPath = fullDataFolderPath.Remove(indexOfAssetsFolder, fullDataFolderPath.Length - indexOfAssetsFolder);
290 string newPathFolder = newPath.Remove(newPath.LastIndexOf(
'/') + 1, newPath.Length - newPath.LastIndexOf(
'/') - 1);
291 textureName = gameObj.name +
"_" + material.name + unityExtension;
293 textureFilePathFullName = fullDataFolderPath +
"/" + newPathFolder + textureName + textureExtension;
296 long textureReference = FBXExporter.GetRandomFBXId();
300 objectsSb.AppendLine(
"\tTexture: " + textureReference +
", \"Texture::" + materialName +
"\", \"\" {");
301 objectsSb.AppendLine(
"\t\tType: \"TextureVideoClip\"");
302 objectsSb.AppendLine(
"\t\tVersion: 202");
303 objectsSb.AppendLine(
"\t\tTextureName: \"Texture::" + materialName +
"\"");
304 objectsSb.AppendLine(
"\t\tProperties70: {");
305 objectsSb.AppendLine(
"\t\t\tP: \"CurrentTextureBlendMode\", \"enum\", \"\", \"\",0");
306 objectsSb.AppendLine(
"\t\t\tP: \"UVSet\", \"KString\", \"\", \"\", \"map1\"");
307 objectsSb.AppendLine(
"\t\t\tP: \"UseMaterial\", \"bool\", \"\", \"\",1");
308 objectsSb.AppendLine(
"\t\t}");
309 objectsSb.AppendLine(
"\t\tMedia: \"Video::" + materialName +
"\"");
312 objectsSb.Append(
"\t\tFileName: \"");
313 objectsSb.Append(textureFilePathFullName);
314 objectsSb.AppendLine(
"\"");
319 objectsSb.AppendLine(
"\t\tRelativeFilename: \"/Textures/" + textureName + textureExtension +
"\"");
321 objectsSb.AppendLine(
"\t\tModelUVTranslation: 0,0");
322 objectsSb.AppendLine(
"\t\tModelUVScaling: 1,1");
323 objectsSb.AppendLine(
"\t\tTexture_Alpha_Source: \"None\"");
324 objectsSb.AppendLine(
"\t\tCropping: 0,0,0,0");
325 objectsSb.AppendLine(
"\t}");
327 connectionsSb.AppendLine(
"\t;Texture::" + textureName +
", Material::" + materialName +
"\"");
328 connectionsSb.AppendLine(
"\tC: \"OP\"," + textureReference +
"," + materialId +
", \"" + textureType +
"\"");
330 connectionsSb.AppendLine();
332 objects = objectsSb.ToString();
333 connections = connectionsSb.ToString();
UnityFBXExporter.FBXExporter FE
static void GetAllMaterialsToString(GameObject gameObj, string newPath, bool copyMaterials, bool copyTextures, out Material[] materials, out string matObjects, out string connections)
Finds all materials in a gameobject and writes them to a string that can be read by the FBX writer