Skip to content

Commit

Permalink
use proper path extension generation for both GLTF and GLB export
Browse files Browse the repository at this point in the history
  • Loading branch information
hybridherbst committed Mar 21, 2021
1 parent cd7ffd1 commit 4c16cf5
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,7 @@ public GLTFRoot GetRoot()
/// <param name="fileName">The name of the GLTF file</param>
public void SaveGLB(string path, string fileName)
{
var fullPath = Path.Combine(path, fileName);
if (!Path.GetExtension(fileName).Equals("glb", StringComparison.OrdinalIgnoreCase))
{
fullPath = Path.Combine(path, fileName + ".glb");
}
var fullPath = GetFileName(Path.Combine(path, fileName), "glb");
_shouldUseInternalBufferForImages = true;

using (FileStream glbFile = new FileStream(fullPath, FileMode.Create))
Expand Down Expand Up @@ -369,7 +365,8 @@ public static uint CalculateAlignment(uint currentSize, uint byteAlignment)
public void SaveGLTFandBin(string path, string fileName)
{
_shouldUseInternalBufferForImages = false;
var binFile = File.Create(Path.Combine(path, fileName + ".bin"));
var fullPath = GetFileName(Path.Combine(path, fileName), "bin");
var binFile = File.Create(fullPath);
_bufferWriter = new BinaryWriter(binFile);

// rotate 180°
Expand All @@ -395,7 +392,7 @@ public void SaveGLTFandBin(string path, string fileName)
_buffer.Uri = fileName + ".bin";
_buffer.ByteLength = CalculateAlignment((uint)_bufferWriter.BaseStream.Length, 4);

var gltfFile = File.CreateText(Path.Combine(path, fileName + ".gltf"));
var gltfFile = File.CreateText(Path.ChangeExtension(fullPath, ".gltf"));
_root.Serialize(gltfFile);

#if WINDOWS_UWP
Expand All @@ -411,6 +408,22 @@ public void SaveGLTFandBin(string path, string fileName)
// t.rotation *= Quaternion.Euler(0,-180,0);
}

/// <summary>
/// Ensures a specific file extension from an absolute path that may or may not already have that extension.
/// </summary>
/// <param name="absolutePathThatMayHaveExtension">Absolute path that may or may not already have the required extension</param>
/// <param name="requiredExtension">The extension to ensure</param>
/// <returns>An absolute path that has the required extension</returns>
private string GetFileName(string absolutePathThatMayHaveExtension, string requiredExtension)
{
if (!Path.GetExtension(absolutePathThatMayHaveExtension).Equals(requiredExtension, StringComparison.OrdinalIgnoreCase))
{
return absolutePathThatMayHaveExtension + "." + requiredExtension;
}

return absolutePathThatMayHaveExtension;
}

private void ExportImages(string outputPath)
{
for (int t = 0; t < _imageInfos.Count; ++t)
Expand Down

0 comments on commit 4c16cf5

Please sign in to comment.