@@ -61,6 +61,47 @@ bool combineMeshesAndSubMeshes
6161 return destinationPrefabInstance ;
6262 }
6363
64+ /// <summary>
65+ /// アセットの保存先のフォルダを決定します。
66+ /// </summary>
67+ /// <param name="avatarGameObject"></param>
68+ /// <param name="type">アセットの種類。</param>
69+ /// <param name="fileName">ファイル名。</param>
70+ /// <returns>「Assets/」から始まるパス。
71+ /// アバターがプレハブインスタンスならプレハブのパスを、
72+ /// そうでなければ、参照されている、アセットとして存在する、最初に見つかったマテリアルのパスを、
73+ /// いずれも存在しなければ、AssetsフォルダとアバターのGameObjectの名前を結合したパスを返します。</returns>
74+ private static string DetermineDestinationFolderPath ( GameObject avatarGameObject )
75+ {
76+ var destinationFolderPath = PrefabUtility . GetPrefabAssetPathOfNearestInstanceRoot ( avatarGameObject ) ;
77+ if ( string . IsNullOrEmpty ( destinationFolderPath ) )
78+ {
79+ foreach ( var material in avatarGameObject
80+ . GetComponentsInChildren < Renderer > ( ) . SelectMany ( renderer => renderer . sharedMaterials ) )
81+ {
82+ if ( material == null )
83+ {
84+ continue ;
85+ }
86+
87+ var materialPath = AssetDatabase . GetAssetPath ( material ) ;
88+ if ( string . IsNullOrEmpty ( materialPath ) )
89+ {
90+ continue ;
91+ }
92+
93+ destinationFolderPath = materialPath ;
94+ break ;
95+ }
96+
97+ if ( string . IsNullOrEmpty ( destinationFolderPath ) )
98+ {
99+ destinationFolderPath = Path . Combine ( "Assets" , avatarGameObject . name ) ;
100+ }
101+ }
102+ return destinationFolderPath ;
103+ }
104+
64105 /// <summary>
65106 /// アセットの種類に応じて、保存先を決定します。
66107 /// </summary>
@@ -85,22 +126,6 @@ internal static string DetermineAssetPath(string destinationFolderPath, Type typ
85126 return destinationFolderUnityPath . Child ( fileName ) . Value ;
86127 }
87128
88- /// <summary>
89- /// アセットの種類に応じて、保存先を決定します。
90- /// </summary>
91- /// <param name="prefabInstance"></param>
92- /// <param name="type">アセットの種類。</param>
93- /// <param name="fileName">ファイル名。</param>
94- /// <returns>「Assets/」から始まるパス。</returns>
95- internal static string DetermineAssetPath ( GameObject prefabInstance , Type type , string fileName = "" )
96- {
97- return Duplicator . DetermineAssetPath (
98- PrefabUtility . GetPrefabAssetPathOfNearestInstanceRoot ( prefabInstance ) ,
99- type ,
100- fileName
101- ) ;
102- }
103-
104129 /// <summary>
105130 /// アセットをプレハブが置かれているディレクトリの直下のフォルダへ複製します。
106131 /// </summary>
@@ -133,10 +158,11 @@ internal static T DuplicateAssetToFolder<T>(
133158 destinationFileName = fileName ;
134159 }
135160
136- return Duplicator . DuplicateAsset (
137- source ,
138- destinationPath : Duplicator . DetermineAssetPath ( prefabInstance , typeof ( T ) , destinationFileName )
139- ) ;
161+ return Duplicator . DuplicateAsset ( source , destinationPath : Duplicator . DetermineAssetPath (
162+ Duplicator . DetermineDestinationFolderPath ( prefabInstance ) ,
163+ typeof ( T ) ,
164+ destinationFileName
165+ ) ) ;
140166 }
141167
142168 /// <summary>
@@ -209,7 +235,7 @@ string destinationFileName
209235 {
210236 return Duplicator . CreateObjectToFolder < T > (
211237 source ,
212- PrefabUtility . GetPrefabAssetPathOfNearestInstanceRoot ( prefabInstance ) ,
238+ Duplicator . DetermineDestinationFolderPath ( prefabInstance ) ,
213239 destinationFileName
214240 ) ;
215241 }
0 commit comments