//クローンしたプレハブやScriptableObject内にテンプレートアセットへのリンクがあったら、クローンアセットのものに変える void ReplaceCloneInSelf(Dictionary <Object, Object> cloneAssetPair) { //Valuesはクローンしたアセット foreach (Object clone in cloneAssetPair.Values) { bool isReplaced = false; if (WrapperUnityVersion.CheckPrefabAsset(clone)) { //プレハブの場合 GameObject go = clone as GameObject; if (go == null) { Debug.Log(clone.name); continue; } foreach (Component component in go.GetComponentsInChildren <Component>(true)) { isReplaced |= UtageEditorToolKit.ReplaceSerializedProperties(new SerializedObject(component), cloneAssetPair); } } else if (UtageEditorToolKit.IsScriptableObject(clone)) { //ScriptableObjectの場合 isReplaced |= UtageEditorToolKit.ReplaceSerializedProperties(new SerializedObject(clone), cloneAssetPair); } if (isReplaced) { EditorUtility.SetDirty(clone); } } }
//プロジェクト内の全てのコンポーネント(プレハブとScriptableObject)のうち、指定のアセットを参照しているものを取得 internal static List <Object> FindReferencesAssetsInProject(string dir, Object srcAsset) { List <Object> references = new List <Object>(); List <string> pathList = UtageEditorToolKit.GetAllAssetPathList(dir); foreach (string assetpath in pathList) { if (Path.GetExtension(assetpath) == ".unity") { continue; } foreach (Object obj in AssetDatabase.LoadAllAssetsAtPath(assetpath)) { if (obj == null) { continue; } if (WrapperUnityVersion.CheckPrefabAsset(obj)) { //プレハブの場合 GameObject go = obj as GameObject; if (go == null) { continue; } foreach (Component component in go.GetComponentsInChildren <Component>(true)) { if (ContainsReferenceObject(new SerializedObject(component), srcAsset)) { references.Add(component); } } } else if (UtageEditorToolKit.IsScriptableObject(obj)) { //ScriptableObjectの場合 if (ContainsReferenceObject(new SerializedObject(obj), srcAsset)) { references.Add(obj); } } } } return(references); }
//シーン内で、クローンしたアセットに置き換えるためのDictionaryを作成 //元のアセットをキーとし、クローンしたアセットをValueとする Dictionary <Object, Object> FindCloneAssets() { Dictionary <Object, Object> cloneAssetPair = new Dictionary <Object, Object>(); //クローンしたアセットにパッキングタグなどの必要な処置をして //テンプレートのアセットとのペアでリスト化する List <string> pathList = UtageEditorToolKit.GetAllAssetPathList(newProjectDir); foreach (string assetpath in pathList) { if (Path.GetExtension(assetpath) == ".unity") { continue; } //テンプレート(クローン元)のアセット string templatePath = assetpath.Replace(GetProjectRelativeDir() + "/", TemplateAssetsDir + "/"); //クローンしたアセット foreach (Object clone in AssetDatabase.LoadAllAssetsAtPath(assetpath)) { if (clone == null) { continue; } if (WrapperUnityVersion.CheckPrefabAsset(clone)) { //プレハブの場合 Object prefab = WrapperUnityVersion.FindPrefabAssetRoot(clone as GameObject); if (prefab == null) { Debug.LogError(templatePath + " not found"); } else { Object template = AssetDatabase.LoadAssetAtPath(templatePath, prefab.GetType()); if (template != null) { if (cloneAssetPair.ContainsKey(template)) { Debug.LogError(templatePath + " is already contains"); } else { cloneAssetPair.Add(template, prefab); } } else { Debug.LogError(templatePath + " not found"); } } break; } else { Sprite sprite = clone as Sprite; if (sprite != null) { //スプライトの場合のみ //インポーターのパッキングタグを変える TextureImporter importer = AssetImporter.GetAtPath(assetpath) as TextureImporter; if (importer != null) { importer.spritePackingTag = newProjectName + "_UI"; AssetDatabase.ImportAsset(assetpath); EditorUtility.SetDirty(importer); } } Object template = AssetDatabase.LoadAssetAtPath(templatePath, clone.GetType()); if (template != null) { if (cloneAssetPair.ContainsKey(template)) { Debug.LogError(templatePath + " is already contains"); } else { cloneAssetPair.Add(template, clone); } } } } } return(cloneAssetPair); }
//アセットの再設定 void RebuildAssetsSub() { cloneAssetPair = new Dictionary <Object, Object>(); //クローンしたアセットにパッキングタグなどの必要な処置をして //テンプレートのアセットとのペアでリスト化する List <string> pathList = UtageEditorToolKit.GetAllAssetPathList(newPath); foreach (string assetpath in pathList) { if (Path.GetExtension(assetpath) == ".unity") { continue; } //クローン元のアセット string oldPath = FilePathUtil.Format(assetpath).Replace(newPath + "/", srcPath + "/"); Object mainAsset = AssetDatabase.LoadMainAssetAtPath(assetpath); if (WrapperUnityVersion.CheckPrefabAsset(mainAsset)) { Object oldAsset = AssetDatabase.LoadMainAssetAtPath(oldPath); cloneAssetPair.Add(oldAsset, mainAsset); } else { foreach (Object clone in AssetDatabase.LoadAllAssetsAtPath(assetpath)) { Object oldAsset = AssetDatabase.LoadAssetAtPath(oldPath, clone.GetType()); if (oldAsset != null) { if (cloneAssetPair.ContainsKey(oldAsset)) { Debug.LogWarning(oldPath + " is already contains"); } else { cloneAssetPair.Add(oldAsset, clone); } } } } } float progress = 0.1f; EditorUtility.DisplayProgressBar("DeepDuplicate", "", progress); float delataProgress = (1 - progress) / cloneAssetPair.Values.Count; //クローンしたプレハブやScriptableObject内にテンプレートアセットへのリンクがあったら、クローンアセットのものに変える foreach (Object obj in cloneAssetPair.Values) { EditorUtility.DisplayProgressBar("DeepDuplicate ", obj.name, progress); bool isReplaced = false; if (WrapperUnityVersion.CheckPrefabAsset(obj)) { UtageEditorToolKit.ReplaceSerializedPropertiesAllComponents(obj as GameObject, cloneAssetPair); } else { UtageEditorToolKit.ReplaceSerializedProperties(new SerializedObject(obj), cloneAssetPair); } if (isReplaced) { EditorUtility.SetDirty(obj); } progress += delataProgress; } }