248 lines
7.4 KiB
C#

using UnityEngine;
using System.IO;
using Entum;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace EasyMotionRecorder
{
public class SavePathManager : MonoBehaviour
{
[Header("저장 경로 설정")]
[SerializeField] private string motionSavePath = "Assets/Resources/Motion";
[Header("설정")]
[SerializeField] private bool createSubdirectories = true;
[Header("자동 출력 옵션")]
[SerializeField] private bool exportHumanoidOnSave = false;
[SerializeField] private bool exportGenericOnSave = false;
[SerializeField] private bool exportFBXAsciiOnSave = false;
[SerializeField] private bool exportFBXBinaryOnSave = false;
[Header("인스턴스 설정")]
[SerializeField] private string instanceID = "";
[SerializeField] private bool useDontDestroyOnLoad = false;
// 같은 오브젝트의 컴포넌트 참조
private MotionDataRecorder motionRecorder;
private FaceAnimationRecorder faceRecorder;
private ObjectMotionRecorder objectRecorder;
public bool ExportHumanoidOnSave => exportHumanoidOnSave;
public bool ExportGenericOnSave => exportGenericOnSave;
public bool ExportFBXAsciiOnSave => exportFBXAsciiOnSave;
public bool ExportFBXBinaryOnSave => exportFBXBinaryOnSave;
public string InstanceID => instanceID;
private void Awake()
{
// 인스턴스 ID가 비어있으면 자동 생성
if (string.IsNullOrEmpty(instanceID))
{
instanceID = System.Guid.NewGuid().ToString().Substring(0, 8);
}
// DontDestroyOnLoad 설정 (선택적)
if (useDontDestroyOnLoad)
{
DontDestroyOnLoad(gameObject);
}
// 같은 오브젝트의 컴포넌트들 찾기
FindAndSetupComponents();
InitializePaths();
}
private void FindAndSetupComponents()
{
// 같은 오브젝트에서 컴포넌트들 찾기
motionRecorder = GetComponent<MotionDataRecorder>();
faceRecorder = GetComponent<FaceAnimationRecorder>();
objectRecorder = GetComponent<ObjectMotionRecorder>();
// 각 컴포넌트에 인스턴스 ID 설정
if (motionRecorder != null)
{
motionRecorder.SetInstanceID(instanceID);
motionRecorder.SetSavePathManager(this);
}
if (faceRecorder != null)
{
faceRecorder.SetInstanceID(instanceID);
faceRecorder.SetSavePathManager(this);
}
if (objectRecorder != null)
{
objectRecorder.SetInstanceID(instanceID);
objectRecorder.SetSavePathManager(this);
}
}
private void InitializePaths()
{
if (createSubdirectories)
{
CreateDirectoryIfNotExists(motionSavePath);
}
}
private void CreateDirectoryIfNotExists(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
}
}
public static DirectoryInfo SafeCreateDirectory(string path)
{
if (!Directory.Exists(path))
{
return Directory.CreateDirectory(path);
}
return new DirectoryInfo(path);
}
public string GetMotionSavePath()
{
return motionSavePath;
}
public string GetFacialSavePath()
{
return motionSavePath; // 통합된 경로 사용
}
public string GetObjectSavePath()
{
return motionSavePath; // 통합된 경로 사용
}
public void SetMotionSavePath(string path)
{
motionSavePath = path;
if (createSubdirectories)
CreateDirectoryIfNotExists(path);
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
public void SetFacialSavePath(string path)
{
// 통합된 경로이므로 모션 저장 경로와 동일하게 설정
SetMotionSavePath(path);
}
public void SetObjectSavePath(string path)
{
// 통합된 경로이므로 모션 저장 경로와 동일하게 설정
SetMotionSavePath(path);
}
private void SetCreateSubdirectories(bool create)
{
createSubdirectories = create;
if (create)
{
InitializePaths();
}
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
private void SetInstanceID(string id)
{
instanceID = id;
// 모든 컴포넌트에 새 인스턴스 ID 적용
if (motionRecorder != null) motionRecorder.SetInstanceID(id);
if (faceRecorder != null) faceRecorder.SetInstanceID(id);
if (objectRecorder != null) objectRecorder.SetInstanceID(id);
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
private void SetUseDontDestroyOnLoad(bool use)
{
useDontDestroyOnLoad = use;
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
public void ResetToDefaults()
{
motionSavePath = "Assets/Resources/Motion";
createSubdirectories = true;
// 자동 출력 옵션 초기화
exportHumanoidOnSave = false;
exportGenericOnSave = false;
exportFBXAsciiOnSave = false;
exportFBXBinaryOnSave = false;
InitializePaths();
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
// 인스턴스별 고유 경로 생성
public string GetInstanceSpecificPath(string basePath)
{
if (string.IsNullOrEmpty(instanceID))
return basePath;
string directory = Path.GetDirectoryName(basePath);
string fileName = Path.GetFileNameWithoutExtension(basePath);
string extension = Path.GetExtension(basePath);
return Path.Combine(directory, $"{fileName}_{instanceID}{extension}");
}
// 자동 출력 옵션 설정 (private으로 변경)
private void SetExportHumanoidOnSave(bool value)
{
exportHumanoidOnSave = value;
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
private void SetExportGenericOnSave(bool value)
{
exportGenericOnSave = value;
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
private void SetExportFBXAsciiOnSave(bool value)
{
exportFBXAsciiOnSave = value;
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
private void SetExportFBXBinaryOnSave(bool value)
{
exportFBXBinaryOnSave = value;
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
}
}