UniVRM/Assets/VRM/UniGLTF/Scripts/IO/UnityPath.cs
ousttrue bf6290ff9d Squashed commit of the following:
commit 2337e6cc3f3911fd32fa82d38b57a4e2ad29c86e
Author: Masataka SUMI <santarh@gmail.com>
Date:   Wed Jun 19 22:06:08 2019 +0900

    Do not integrate below Unity 2017.2

commit edbae238840aaf7157e0dd81ca03e11e3aeaf50b
Author: Masataka SUMI <santarh@gmail.com>
Date:   Wed Jun 19 21:37:06 2019 +0900

    Add Integrate method for Runtime

commit ad50a2e5565aea0d8e13f04bbe984671e66692bd
Author: Masataka SUMI <santarh@gmail.com>
Date:   Wed Jun 19 19:09:17 2019 +0900

    MeshIntegrator only work with prefab.

commit 3fb428e0ef05adb490f98bf0391f22a58c2c5d41
Author: Masataka SUMI <santarh@gmail.com>
Date:   Wed Jun 19 19:08:51 2019 +0900

    Add FileName Property to UnityPath

commit e17e42d3c91238923352511f4979d4139ed15a83
Author: Masataka SUMI <santarh@gmail.com>
Date:   Wed Jun 19 15:48:22 2019 +0900

    Refactoring GetPrefab method

commit ab1b3b9535e7cf21de43b597318347f34ba771c2
Author: Masataka SUMI <santarh@gmail.com>
Date:   Tue Jun 11 20:09:34 2019 +0900

    Blendshape's values Follow renderers change.

commit 194599955b91d62410a08f16f159ce7ddcaec635
Author: Masataka SUMI <santarh@gmail.com>
Date:   Tue Jun 11 03:37:28 2019 +0900

    Move general integration method from Editor to Runtime.

commit bbe2697e298c8c61d66a46e33c61545ee4d6cdae
Author: Masataka SUMI <santarh@gmail.com>
Date:   Tue Jun 11 03:18:28 2019 +0900

    Move integration method from Editor to Runtime.

commit a60e4da63ec40761cdd53ef75c3245b36e4d0ba2
Author: Masataka SUMI <santarh@gmail.com>
Date:   Tue Jun 11 02:46:29 2019 +0900

    Disabled source renderers after integration.

commit 28cb426331b071020fe5806bb929019a2617f58c
Author: Masataka SUMI <santarh@gmail.com>
Date:   Tue Jun 11 02:11:12 2019 +0900

    Consider Renderer with blendshapes.

commit 898ee75ac7f1457a8d2e6b02d6d40d607565d430
Author: Masataka SUMI <santarh@gmail.com>
Date:   Tue Jun 11 01:57:16 2019 +0900

    Refactoring codes.

commit bb2339dd14eef7cedce5a1a446f54741fea02521
Author: Masataka SUMI <santarh@gmail.com>
Date:   Tue Jun 11 01:36:32 2019 +0900

    Refactoring.

commit 8a0330fa4a8cc3458997bfc342589565fd16b6c7
Author: Masataka SUMI <santarh@gmail.com>
Date:   Tue Jun 11 01:36:26 2019 +0900

    Remove unused bone root finding code.

commit 90b1a1a82b5b948bb3e33030d4392dbc10c800ef
Author: Masataka SUMI <santarh@gmail.com>
Date:   Mon Jun 10 23:51:05 2019 +0900

    Consider zero bone SkinnedMesh.

commit 16949adabedc9ca9ff7e98a4aacb5147fb956731
Author: Masataka SUMI <santarh@gmail.com>
Date:   Mon Jun 10 23:45:13 2019 +0900

    Fix bug of merging blendshapes.

commit 559921ce2620dc0152a2a88f1ca93229670ca1f7
Author: Masataka SUMI <santarh@gmail.com>
Date:   Mon Jun 10 21:24:20 2019 +0900

    Divide MeshIntegrator classes
2020-08-25 17:00:25 +09:00

436 lines
10 KiB
C#

