Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistent store of Settings #576

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions UnityGLTF/Assets/UnityGLTF/Editor/Scripts/GLTFExportMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
using UnityEngine;
using UnityEngine.SceneManagement;


public class GLTFExportMenu : EditorWindow
{
public static string RetrieveTexturePath(UnityEngine.Texture texture)
public static string RetrieveTexturePath(Texture texture)
{
return AssetDatabase.GetAssetPath(texture);
}
Expand All @@ -18,18 +19,32 @@ static void Init()
window.Show();
}

private void OnEnable()
{
GLTFSceneExporter.InitSettings();
}

private void OnDisable()
{
AssetDatabase.SaveAssets();
}

void OnGUI()
{
EditorGUILayout.LabelField("Exporter", EditorStyles.boldLabel);
GLTFSceneExporter.ExportFullPath = EditorGUILayout.Toggle("Export using original path", GLTFSceneExporter.ExportFullPath);
GLTFSceneExporter.ExportNames = EditorGUILayout.Toggle("Export names of nodes", GLTFSceneExporter.ExportNames);
GLTFSceneExporter.RequireExtensions= EditorGUILayout.Toggle("Require extensions", GLTFSceneExporter.RequireExtensions);
EditorGUILayout.Separator();
GLTFSettings.Update();

EditorGUILayout.LabelField("Exporter", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(GLTFSettings.ExportNamesSp, GLTFSettings.ExportNamesGc, true);
EditorGUILayout.PropertyField(GLTFSettings.ExportFullPathSp, GLTFSettings.ExportFullPathGc, true);
EditorGUILayout.PropertyField(GLTFSettings.RequireExtensionsSp, GLTFSettings.RequireExtensionsGc, true);
EditorGUILayout.Separator();
EditorGUILayout.LabelField("Importer", EditorStyles.boldLabel);
EditorGUILayout.Separator();
EditorGUILayout.HelpBox("UnityGLTF version 0.1", MessageType.Info);
EditorGUILayout.HelpBox("Supported extensions: KHR_material_pbrSpecularGlossiness, ExtTextureTransform", MessageType.Info);
}
EditorGUILayout.HelpBox("Supported extensions: KHR_material_pbrSpecularGlossiness, ExtTextureTransform", MessageType.Info);

GLTFSettings.ApplyModifiedProperties();
}

[MenuItem("GLTF/Export Selected")]
static void ExportSelected()
Expand All @@ -45,9 +60,11 @@ static void ExportSelected()
var exportOptions = new ExportOptions { TexturePathRetriever = RetrieveTexturePath };
var exporter = new GLTFSceneExporter(Selection.transforms, exportOptions);

var path = EditorUtility.OpenFolderPanel("glTF Export Path", "", "");
if (!string.IsNullOrEmpty(path)) {
exporter.SaveGLTFandBin (path, name);
var path = EditorUtility.SaveFolderPanel("glTF Export Path", GLTFSettings.OutputPath, "");
if (!string.IsNullOrEmpty(path))
{
GLTFSettings.OutputPath = path;
exporter.SaveGLTFandBin(GLTFSettings.OutputPath, name);
}
}

Expand All @@ -65,10 +82,11 @@ static void ExportGLBSelected()
var exportOptions = new ExportOptions { TexturePathRetriever = RetrieveTexturePath };
var exporter = new GLTFSceneExporter(Selection.transforms, exportOptions);

var path = EditorUtility.OpenFolderPanel("glTF Export Path", "", "");
var path = EditorUtility.SaveFolderPanel("glTF Export Path", GLTFSettings.OutputPath, "");
if (!string.IsNullOrEmpty(path))
{
exporter.SaveGLB(path, name);
GLTFSettings.OutputPath = path;
exporter.SaveGLB(GLTFSettings.OutputPath, name);
}
}

Expand All @@ -81,9 +99,11 @@ static void ExportScene()

var exportOptions = new ExportOptions { TexturePathRetriever = RetrieveTexturePath };
var exporter = new GLTFSceneExporter(transforms, exportOptions);
var path = EditorUtility.OpenFolderPanel("glTF Export Path", "", "");
if (path != "") {
exporter.SaveGLTFandBin (path, scene.name);
var path = EditorUtility.SaveFolderPanel("glTF Export Path", GLTFSettings.OutputPath, "");
if (!string.IsNullOrEmpty(path))
{
GLTFSettings.OutputPath = path;
exporter.SaveGLTFandBin(GLTFSettings.OutputPath, scene.name);
}
}
}
30 changes: 17 additions & 13 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSceneExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ protected struct PrimKey
private readonly Dictionary<Mesh, MeshPrimitive[]> _meshToPrims = new Dictionary<Mesh, MeshPrimitive[]>();

// Settings
public static bool ExportNames = true;
public static bool ExportFullPath = true;
public static bool RequireExtensions = false;
public static GLTFSettings Settings;
public static void InitSettings()
{
Settings = GLTFSettings.CreateInstance();
}

