public static void ConvertToPrefab(string searchRootDirectory, string exportDirectoryPath, SearchThreedObjectFileExtention searchFileExtention = SearchThreedObjectFileExtention.fbx, bool isExportMaterialFiles = true, bool distoributeParentFlag = false, int hierarchyNumber = 1) { List <string> pathes = FindAllThreedSearchDirectory(searchRootDirectory, searchFileExtention); Dictionary <string, GameObject> pathToThreedObjects = new Dictionary <string, GameObject>(); Dictionary <string, List <Renderer> > pathToRendererList = new Dictionary <string, List <Renderer> >(); for (int i = 0; i < pathes.Count; ++i) { string path = pathes[i]; GameObject threedObject = AssetDatabase.LoadAssetAtPath <GameObject>(path); if (threedObject == null) { continue; } pathToThreedObjects.Add(path, threedObject); if (isExportMaterialFiles) { List <Renderer> threedObjectRenderers = FindAllCompomentInChildren <Renderer>(threedObject.transform); if (threedObject.GetComponent <Renderer>() != null) { threedObjectRenderers.Add(threedObject.GetComponent <Renderer>()); } pathToRendererList.Add(path, threedObjectRenderers); } } Dictionary <string, Material> generatedMaterialFiles = new Dictionary <string, Material>(); Dictionary <Renderer, List <Material> > attachDic = new Dictionary <Renderer, List <Material> >(); List <GameObject> generatedPrefabs = new List <GameObject>(); foreach (KeyValuePair <string, GameObject> pathThreedObject in pathToThreedObjects) { string prefabFilePath = SetupAndGetPlaneFilePath(exportDirectoryPath, pathThreedObject.Key, distoributeParentFlag, hierarchyNumber) + ".prefab"; if (File.Exists(prefabFilePath)) { continue; } GameObject generatedPrefab = PrefabUtility.CreatePrefab(prefabFilePath, pathThreedObject.Value); generatedPrefabs.Add(generatedPrefab); if (isExportMaterialFiles) { //Get new Prefabs Renderer List <Renderer> newRenderers = FindAllCompomentInChildren <Renderer>(generatedPrefab.transform); if (generatedPrefab.GetComponent <Renderer>() != null) { newRenderers.Add(generatedPrefab.GetComponent <Renderer>()); } List <string> splitPathes = new List <string>(prefabFilePath.Split("/".ToCharArray())); int joinArrayCount = Mathf.Max(splitPathes.Count - 1, 0); if (distoributeParentFlag) { joinArrayCount = Mathf.Max(splitPathes.Count - 2, 0); } string rootMaterialDirectoryPath = string.Join("/", splitPathes.GetRange(0, joinArrayCount).ToArray()) + "/Materials/"; List <Renderer> originRenderers = pathToRendererList[pathThreedObject.Key]; for (int i = 0; i < originRenderers.Count; ++i) { Material[] mats = originRenderers[i].sharedMaterials; if (mats != null) { List <Material> copyMaterials = new List <Material>(); for (int j = 0; j < mats.Length; ++j) { string materialFilePath = SetupAndGetPlaneFilePath(rootMaterialDirectoryPath, mats[j].name) + ".mat"; if (File.Exists(materialFilePath)) { copyMaterials.Add(generatedMaterialFiles[materialFilePath]); } else { Material copyMaterial = CopyAssetFile(materialFilePath, mats[j]); copyMaterials.Add(copyMaterial); generatedMaterialFiles.Add(materialFilePath, copyMaterial); } } attachDic.Add(newRenderers[i], copyMaterials); } } } } AssetDatabase.StartAssetEditing(); foreach (KeyValuePair <Renderer, List <Material> > newRendererMaterials in attachDic) { newRendererMaterials.Key.materials = newRendererMaterials.Value.ToArray(); } AssetDatabase.StopAssetEditing(); for (int i = 0; i < generatedPrefabs.Count; ++i) { EditorUtility.SetDirty(generatedPrefabs[i]); } foreach (KeyValuePair <Renderer, List <Material> > newRendererMaterials in attachDic) { foreach (Material mat in newRendererMaterials.Value) { EditorUtility.SetDirty(mat); } } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); Debug.Log("Convert ThreedObject To Prefab Count:" + generatedPrefabs.Count); }
public static List <string> FindAllThreedSearchDirectory(string searchRootDirectory, SearchThreedObjectFileExtention searchFileExtention = SearchThreedObjectFileExtention.fbx) { string extension = searchFileExtention.ToString(); if (searchFileExtention == SearchThreedObjectFileExtention.threeds) { extension = "3ds"; } return(FindAllThreedSearchDirectory(searchRootDirectory, extension)); }
public static void DissociateAnimationClip(string searchRootDirectory, string exportDirectoryPath, bool distoributeParentFlag = false, SearchThreedObjectFileExtention searchFileExtention = SearchThreedObjectFileExtention.fbx, int hierarchyNumber = 1) { List <string> pathes = FindAllThreedSearchDirectory(searchRootDirectory, searchFileExtention); Dictionary <string, List <AnimationClip> > pathToAnimationClips = new Dictionary <string, List <AnimationClip> >(); for (int i = 0; i < pathes.Count; ++i) { string path = pathes[i]; List <AnimationClip> animationClips = new List <AnimationClip>(); object[] threedObjects = AssetDatabase.LoadAllAssetsAtPath(path); if (threedObjects == null) { continue; } for (int j = 0; j < threedObjects.Length; ++j) { if (threedObjects[j] is AnimationClip) { AnimationClip clip = threedObjects[j] as AnimationClip; if (clip.name.StartsWith("__preview__")) { continue; } animationClips.Add(clip); } } if (animationClips.Count <= 0) { continue; } pathToAnimationClips.Add(path, animationClips); } List <AnimationClip> generatedClips = new List <AnimationClip>(); foreach (KeyValuePair <string, List <AnimationClip> > pathClips in pathToAnimationClips) { string animRootFileNamePath = SetupAndGetPlaneFilePath(exportDirectoryPath, pathClips.Key, distoributeParentFlag, hierarchyNumber); for (int i = 0; i < pathClips.Value.Count; ++i) { string animFileName = animRootFileNamePath; if (i > 0) { animFileName += i.ToString(); } animFileName += ".anim"; if (File.Exists(animFileName)) { continue; } generatedClips.Add(CopyAssetFile(animFileName, pathClips.Value[i])); } } for (int i = 0; i < generatedClips.Count; ++i) { EditorUtility.SetDirty(generatedClips[i]); } AssetDatabase.Refresh(); Debug.Log("DissociateAnimationClip Count:" + generatedClips.Count); }