using System;
using System.IO;
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UniGLTF
{
/// <summary>
/// relative path from Unity project root.
/// For AssetDatabase.
/// </summary>
public struct UnityPath
{
#region UnityPath
public string Value
{
get;
private set;
}
public override string ToString()
{
return string.Format("unity://{0}", Value);
}
public bool IsNull
{
get { return Value == null; }
}
public bool IsUnderAssetsFolder
{
get
{
if (IsNull)
{
return false;
}
return Value == "Assets" || Value.StartsWith("Assets/");
}
}
public bool IsStreamingAsset
{
get
{
if (IsNull)
{
return false;
}
return FullPath.StartsWith(Application.streamingAssetsPath + "/");
}
}
public string FileName
{
get { return Path.GetFileName(Value); }
}
public string FileNameWithoutExtension
{
get { return Path.GetFileNameWithoutExtension(Value); }
}
public string Extension
{
get { return Path.GetExtension(Value); }
}
public UnityPath Parent
{
get
{
if (IsNull)
{
return default(UnityPath);
}
return new UnityPath(Path.GetDirectoryName(Value));
}
}
public bool HasParent
{
get
{
return !string.IsNullOrEmpty(Value);
}
}
static readonly char[] EscapeChars = new char[]
{
'\\',
'/',
':',
'*',
'?',
'"',
'<',
'>',
'|',
};
static string EscapeFilePath(string path)
{
foreach (var x in EscapeChars)
{
path = path.Replace(x, '+');
}
return path;
}
public UnityPath Child(string name)
{
if (IsNull)
{
throw new NotImplementedException();
}
else if (Value == "")
{
return new UnityPath(name);
}
else
{
return new UnityPath(Value + "/" + name);
}
}
public override int GetHashCode()
{
if (IsNull)
{
return 0;
}
return Value.GetHashCode();
}
public override bool Equals(object obj)
{
if(obj is UnityPath)
{
var rhs = (UnityPath)obj;
if(Value==null && rhs.Value == null)
{
return true;
}
else if (Value == null)
{
return false;
}
else if (rhs.Value == null)
{
return false;
}
else
{
return Value == rhs.Value;
}
}
else
{
return false;
}
}
/// <summary>
/// Remove extension and add suffix
/// </summary>
/// <param name="prefabPath"></param>
/// <param name="suffix"></param>
/// <returns></returns>
public UnityPath GetAssetFolder(string suffix)
{
if (!IsUnderAssetsFolder)
{
throw new NotImplementedException();
}
return new UnityPath(
string.Format("{0}/{1}{2}",
Parent.Value,
FileNameWithoutExtension,
suffix
));
}
UnityPath(string value) : this()
{
Value = value.Replace("\\", "/");
}
/// <summary>
///
/// </summary>
/// <param name="unityPath">Relative from unity current path. GetParent(Application.dataPath)</param>
/// <returns></returns>
public static UnityPath FromUnityPath(string unityPath)
{
if (String.IsNullOrEmpty(unityPath))
{
return new UnityPath
{
Value=""
};
}
return FromFullpath(Path.GetFullPath(unityPath));
}
#endregion
#region FullPath
static string s_basePath;
static string BaseFullPath
{
get
{
if (string.IsNullOrEmpty(s_basePath))
{
s_basePath = Path.GetFullPath(Application.dataPath + "/..").Replace("\\", "/");
}
return s_basePath;
}
}
static string AssetFullPath
{
get
{
return BaseFullPath + "/Assets";
}
}
public string FullPath
{
get
{
if (IsNull)
{
throw new NotImplementedException();
}
return Path.Combine(BaseFullPath, Value).Replace("\\", "/");
}
}
public bool IsFileExists
{
get { return File.Exists(FullPath); }
}
public bool IsDirectoryExists
{
get { return Directory.Exists(FullPath); }
}
/// <summary>
///
/// </summary>
/// <param name="fullPath">C:/path/to/file</param>
/// <returns></returns>
public static UnityPath FromFullpath(string fullPath)
{
if(fullPath == null)
{
fullPath = "";
}
fullPath = fullPath.Replace("\\", "/");
if (fullPath == BaseFullPath) {
return new UnityPath
{
Value=""
};
}
else if(fullPath.StartsWith(BaseFullPath + "/"))
{
return new UnityPath(fullPath.Substring(BaseFullPath.Length + 1));
}
else
{
return default(UnityPath);
}
}
public static bool IsUnderAssetFolder(string fullPath)
{
return fullPath.Replace("\\", "/").StartsWith(AssetFullPath);
}
#endregion
[Obsolete("Use TraverseDir()")]
public IEnumerable<UnityPath> TravserseDir()
{
return TraverseDir();
}
public IEnumerable<UnityPath> TraverseDir()
{
if (IsDirectoryExists)
{
yield return this;
foreach(var child in ChildDirs)
{
foreach(var x in child.TraverseDir())
{
yield return x;
}
}
}
}
public IEnumerable<UnityPath> ChildDirs
{
get
{
foreach(var x in Directory.GetDirectories(FullPath))
{
yield return UnityPath.FromFullpath(x);
}
}
}
public IEnumerable<UnityPath> ChildFiles
{
get
{
foreach (var x in Directory.GetFiles(FullPath))
{
yield return UnityPath.FromFullpath(x);
}
}
}
#if UNITY_EDITOR
public T GetImporter<T>() where T : AssetImporter
{
return AssetImporter.GetAtPath(Value) as T;
}
public static UnityPath FromAsset(UnityEngine.Object asset)
{
return new UnityPath(AssetDatabase.GetAssetPath(asset));
}
public void ImportAsset()
{
if (!IsUnderAssetsFolder)
{
throw new NotImplementedException();
}
AssetDatabase.ImportAsset(Value);
}
public void EnsureFolder()
{
if (IsNull)
{
throw new NotImplementedException();
}
if (HasParent)
{
Parent.EnsureFolder();
}
if (!IsDirectoryExists)
{
var parent = Parent;
// ensure parent
parent.ImportAsset();
// create
AssetDatabase.CreateFolder(
parent.Value,
Path.GetFileName(Value)
);
ImportAsset();
}
}
public UnityEngine.Object[] GetSubAssets()
{
if (!IsUnderAssetsFolder)
{
throw new NotImplementedException();
}
return AssetDatabase.LoadAllAssetsAtPath(Value);
}
public void CreateAsset(UnityEngine.Object o)
{
if (!IsUnderAssetsFolder)
{
throw new NotImplementedException();
}
AssetDatabase.CreateAsset(o, Value);
}
public void AddObjectToAsset(UnityEngine.Object o)
{
if (!IsUnderAssetsFolder)
{
throw new NotImplementedException();
}
AssetDatabase.AddObjectToAsset(o, Value);
}
public T LoadAsset<T>() where T : UnityEngine.Object
{
if (!IsUnderAssetsFolder)
{
throw new NotImplementedException();
}
return AssetDatabase.LoadAssetAtPath<T>(Value);
}
public UnityPath GenerateUniqueAssetPath()
{
if (!IsUnderAssetsFolder)
{
throw new NotImplementedException();
}
return new UnityPath(AssetDatabase.GenerateUniqueAssetPath(Value));
}
#endif
}
}