30using System.Collections.Generic;
41 public static bool ExportGameObjToFBX(GameObject gameObj,
string newPath,
bool copyMaterials =
false,
bool copyTextures =
false)
44 if (Path.GetExtension(newPath).ToLower() !=
".fbx")
46 Debug.LogError(
"The end of the path wasn't \".fbx\"");
53 string buildMesh =
MeshToString(gameObj, newPath, copyMaterials, copyTextures);
55 if(System.IO.File.Exists(newPath))
56 System.IO.File.Delete(newPath);
58 System.IO.File.WriteAllText(newPath, buildMesh);
60#if UNITY_EDITOR && false
64 AssetDatabase.Refresh();
65 string stringLocalPath = newPath.Remove(0, newPath.LastIndexOf(
"/Assets") + 1);
66 ModelImporter modelImporter = ModelImporter.GetAtPath(stringLocalPath) as ModelImporter;
67 if(modelImporter !=
null)
69 ModelImporterMaterialName modelImportOld = modelImporter.materialName;
70 modelImporter.materialName = ModelImporterMaterialName.BasedOnMaterialName;
72 modelImporter.normalImportMode = ModelImporterTangentSpaceMode.Import;
74 modelImporter.importNormals = ModelImporterNormals.Import;
76 if (copyMaterials ==
false)
77 modelImporter.materialSearch = ModelImporterMaterialSearch.Everywhere;
79 AssetDatabase.ImportAsset(stringLocalPath, ImportAssetOptions.ForceUpdate);
83 Debug.Log(
"Model Importer is null and can't import");
86 AssetDatabase.Refresh();
97 get {
return "FBX Unity Export version 1.1.1 (Originally created for the Unity Asset, Building Crafter)"; }
102 return System.BitConverter.ToInt64(System.Guid.NewGuid().ToByteArray(), 0);
105 public static string MeshToString (GameObject gameObj,
string newPath,
bool copyMaterials =
false,
bool copyTextures =
false)
107 StringBuilder sb =
new StringBuilder();
109 StringBuilder objectProps =
new StringBuilder();
110 objectProps.AppendLine(
"; Object properties");
111 objectProps.AppendLine(
";------------------------------------------------------------------");
112 objectProps.AppendLine(
"");
113 objectProps.AppendLine(
"Objects: {");
115 StringBuilder objectConnections =
new StringBuilder();
116 objectConnections.AppendLine(
"; Object connections");
117 objectConnections.AppendLine(
";------------------------------------------------------------------");
118 objectConnections.AppendLine(
"");
119 objectConnections.AppendLine(
"Connections: {");
120 objectConnections.AppendLine(
"\t");
122 Material[] materials =
new Material[0];
125 string materialsObjectSerialized =
"";
126 string materialConnectionsSerialized =
"";
135 objectProps.Append(materialsObjectSerialized);
136 objectConnections.Append(materialConnectionsSerialized);
139 objectProps.AppendLine(
"}");
140 objectConnections.AppendLine(
"}");
145 sb.AppendLine(
"; FBX 7.3.0 project file");
146 sb.AppendLine(
"; Copyright (C) 1997-2010 Autodesk Inc. and/or its licensors.");
147 sb.AppendLine(
"; All rights reserved.");
148 sb.AppendLine(
"; ----------------------------------------------------");
152 sb.AppendLine(
"FBXHeaderExtension: {");
153 sb.AppendLine(
"\tFBXHeaderVersion: 1003");
154 sb.AppendLine(
"\tFBXVersion: 7300");
157 System.DateTime currentDate = System.DateTime.Now;
158 sb.AppendLine(
"\tCreationTimeStamp: {");
159 sb.AppendLine(
"\t\tVersion: 1000");
160 sb.AppendLine(
"\t\tYear: " + currentDate.Year);
161 sb.AppendLine(
"\t\tMonth: " + currentDate.Month);
162 sb.AppendLine(
"\t\tDay: " + currentDate.Day);
163 sb.AppendLine(
"\t\tHour: " + currentDate.Hour);
164 sb.AppendLine(
"\t\tMinute: " + currentDate.Minute);
165 sb.AppendLine(
"\t\tSecond: " + currentDate.Second);
166 sb.AppendLine(
"\t\tMillisecond: " + currentDate.Millisecond);
167 sb.AppendLine(
"\t}");
171 sb.AppendLine(
"\tSceneInfo: \"SceneInfo::GlobalInfo\", \"UserData\" {");
172 sb.AppendLine(
"\t\tType: \"UserData\"");
173 sb.AppendLine(
"\t\tVersion: 100");
174 sb.AppendLine(
"\t\tMetaData: {");
175 sb.AppendLine(
"\t\t\tVersion: 100");
176 sb.AppendLine(
"\t\t\tTitle: \"\"");
177 sb.AppendLine(
"\t\t\tSubject: \"\"");
178 sb.AppendLine(
"\t\t\tAuthor: \"\"");
179 sb.AppendLine(
"\t\t\tKeywords: \"\"");
180 sb.AppendLine(
"\t\t\tRevision: \"\"");
181 sb.AppendLine(
"\t\t\tComment: \"\"");
182 sb.AppendLine(
"\t\t}");
183 sb.AppendLine(
"\t\tProperties70: {");
186 string documentInfoPaths = Application.dataPath + newPath +
".fbx";
187 sb.AppendLine(
"\t\t\tP: \"DocumentUrl\", \"KString\", \"Url\", \"\", \"" + documentInfoPaths +
"\"");
188 sb.AppendLine(
"\t\t\tP: \"SrcDocumentUrl\", \"KString\", \"Url\", \"\", \"" + documentInfoPaths +
"\"");
189 sb.AppendLine(
"\t\t\tP: \"Original\", \"Compound\", \"\", \"\"");
190 sb.AppendLine(
"\t\t\tP: \"Original|ApplicationVendor\", \"KString\", \"\", \"\", \"\"");
191 sb.AppendLine(
"\t\t\tP: \"Original|ApplicationName\", \"KString\", \"\", \"\", \"\"");
192 sb.AppendLine(
"\t\t\tP: \"Original|ApplicationVersion\", \"KString\", \"\", \"\", \"\"");
193 sb.AppendLine(
"\t\t\tP: \"Original|DateTime_GMT\", \"DateTime\", \"\", \"\", \"\"");
194 sb.AppendLine(
"\t\t\tP: \"Original|FileName\", \"KString\", \"\", \"\", \"\"");
195 sb.AppendLine(
"\t\t\tP: \"LastSaved\", \"Compound\", \"\", \"\"");
196 sb.AppendLine(
"\t\t\tP: \"LastSaved|ApplicationVendor\", \"KString\", \"\", \"\", \"\"");
197 sb.AppendLine(
"\t\t\tP: \"LastSaved|ApplicationName\", \"KString\", \"\", \"\", \"\"");
198 sb.AppendLine(
"\t\t\tP: \"LastSaved|ApplicationVersion\", \"KString\", \"\", \"\", \"\"");
199 sb.AppendLine(
"\t\t\tP: \"LastSaved|DateTime_GMT\", \"DateTime\", \"\", \"\", \"\"");
200 sb.AppendLine(
"\t\t}");
201 sb.AppendLine(
"\t}");
205 sb.AppendLine(
"GlobalSettings: {");
206 sb.AppendLine(
"\tVersion: 1000");
207 sb.AppendLine(
"\tProperties70: {");
208 sb.AppendLine(
"\t\tP: \"UpAxis\", \"int\", \"Integer\", \"\",1");
209 sb.AppendLine(
"\t\tP: \"UpAxisSign\", \"int\", \"Integer\", \"\",1");
210 sb.AppendLine(
"\t\tP: \"FrontAxis\", \"int\", \"Integer\", \"\",2");
211 sb.AppendLine(
"\t\tP: \"FrontAxisSign\", \"int\", \"Integer\", \"\",1");
212 sb.AppendLine(
"\t\tP: \"CoordAxis\", \"int\", \"Integer\", \"\",0");
213 sb.AppendLine(
"\t\tP: \"CoordAxisSign\", \"int\", \"Integer\", \"\",1");
214 sb.AppendLine(
"\t\tP: \"OriginalUpAxis\", \"int\", \"Integer\", \"\",-1");
215 sb.AppendLine(
"\t\tP: \"OriginalUpAxisSign\", \"int\", \"Integer\", \"\",1");
216 sb.AppendLine(
"\t\tP: \"UnitScaleFactor\", \"double\", \"Number\", \"\",100");
217 sb.AppendLine(
"\t\tP: \"OriginalUnitScaleFactor\", \"double\", \"Number\", \"\",100");
218 sb.AppendLine(
"\t\tP: \"AmbientColor\", \"ColorRGB\", \"Color\", \"\",0,0,0");
219 sb.AppendLine(
"\t\tP: \"DefaultCamera\", \"KString\", \"\", \"\", \"Producer Perspective\"");
220 sb.AppendLine(
"\t\tP: \"TimeMode\", \"enum\", \"\", \"\",11");
221 sb.AppendLine(
"\t\tP: \"TimeSpanStart\", \"KTime\", \"Time\", \"\",0");
222 sb.AppendLine(
"\t\tP: \"TimeSpanStop\", \"KTime\", \"Time\", \"\",479181389250");
223 sb.AppendLine(
"\t\tP: \"CustomFrameRate\", \"double\", \"Number\", \"\",-1");
224 sb.AppendLine(
"\t}");
228 sb.AppendLine(
"; Object definitions");
229 sb.AppendLine(
";------------------------------------------------------------------");
231 sb.AppendLine(
"Definitions: {");
232 sb.AppendLine(
"\tVersion: 100");
233 sb.AppendLine(
"\tCount: 4");
235 sb.AppendLine(
"\tObjectType: \"GlobalSettings\" {");
236 sb.AppendLine(
"\t\tCount: 1");
237 sb.AppendLine(
"\t}");
240 sb.AppendLine(
"\tObjectType: \"Model\" {");
241 sb.AppendLine(
"\t\tCount: 1");
242 sb.AppendLine(
"\t\tPropertyTemplate: \"FbxNode\" {");
243 sb.AppendLine(
"\t\t\tProperties70: {");
244 sb.AppendLine(
"\t\t\t\tP: \"QuaternionInterpolate\", \"enum\", \"\", \"\",0");
245 sb.AppendLine(
"\t\t\t\tP: \"RotationOffset\", \"Vector3D\", \"Vector\", \"\",0,0,0");
246 sb.AppendLine(
"\t\t\t\tP: \"RotationPivot\", \"Vector3D\", \"Vector\", \"\",0,0,0");
247 sb.AppendLine(
"\t\t\t\tP: \"ScalingOffset\", \"Vector3D\", \"Vector\", \"\",0,0,0");
248 sb.AppendLine(
"\t\t\t\tP: \"ScalingPivot\", \"Vector3D\", \"Vector\", \"\",0,0,0");
249 sb.AppendLine(
"\t\t\t\tP: \"TranslationActive\", \"bool\", \"\", \"\",0");
250 sb.AppendLine(
"\t\t\t\tP: \"TranslationMin\", \"Vector3D\", \"Vector\", \"\",0,0,0");
251 sb.AppendLine(
"\t\t\t\tP: \"TranslationMax\", \"Vector3D\", \"Vector\", \"\",0,0,0");
252 sb.AppendLine(
"\t\t\t\tP: \"TranslationMinX\", \"bool\", \"\", \"\",0");
253 sb.AppendLine(
"\t\t\t\tP: \"TranslationMinY\", \"bool\", \"\", \"\",0");
254 sb.AppendLine(
"\t\t\t\tP: \"TranslationMinZ\", \"bool\", \"\", \"\",0");
255 sb.AppendLine(
"\t\t\t\tP: \"TranslationMaxX\", \"bool\", \"\", \"\",0");
256 sb.AppendLine(
"\t\t\t\tP: \"TranslationMaxY\", \"bool\", \"\", \"\",0");
257 sb.AppendLine(
"\t\t\t\tP: \"TranslationMaxZ\", \"bool\", \"\", \"\",0");
258 sb.AppendLine(
"\t\t\t\tP: \"RotationOrder\", \"enum\", \"\", \"\",0");
259 sb.AppendLine(
"\t\t\t\tP: \"RotationSpaceForLimitOnly\", \"bool\", \"\", \"\",0");
260 sb.AppendLine(
"\t\t\t\tP: \"RotationStiffnessX\", \"double\", \"Number\", \"\",0");
261 sb.AppendLine(
"\t\t\t\tP: \"RotationStiffnessY\", \"double\", \"Number\", \"\",0");
262 sb.AppendLine(
"\t\t\t\tP: \"RotationStiffnessZ\", \"double\", \"Number\", \"\",0");
263 sb.AppendLine(
"\t\t\t\tP: \"AxisLen\", \"double\", \"Number\", \"\",10");
264 sb.AppendLine(
"\t\t\t\tP: \"PreRotation\", \"Vector3D\", \"Vector\", \"\",0,0,0");
265 sb.AppendLine(
"\t\t\t\tP: \"PostRotation\", \"Vector3D\", \"Vector\", \"\",0,0,0");
266 sb.AppendLine(
"\t\t\t\tP: \"RotationActive\", \"bool\", \"\", \"\",0");
267 sb.AppendLine(
"\t\t\t\tP: \"RotationMin\", \"Vector3D\", \"Vector\", \"\",0,0,0");
268 sb.AppendLine(
"\t\t\t\tP: \"RotationMax\", \"Vector3D\", \"Vector\", \"\",0,0,0");
269 sb.AppendLine(
"\t\t\t\tP: \"RotationMinX\", \"bool\", \"\", \"\",0");
270 sb.AppendLine(
"\t\t\t\tP: \"RotationMinY\", \"bool\", \"\", \"\",0");
271 sb.AppendLine(
"\t\t\t\tP: \"RotationMinZ\", \"bool\", \"\", \"\",0");
272 sb.AppendLine(
"\t\t\t\tP: \"RotationMaxX\", \"bool\", \"\", \"\",0");
273 sb.AppendLine(
"\t\t\t\tP: \"RotationMaxY\", \"bool\", \"\", \"\",0");
274 sb.AppendLine(
"\t\t\t\tP: \"RotationMaxZ\", \"bool\", \"\", \"\",0");
275 sb.AppendLine(
"\t\t\t\tP: \"InheritType\", \"enum\", \"\", \"\",0");
276 sb.AppendLine(
"\t\t\t\tP: \"ScalingActive\", \"bool\", \"\", \"\",0");
277 sb.AppendLine(
"\t\t\t\tP: \"ScalingMin\", \"Vector3D\", \"Vector\", \"\",0,0,0");
278 sb.AppendLine(
"\t\t\t\tP: \"ScalingMax\", \"Vector3D\", \"Vector\", \"\",1,1,1");
279 sb.AppendLine(
"\t\t\t\tP: \"ScalingMinX\", \"bool\", \"\", \"\",0");
280 sb.AppendLine(
"\t\t\t\tP: \"ScalingMinY\", \"bool\", \"\", \"\",0");
281 sb.AppendLine(
"\t\t\t\tP: \"ScalingMinZ\", \"bool\", \"\", \"\",0");
282 sb.AppendLine(
"\t\t\t\tP: \"ScalingMaxX\", \"bool\", \"\", \"\",0");
283 sb.AppendLine(
"\t\t\t\tP: \"ScalingMaxY\", \"bool\", \"\", \"\",0");
284 sb.AppendLine(
"\t\t\t\tP: \"ScalingMaxZ\", \"bool\", \"\", \"\",0");
285 sb.AppendLine(
"\t\t\t\tP: \"GeometricTranslation\", \"Vector3D\", \"Vector\", \"\",0,0,0");
286 sb.AppendLine(
"\t\t\t\tP: \"GeometricRotation\", \"Vector3D\", \"Vector\", \"\",0,0,0");
287 sb.AppendLine(
"\t\t\t\tP: \"GeometricScaling\", \"Vector3D\", \"Vector\", \"\",1,1,1");
288 sb.AppendLine(
"\t\t\t\tP: \"MinDampRangeX\", \"double\", \"Number\", \"\",0");
289 sb.AppendLine(
"\t\t\t\tP: \"MinDampRangeY\", \"double\", \"Number\", \"\",0");
290 sb.AppendLine(
"\t\t\t\tP: \"MinDampRangeZ\", \"double\", \"Number\", \"\",0");
291 sb.AppendLine(
"\t\t\t\tP: \"MaxDampRangeX\", \"double\", \"Number\", \"\",0");
292 sb.AppendLine(
"\t\t\t\tP: \"MaxDampRangeY\", \"double\", \"Number\", \"\",0");
293 sb.AppendLine(
"\t\t\t\tP: \"MaxDampRangeZ\", \"double\", \"Number\", \"\",0");
294 sb.AppendLine(
"\t\t\t\tP: \"MinDampStrengthX\", \"double\", \"Number\", \"\",0");
295 sb.AppendLine(
"\t\t\t\tP: \"MinDampStrengthY\", \"double\", \"Number\", \"\",0");
296 sb.AppendLine(
"\t\t\t\tP: \"MinDampStrengthZ\", \"double\", \"Number\", \"\",0");
297 sb.AppendLine(
"\t\t\t\tP: \"MaxDampStrengthX\", \"double\", \"Number\", \"\",0");
298 sb.AppendLine(
"\t\t\t\tP: \"MaxDampStrengthY\", \"double\", \"Number\", \"\",0");
299 sb.AppendLine(
"\t\t\t\tP: \"MaxDampStrengthZ\", \"double\", \"Number\", \"\",0");
300 sb.AppendLine(
"\t\t\t\tP: \"PreferedAngleX\", \"double\", \"Number\", \"\",0");
301 sb.AppendLine(
"\t\t\t\tP: \"PreferedAngleY\", \"double\", \"Number\", \"\",0");
302 sb.AppendLine(
"\t\t\t\tP: \"PreferedAngleZ\", \"double\", \"Number\", \"\",0");
303 sb.AppendLine(
"\t\t\t\tP: \"LookAtProperty\", \"object\", \"\", \"\"");
304 sb.AppendLine(
"\t\t\t\tP: \"UpVectorProperty\", \"object\", \"\", \"\"");
305 sb.AppendLine(
"\t\t\t\tP: \"Show\", \"bool\", \"\", \"\",1");
306 sb.AppendLine(
"\t\t\t\tP: \"NegativePercentShapeSupport\", \"bool\", \"\", \"\",1");
307 sb.AppendLine(
"\t\t\t\tP: \"DefaultAttributeIndex\", \"int\", \"Integer\", \"\",-1");
308 sb.AppendLine(
"\t\t\t\tP: \"Freeze\", \"bool\", \"\", \"\",0");
309 sb.AppendLine(
"\t\t\t\tP: \"LODBox\", \"bool\", \"\", \"\",0");
310 sb.AppendLine(
"\t\t\t\tP: \"Lcl Translation\", \"Lcl Translation\", \"\", \"A\",0,0,0");
311 sb.AppendLine(
"\t\t\t\tP: \"Lcl Rotation\", \"Lcl Rotation\", \"\", \"A\",0,0,0");
312 sb.AppendLine(
"\t\t\t\tP: \"Lcl Scaling\", \"Lcl Scaling\", \"\", \"A\",1,1,1");
313 sb.AppendLine(
"\t\t\t\tP: \"Visibility\", \"Visibility\", \"\", \"A\",1");
314 sb.AppendLine(
"\t\t\t\tP: \"Visibility Inheritance\", \"Visibility Inheritance\", \"\", \"\",1");
315 sb.AppendLine(
"\t\t\t}");
316 sb.AppendLine(
"\t\t}");
317 sb.AppendLine(
"\t}");
320 sb.AppendLine(
"\tObjectType: \"Geometry\" {");
321 sb.AppendLine(
"\t\tCount: 1");
322 sb.AppendLine(
"\t\tPropertyTemplate: \"FbxMesh\" {");
323 sb.AppendLine(
"\t\t\tProperties70: {");
324 sb.AppendLine(
"\t\t\t\tP: \"Color\", \"ColorRGB\", \"Color\", \"\",0.8,0.8,0.8");
325 sb.AppendLine(
"\t\t\t\tP: \"BBoxMin\", \"Vector3D\", \"Vector\", \"\",0,0,0");
326 sb.AppendLine(
"\t\t\t\tP: \"BBoxMax\", \"Vector3D\", \"Vector\", \"\",0,0,0");
327 sb.AppendLine(
"\t\t\t\tP: \"Primary Visibility\", \"bool\", \"\", \"\",1");
328 sb.AppendLine(
"\t\t\t\tP: \"Casts Shadows\", \"bool\", \"\", \"\",1");
329 sb.AppendLine(
"\t\t\t\tP: \"Receive Shadows\", \"bool\", \"\", \"\",1");
330 sb.AppendLine(
"\t\t\t}");
331 sb.AppendLine(
"\t\t}");
332 sb.AppendLine(
"\t}");
335 sb.AppendLine(
"\tObjectType: \"Material\" {");
336 sb.AppendLine(
"\t\tCount: 1");
337 sb.AppendLine(
"\t\tPropertyTemplate: \"FbxSurfacePhong\" {");
338 sb.AppendLine(
"\t\t\tProperties70: {");
339 sb.AppendLine(
"\t\t\t\tP: \"ShadingModel\", \"KString\", \"\", \"\", \"Phong\"");
340 sb.AppendLine(
"\t\t\t\tP: \"MultiLayer\", \"bool\", \"\", \"\",0");
341 sb.AppendLine(
"\t\t\t\tP: \"EmissiveColor\", \"Color\", \"\", \"A\",0,0,0");
342 sb.AppendLine(
"\t\t\t\tP: \"EmissiveFactor\", \"Number\", \"\", \"A\",1");
343 sb.AppendLine(
"\t\t\t\tP: \"AmbientColor\", \"Color\", \"\", \"A\",0.2,0.2,0.2");
344 sb.AppendLine(
"\t\t\t\tP: \"AmbientFactor\", \"Number\", \"\", \"A\",1");
345 sb.AppendLine(
"\t\t\t\tP: \"DiffuseColor\", \"Color\", \"\", \"A\",0.8,0.8,0.8");
346 sb.AppendLine(
"\t\t\t\tP: \"DiffuseFactor\", \"Number\", \"\", \"A\",1");
347 sb.AppendLine(
"\t\t\t\tP: \"Bump\", \"Vector3D\", \"Vector\", \"\",0,0,0");
348 sb.AppendLine(
"\t\t\t\tP: \"NormalMap\", \"Vector3D\", \"Vector\", \"\",0,0,0");
349 sb.AppendLine(
"\t\t\t\tP: \"BumpFactor\", \"double\", \"Number\", \"\",1");
350 sb.AppendLine(
"\t\t\t\tP: \"TransparentColor\", \"Color\", \"\", \"A\",0,0,0");
351 sb.AppendLine(
"\t\t\t\tP: \"TransparencyFactor\", \"Number\", \"\", \"A\",0");
352 sb.AppendLine(
"\t\t\t\tP: \"DisplacementColor\", \"ColorRGB\", \"Color\", \"\",0,0,0");
353 sb.AppendLine(
"\t\t\t\tP: \"DisplacementFactor\", \"double\", \"Number\", \"\",1");
354 sb.AppendLine(
"\t\t\t\tP: \"VectorDisplacementColor\", \"ColorRGB\", \"Color\", \"\",0,0,0");
355 sb.AppendLine(
"\t\t\t\tP: \"VectorDisplacementFactor\", \"double\", \"Number\", \"\",1");
356 sb.AppendLine(
"\t\t\t\tP: \"SpecularColor\", \"Color\", \"\", \"A\",0.2,0.2,0.2");
357 sb.AppendLine(
"\t\t\t\tP: \"SpecularFactor\", \"Number\", \"\", \"A\",1");
358 sb.AppendLine(
"\t\t\t\tP: \"ShininessExponent\", \"Number\", \"\", \"A\",20");
359 sb.AppendLine(
"\t\t\t\tP: \"ReflectionColor\", \"Color\", \"\", \"A\",0,0,0");
360 sb.AppendLine(
"\t\t\t\tP: \"ReflectionFactor\", \"Number\", \"\", \"A\",1");
361 sb.AppendLine(
"\t\t\t}");
362 sb.AppendLine(
"\t\t}");
363 sb.AppendLine(
"\t}");
366 sb.AppendLine(
"\tObjectType: \"Texture\" {");
367 sb.AppendLine(
"\t\tCount: 2");
368 sb.AppendLine(
"\t\tPropertyTemplate: \"FbxFileTexture\" {");
369 sb.AppendLine(
"\t\t\tProperties70: {");
370 sb.AppendLine(
"\t\t\t\tP: \"TextureTypeUse\", \"enum\", \"\", \"\",0");
371 sb.AppendLine(
"\t\t\t\tP: \"Texture alpha\", \"Number\", \"\", \"A\",1");
372 sb.AppendLine(
"\t\t\t\tP: \"CurrentMappingType\", \"enum\", \"\", \"\",0");
373 sb.AppendLine(
"\t\t\t\tP: \"WrapModeU\", \"enum\", \"\", \"\",0");
374 sb.AppendLine(
"\t\t\t\tP: \"WrapModeV\", \"enum\", \"\", \"\",0");
375 sb.AppendLine(
"\t\t\t\tP: \"UVSwap\", \"bool\", \"\", \"\",0");
376 sb.AppendLine(
"\t\t\t\tP: \"PremultiplyAlpha\", \"bool\", \"\", \"\",1");
377 sb.AppendLine(
"\t\t\t\tP: \"Translation\", \"Vector\", \"\", \"A\",0,0,0");
378 sb.AppendLine(
"\t\t\t\tP: \"Rotation\", \"Vector\", \"\", \"A\",0,0,0");
379 sb.AppendLine(
"\t\t\t\tP: \"Scaling\", \"Vector\", \"\", \"A\",1,1,1");
380 sb.AppendLine(
"\t\t\t\tP: \"TextureRotationPivot\", \"Vector3D\", \"Vector\", \"\",0,0,0");
381 sb.AppendLine(
"\t\t\t\tP: \"TextureScalingPivot\", \"Vector3D\", \"Vector\", \"\",0,0,0");
382 sb.AppendLine(
"\t\t\t\tP: \"CurrentTextureBlendMode\", \"enum\", \"\", \"\",1");
383 sb.AppendLine(
"\t\t\t\tP: \"UVSet\", \"KString\", \"\", \"\", \"default\"");
384 sb.AppendLine(
"\t\t\t\tP: \"UseMaterial\", \"bool\", \"\", \"\",0");
385 sb.AppendLine(
"\t\t\t\tP: \"UseMipMap\", \"bool\", \"\", \"\",0");
386 sb.AppendLine(
"\t\t\t}");
387 sb.AppendLine(
"\t\t}");
388 sb.AppendLine(
"\t}");
393 sb.Append(objectProps.ToString());
394 sb.Append(objectConnections.ToString());
396 return sb.ToString();
399 public static void CopyComplexMaterialsToPath(GameObject gameObj,
string path,
bool copyTextures,
string texturesFolder =
"/Textures",
string materialsFolder =
"/Materials")
402 int folderIndex = path.LastIndexOf(
'/');
403 path = path.Remove(folderIndex, path.Length - folderIndex);
406 string texturesPath = path + texturesFolder;
407 string materialsPath = path + materialsFolder;
409 if(Directory.Exists(path) ==
false)
410 Directory.CreateDirectory(path);
411 if(Directory.Exists(materialsPath) ==
false)
412 Directory.CreateDirectory(materialsPath);
417 Renderer[] meshRenderers = gameObj.GetComponentsInChildren<Renderer>();
418 List<Material> everyMaterial =
new List<Material>();
419 for(
int i = 0; i < meshRenderers.Length; i++)
421 for(
int n = 0; n < meshRenderers[i].sharedMaterials.Length; n++)
423 everyMaterial.Add(meshRenderers[i].sharedMaterials[n]);
428 Material[] everyDistinctMaterial = everyMaterial.Distinct().ToArray<Material>();
429 everyDistinctMaterial = everyDistinctMaterial.OrderBy(o => o.name).ToArray<Material>();
432 for(
int i = 0; i < everyDistinctMaterial.Length; i++)
434 for(
int n = 0; n < everyDistinctMaterial.Length; n++)
439 if(everyDistinctMaterial[i].name == everyDistinctMaterial[n].name)
441 Debug.LogErrorFormat(
"Two distinct materials {0} and {1} have the same name, this will not work with the FBX Exporter", everyDistinctMaterial[i], everyDistinctMaterial[n]);
447 List<string> everyMaterialName =
new List<string>();
450 for(
int i = 0; i < everyDistinctMaterial.Length; i++)
452 string newName = gameObj.name +
"_" + everyDistinctMaterial[i].name;
453 string fullPath = materialsPath +
"/" + newName +
".mat";
455 if(File.Exists(fullPath))
456 File.Delete(fullPath);
459 everyMaterialName.Add(newName);
463 AssetDatabase.Refresh();
465 List<Material> allNewMaterials =
new List<Material>();
467 for (
int i = 0; i < everyMaterialName.Count; i++)
469 string assetPath = materialsPath;
470 if(assetPath[assetPath.Length - 1] !=
'/')
473 assetPath += everyMaterialName[i] +
".mat";
475 Material sourceMat = (Material)AssetDatabase.LoadAssetAtPath(assetPath, typeof(Material));
477 if(sourceMat !=
null)
478 allNewMaterials.Add(sourceMat);
485 if(Directory.Exists(texturesPath) ==
false)
486 Directory.CreateDirectory(texturesPath);
488 AssetDatabase.Refresh();
490 for(
int i = 0; i < allNewMaterials.Count; i++)
492 allNewMaterials[i] = CopyTexturesAndAssignCopiesToMaterial(allNewMaterials[i], texturesPath);
496 AssetDatabase.Refresh();
503 string path = newFolderPath;
505 if(path[path.Length - 1] !=
'/')
515 string assetPath = AssetDatabase.GetAssetPath(obj);
516 string extension = Path.GetExtension(assetPath);
518 string newFileName = path + newName + extension;
520 if(File.Exists(newFileName))
523 return AssetDatabase.CopyAsset(assetPath, newFileName);
535 private static string GetFileName(
string path)
537 string fileName = path.ToString();
538 fileName = fileName.Remove(0, fileName.LastIndexOf(
'/') + 1);
543 private static Material CopyTexturesAndAssignCopiesToMaterial(Material material,
string newPath)
545 if(material.shader.name ==
"Standard" || material.shader.name ==
"Standard (Specular setup)")
547 GetTextureUpdateMaterialWithPath(material,
"_MainTex", newPath);
549 if(material.shader.name ==
"Standard")
550 GetTextureUpdateMaterialWithPath(material,
"_MetallicGlossMap", newPath);
552 if(material.shader.name ==
"Standard (Specular setup)")
553 GetTextureUpdateMaterialWithPath(material,
"_SpecGlossMap", newPath);
555 GetTextureUpdateMaterialWithPath(material,
"_BumpMap", newPath);
556 GetTextureUpdateMaterialWithPath(material,
"_BumpMap", newPath);
557 GetTextureUpdateMaterialWithPath(material,
"_BumpMap", newPath);
558 GetTextureUpdateMaterialWithPath(material,
"_BumpMap", newPath);
559 GetTextureUpdateMaterialWithPath(material,
"_ParallaxMap", newPath);
560 GetTextureUpdateMaterialWithPath(material,
"_OcclusionMap", newPath);
561 GetTextureUpdateMaterialWithPath(material,
"_EmissionMap", newPath);
562 GetTextureUpdateMaterialWithPath(material,
"_DetailMask", newPath);
563 GetTextureUpdateMaterialWithPath(material,
"_DetailAlbedoMap", newPath);
564 GetTextureUpdateMaterialWithPath(material,
"_DetailNormalMap", newPath);
568 Debug.LogError(
"WARNING: " + material.name +
" is not a physically based shader, may not export to package correctly");
580 private static void GetTextureUpdateMaterialWithPath(Material material,
string textureShaderName,
string newPath)
582 Texture textureInQ = material.GetTexture(textureShaderName);
583 if(textureInQ !=
null)
585 string name = material.name + textureShaderName;
588 if(newTexture !=
null)
589 material.SetTexture(textureShaderName, newTexture);
596 string path = newFolderPath;
598 if(path[path.Length - 1] !=
'/')
600 string testPath = path.Remove(path.Length - 1);
602 if(System.IO.Directory.Exists(testPath) ==
false)
604 Debug.LogError(
"This folder does not exist " + testPath);
608 string assetPath = AssetDatabase.GetAssetPath(obj);
609 string fileName = GetFileName(assetPath);
610 string extension = fileName.Remove(0, fileName.LastIndexOf(
'.'));
612 string newFullPathName = path + newName + extension;
614 if(AssetDatabase.CopyAsset(assetPath, newFullPathName) ==
false)
617 AssetDatabase.Refresh();
619 return AssetDatabase.LoadAssetAtPath(newFullPathName, typeof(Texture));
633 return val.ToString();
635 string stringValue = val.ToString();
637 int index = CheckForCommaInsteadOfDecimal(ref stringValue);
639 stringValue = ReplaceCommasWithDecimals(stringValue, index);
647 private static int CheckForCommaInsteadOfDecimal(ref
string vert)
651 for(
int i = 0, l = vert.Length; i < l; i++)
660 private static string ReplaceCommasWithDecimals(
string vert,
int breakIndex)
662 return vert.Remove(breakIndex, vert.Length - breakIndex) +
"." + vert.Remove(0, breakIndex + 1);
static string MeshToString(GameObject gameObj, string newPath, bool copyMaterials=false, bool copyTextures=false)
static long GetRandomFBXId()
static string VersionInformation
static Object CopyAndRenameAssetReturnObject(Object obj, string newName, string newFolderPath)
static bool ExportGameObjToFBX(GameObject gameObj, string newPath, bool copyMaterials=false, bool copyTextures=false)
static void CopyComplexMaterialsToPath(GameObject gameObj, string path, bool copyTextures, string texturesFolder="/Textures", string materialsFolder="/Materials")
static string FBXFormat(float val)
Provides internationalization for countries that use commas instead of decimals to denote the break p...
static bool CopyAndRenameAsset(Object obj, string newName, string newFolderPath)
static string ExportGameObjToFBXString(GameObject gameObj)
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
static long GetMeshToString(GameObject gameObj, Material[] materials, ref StringBuilder objects, ref StringBuilder connections, GameObject parentObject=null, long parentModelId=0)
Gets all the meshes and outputs to a string (even grabbing the child of each gameObject)