/// <summary> /// プレハブが依存しているマテリアルを複製します。 /// </summary> /// <param name="prefabInstance"></param> private static void DuplicateMaterials(GameObject prefabInstance) { var alreadyDuplicatedMaterials = new Dictionary <Material, Material>(); foreach (var renderer in prefabInstance.GetComponentsInChildren <Renderer>()) { renderer.sharedMaterials = renderer.sharedMaterials.Select(material => { if (alreadyDuplicatedMaterials.ContainsKey(material)) { return(alreadyDuplicatedMaterials[material]); } return(alreadyDuplicatedMaterials[material] = Duplicator.DuplicateAssetToFolder <Material>(source: material, prefabInstance: prefabInstance)); }).ToArray(); } }
/// <summary> /// プレハブが依存しているメッシュを複製・結合します。 /// </summary> /// <param name="prefabInstance"></param> /// <param name="combineMeshesAndSubMeshes"></param> /// <param name="notCombineRendererObjectNames"></param> private static void DuplicateAndCombineMeshes( GameObject prefabInstance, bool combineMeshesAndSubMeshes, IEnumerable <string> notCombineRendererObjectNames ) { var faceMeshRenderer = combineMeshesAndSubMeshes ? null : Duplicator.GetFaceMeshRenderer(prefabInstance: prefabInstance); var sameNameTransform = prefabInstance.transform.Find(VRChatUtility.AutoBlinkMeshPath); if (sameNameTransform && (combineMeshesAndSubMeshes || faceMeshRenderer.transform != sameNameTransform)) { sameNameTransform.name += "-" + VRChatUtility.AutoBlinkMeshPath; } var prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(prefabInstance); if (combineMeshesAndSubMeshes) { CombineMeshesAndSubMeshes.Combine( root: prefabInstance, notCombineRendererObjectNames, destinationObjectName: VRChatUtility.AutoBlinkMeshPath ); } else { if (faceMeshRenderer.transform != sameNameTransform) { faceMeshRenderer.transform.parent = prefabInstance.transform; faceMeshRenderer.transform.name = VRChatUtility.AutoBlinkMeshPath; } } PrefabUtility.SaveAsPrefabAssetAndConnect(prefabInstance, prefabPath, InteractionMode.AutomatedAction); var alreadyDuplicatedMeshes = new Dictionary <Mesh, Mesh>(); foreach (var renderer in prefabInstance.GetComponentsInChildren <SkinnedMeshRenderer>()) { if (combineMeshesAndSubMeshes && renderer.name == VRChatUtility.AutoBlinkMeshPath) { continue; } var mesh = renderer.sharedMesh; renderer.sharedMesh = alreadyDuplicatedMeshes.ContainsKey(mesh) ? alreadyDuplicatedMeshes[mesh] : Duplicator.DuplicateAssetToFolder <Mesh>( source: mesh, prefabInstance, fileName: Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(mesh)) == VRChatUtility.AutoBlinkMeshPath + ".asset" ? VRChatUtility.AutoBlinkMeshPath + "-" + VRChatUtility.AutoBlinkMeshPath + ".asset" : "" ); alreadyDuplicatedMeshes[mesh] = renderer.sharedMesh; } foreach (var filter in prefabInstance.GetComponentsInChildren <MeshFilter>()) { Mesh mesh = filter.sharedMesh; filter.sharedMesh = alreadyDuplicatedMeshes.ContainsKey(mesh) ? alreadyDuplicatedMeshes[mesh] : Duplicator.DuplicateAssetToFolder <Mesh>( source: mesh, prefabInstance, fileName: Path.GetFileNameWithoutExtension(AssetDatabase.GetAssetPath(mesh)) == VRChatUtility.AutoBlinkMeshPath + ".asset" ? VRChatUtility.AutoBlinkMeshPath + "-" + VRChatUtility.AutoBlinkMeshPath + ".asset" : "" ); alreadyDuplicatedMeshes[mesh] = filter.sharedMesh; } }