/// <summary>
/// Create a GLTFExporter that exports out a transform
Expand All @@ -100,6 +102,8 @@ public GLTFSceneExporter(Transform[] rootTransforms, RetrieveTexturePathDelegate
/// <param name="rootTransforms">Root transform of object to export</param>
public GLTFSceneExporter(Transform[] rootTransforms, ExportOptions options)
{
InitSettings();

_exportOptions = options;

var metalGlossChannelSwapShader = Resources.Load("MetalGlossChannelSwap", typeof(Shader)) as Shader;
Expand Down Expand Up @@ -434,7 +438,7 @@ private string ConstructImageFilenamePath(Texture2D texture, string outputPath)
}

var filenamePath = Path.Combine(outputPath, imagePath);
if (!ExportFullPath)
if (!Settings.exportFullPath)
{
filenamePath = outputPath + "/" + texture.name;
}
Expand All @@ -447,7 +451,7 @@ private SceneId ExportScene(string name, Transform[] rootObjTransforms)
{
var scene = new GLTFScene();

if (ExportNames)
if (Settings.exportNames)
{
scene.Name = name;
}
Expand All @@ -471,7 +475,7 @@ private NodeId ExportNode(Transform nodeTransform)
{
var node = new Node();

if (ExportNames)
if (Settings.exportNames)
{
node.Name = nodeTransform.name;
}
Expand Down Expand Up @@ -682,7 +686,7 @@ private MeshId ExportMesh(string name, GameObject[] primitives)
// if not, create new mesh and return its id
var mesh = new GLTFMesh();

if (ExportNames)
if (Settings.exportNames)
{
mesh.Name = name;
}
Expand Down Expand Up @@ -826,7 +830,7 @@ private MaterialId ExportMaterial(Material materialObj)

var material = new GLTFMaterial();

if (ExportNames)
if (Settings.exportNames)
{
material.Name = materialObj.name;
}
Expand Down Expand Up @@ -1015,7 +1019,7 @@ private void ExportTextureTransform(TextureInfo def, Material mat, string texNam
_root.ExtensionsUsed.Add(ExtTextureTransformExtensionFactory.EXTENSION_NAME);
}

if (RequireExtensions)
if (Settings.requireExtensions)
{
if (_root.ExtensionsRequired == null)
{
Expand Down Expand Up @@ -1162,7 +1166,7 @@ private MaterialCommonConstant ExportCommonConstant(Material materialObj)
_root.ExtensionsUsed.Add("KHR_materials_common");
}

if (RequireExtensions)
if (Settings.requireExtensions)
{
if (_root.ExtensionsRequired == null)
{
Expand Down Expand Up @@ -1226,7 +1230,7 @@ private TextureId ExportTexture(Texture textureObj, TextureMapType textureMapTyp
textureObj.name = (_root.Textures.Count + 1).ToString();
}

if (ExportNames)
if (Settings.exportNames)
{
texture.Name = textureObj.name;
}
Expand Down Expand Up @@ -1264,7 +1268,7 @@ private ImageId ExportImage(Texture texture, TextureMapType texturMapType)

var image = new GLTFImage();

if (ExportNames)
if (Settings.exportNames)
{
image.Name = texture.name;
}
Expand All @@ -1282,7 +1286,7 @@ private ImageId ExportImage(Texture texture, TextureMapType texturMapType)
}

var filenamePath = Path.ChangeExtension(imagePath, ".png");
if (!ExportFullPath)
if (!Settings.exportFullPath)
{
filenamePath = Path.ChangeExtension(texture.name, ".png");
}
Expand Down
96 changes: 96 additions & 0 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System.IO;
using UnityEditor;
using UnityEngine;


public class GLTFSettings : ScriptableObject
{
public static string SettingsPath = "Assets/Plugins/Editor/GLTFSettings.asset";

private static GLTFSettings Singleton = null;

private static SerializedObject sObj;

public static SerializedProperty OutputPathSp;
public static GUIContent ExportNamesGc = new GUIContent("Export names of nodes");
public static SerializedProperty ExportNamesSp;
public static GUIContent ExportFullPathGc = new GUIContent("Export using original path");
public static SerializedProperty ExportFullPathSp;
public static GUIContent RequireExtensionsGc = new GUIContent("Require extensions");
public static SerializedProperty RequireExtensionsSp;

public string outputPath = "";
public bool exportNames = true;
public bool exportFullPath = true;
public bool requireExtensions = false;


public static GLTFSettings CreateInstance()
{
if (null == Singleton)
{
if (!File.Exists(GLTFSettings.SettingsPath))
{
var fileName = Path.GetFileName(SettingsPath);
var path = SettingsPath.Substring(0, SettingsPath.Length - fileName.Length);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
Singleton = ScriptableObject.CreateInstance<GLTFSettings>();
AssetDatabase.CreateAsset(Singleton, GLTFSettings.SettingsPath);
}

Singleton = AssetDatabase.LoadAssetAtPath<GLTFSettings>(GLTFSettings.SettingsPath);

if (sObj == null)
{
sObj = new SerializedObject(Singleton);
}

Init();
}

return Singleton;
}

private GLTFSettings() { }

public static string OutputPath
{
get
{
return OutputPathSp.stringValue;
}

set
{
OutputPathSp.stringValue = value;
ApplyModifiedProperties();
}
}

private static void Init()
{
OutputPathSp = FindProperty("outputPath");
ExportNamesSp = FindProperty("exportNames");
ExportFullPathSp = FindProperty("exportFullPath");
RequireExtensionsSp = FindProperty("requireExtensions");
}

public static void Update()
{
sObj.Update();
}

public static void ApplyModifiedProperties()
{
sObj.ApplyModifiedProperties();
AssetDatabase.SaveAssets();
}

protected static SerializedProperty FindProperty(string propertyPath)
{
return sObj.FindProperty(propertyPath);
}
}
11 changes: 11 additions & 0 deletions UnityGLTF/Assets/UnityGLTF/Runtime/Scripts/GLTFSettings.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.