Skip to content

Commit

Permalink
Merge pull request #614 from ousttrue/fix/createasset
Browse files Browse the repository at this point in the history
retry random name
  • Loading branch information
ousttrue authored Nov 16, 2020
2 parents 4544cba + 404305d commit 16239e6
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions Assets/VRM/UniGLTF/Scripts/IO/UnityPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ public override int GetHashCode()

public override bool Equals(object obj)
{
if(obj is UnityPath)
if (obj is UnityPath)
{
var rhs = (UnityPath)obj;
if(Value==null && rhs.Value == null)
if (Value == null && rhs.Value == null)
{
return true;
}
Expand Down Expand Up @@ -205,7 +205,7 @@ public static UnityPath FromUnityPath(string unityPath)
{
return new UnityPath
{
Value=""
Value = ""
};
}
return FromFullpath(Path.GetFullPath(unityPath));
Expand Down Expand Up @@ -263,19 +263,20 @@ public bool IsDirectoryExists
/// <returns></returns>
public static UnityPath FromFullpath(string fullPath)
{
if(fullPath == null)
if (fullPath == null)
{
fullPath = "";
}
fullPath = fullPath.Replace("\\", "/");

if (fullPath == BaseFullPath) {
if (fullPath == BaseFullPath)
{
return new UnityPath
{
Value=""
Value = ""
};
}
else if(fullPath.StartsWith(BaseFullPath + "/"))
else if (fullPath.StartsWith(BaseFullPath + "/"))
{
return new UnityPath(fullPath.Substring(BaseFullPath.Length + 1));
}
Expand Down Expand Up @@ -303,9 +304,9 @@ public IEnumerable<UnityPath> TraverseDir()
{
yield return this;

foreach(var child in ChildDirs)
foreach (var child in ChildDirs)
{
foreach(var x in child.TraverseDir())
foreach (var x in child.TraverseDir())
{
yield return x;
}
Expand All @@ -317,7 +318,7 @@ public IEnumerable<UnityPath> ChildDirs
{
get
{
foreach(var x in Directory.GetDirectories(FullPath))
foreach (var x in Directory.GetDirectories(FullPath))
{
yield return UnityPath.FromFullpath(x);
}
Expand Down Expand Up @@ -398,7 +399,19 @@ public void CreateAsset(UnityEngine.Object o)
throw new NotImplementedException();
}

AssetDatabase.CreateAsset(o, Value);
try
{
AssetDatabase.CreateAsset(o, Value);
}
catch (UnityException)
{
// アセットを作ることができないファイル名だったと仮定。
// 元のファイル名のどこに問題があるか不明なので Guid で置き換える。
var newName = $"{Parent.Value}/{Guid.NewGuid().ToString("N")}{Extension}";
Debug.LogWarning($"rename: {Value} => {newName}");

AssetDatabase.CreateAsset(o, newName);
}
}

public void AddObjectToAsset(UnityEngine.Object o)
Expand Down Expand Up @@ -430,6 +443,6 @@ public UnityPath GenerateUniqueAssetPath()

return new UnityPath(AssetDatabase.GenerateUniqueAssetPath(Value));
}
#endif
#endif
}
}

0 comments on commit 16239e6

Please sign in to comment.