界面:
实现功能介绍:
①将Prefab的名称修改成指定的名称(包括子级的名称)
②将新生成的Prefab创建到指定路径下
使用方法:
① AssetDatabase.GetAssetPath(Object assetObject)获取文件路径
②文件获取路径(针对到后缀名)
private static string FilePath()//文件路径获取{string[] guids = Selection.assetGUIDs;if (guids.Length < 0)return null;for (int i = 0; i < guids.Length; i++){assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);}return assetPath;}
③ PrefabUtility.SaveAsPrefabAsset(GameObject instanceRoot,string assetPath)
实例化物体保存为Prefab
④ 实例化物体获取子级 GetChild() 以及相关操作
⑤OnGUI()相关语句
⑥using System.IO;下的Path获取文件名称
代码:
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEditor;using System.IO;public class PrefabModifyAndMove : EditorWindow{static int currentMode = 0;enum Mode{CopyAndMove,ModifyAndMove}//声明数据并预填写static string inputParentPrefab = null;static string inputCloth = "Cloth";static string inputHair = "Hair";static string inputBody = "Body";static string inputFace = "Face";string outputMessage = "已成功复制并修改数据,点击确定自动关闭窗口";static string destPath = "Assets/Game/";private static string assetPath;//文件源路径[MenuItem("Assets/修改角色Prefab及其子对象名称并移动到指定文件夹", false, 1)]private static void ShowWindow(){FilePath();string assetPath = AssetDatabase.GetAssetPath(Selection.activeObject);string Filename = Path.GetFileNameWithoutExtension(assetPath);inputParentPrefab = Filename;if (assetPath.EndsWith(".prefab")){//----------编辑器窗口区---------PrefabModifyAndMove window = GetWindow<PrefabModifyAndMove>("自定义窗口");//窗口大小,不可修改宽高Vector2 windowSize = new Vector2(300, 180);window.maxSize = windowSize;window.minSize = windowSize;//显示窗口window.Show();}else { Debug.Log("您未选择相应的Prefab资源"); }}//------------------------窗口编写-------------------------private void OnGUI() //纯GUI逻辑{GUILayout.BeginArea(new Rect(0, 0, 300, 300));//区域开始//GUIStyleGUIStyle centerStyle = new GUIStyle();centerStyle.fontSize = 16;//字体大小centerStyle.normal.textColor = Color.white;//颜色centerStyle.alignment = TextAnchor.MiddleCenter;//显示居中GUILayout.Label("无需修改数据请勿填数据", centerStyle);//局部字体样式int size = GUI.skin.label.fontSize;GUI.skin.label.fontSize = 18;GUI.skin.label.fontSize = size;//全局字体样式GUI.skin.label.fontSize = 14;//填写参数列表GUILayout.Space(2);EditorGUILayout.BeginHorizontal();//垂直布局开始GUILayout.Label("预制体名称:",GUILayout.Width(80));inputParentPrefab = GUILayout.TextField(inputParentPrefab,GUILayout.Width(200));EditorGUILayout.EndHorizontal();//垂直布局结束GUILayout.Space(2);EditorGUILayout.BeginHorizontal();GUILayout.Label("Cloth:", GUILayout.Width(80));inputCloth = GUILayout.TextField(inputCloth, GUILayout.Width(200));EditorGUILayout.EndHorizontal();GUILayout.Space(2);EditorGUILayout.BeginHorizontal();GUILayout.Label("Hair:", GUILayout.Width(80));inputHair = GUILayout.TextField(inputHair, GUILayout.Width(200));EditorGUILayout.EndHorizontal();GUILayout.Space(2);EditorGUILayout.BeginHorizontal();GUILayout.Label("Body:", GUILayout.Width(80));inputBody = GUILayout.TextField(inputBody, GUILayout.Width(200));EditorGUILayout.EndHorizontal();GUILayout.Space(2);EditorGUILayout.BeginHorizontal();GUILayout.Label("Face:", GUILayout.Width(80));inputFace = GUILayout.TextField(inputFace, GUILayout.Width(200));EditorGUILayout.EndHorizontal();GUILayout.Space(2);//指定新路径EditorGUILayout.BeginHorizontal();GUILayout.Label("指定路径:", GUILayout.Width(70));destPath = GUILayout.TextField(destPath, GUILayout.Width(210));EditorGUILayout.EndHorizontal();//输出确定信息EditorGUILayout.BeginHorizontal();if(GUILayout.Button("保持原名称", GUILayout.Width(145))) //纯复制 + 移动{if (EditorUtility.DisplayDialog("结果通知:", "未改变任何状态,点击确定关闭窗口", "确定")){currentMode = 0;ChangeData();this.Close();}}if (GUILayout.Button(new GUIContent("确定"),GUILayout.Width(145))) //改名 + 移动{if (EditorUtility.DisplayDialog("结果通知:", outputMessage, "确定")){currentMode = 1;ChangeData();this.Close();}}EditorGUILayout.EndHorizontal();GUILayout.EndArea();//区域结束}private static string FilePath()//文件路径获取{string[] guids = Selection.assetGUIDs;if (guids.Length < 0)return null;for (int i = 0; i < guids.Length; i++){assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);}Debug.Log(assetPath);return assetPath;}private static void ChangeData(){//--------------正式开始对数据进行修改-----------------GameObject o = PrefabUtility.InstantiatePrefab(Selection.activeObject as GameObject) as GameObject;if (currentMode.Equals((int)Mode.CopyAndMove))//纯复制逻辑{o.name = o.name;}else if(currentMode.Equals((int)Mode.ModifyAndMove))//修改 + 移动逻辑{//修改父集信息--------需要自定义或者指定路径if (inputParentPrefab != null && JudgeSpace(inputParentPrefab)){o.name = inputParentPrefab;}else { o.name = o.name; }//修改子集信息-------自定义或者指定路径for (int i = 0; i < o.transform.childCount; i++){if (o.transform.GetChild(i).GetComponent<SkinnedMeshRenderer>() != null){if (o.transform.GetChild(i).GetComponent<SkinnedMeshRenderer>().sharedMaterial.shader.name == "Character/Character"){if (inputCloth != null && JudgeSpace(inputCloth)){o.transform.GetChild(i).name = inputCloth;}else { o.transform.GetChild(i).name = "Cloth"; }}else if (o.transform.GetChild(i).GetComponent<SkinnedMeshRenderer>().sharedMaterial.shader.name == "Character/Character_Hair"){if (inputHair != null && JudgeSpace(inputHair)){o.transform.GetChild(i).name = inputHair;}else { o.transform.GetChild(i).name = "Hair"; }}else if (o.transform.GetChild(i).GetComponent<SkinnedMeshRenderer>().sharedMaterial.shader.name == "Character/Actor_Skin"){if (inputBody != null && JudgeSpace(inputBody)){o.transform.GetChild(i).name = inputBody;}else { o.transform.GetChild(i).name = "Body"; }}else if (o.transform.GetChild(i).GetComponent<SkinnedMeshRenderer>().sharedMaterial.shader.name == "Character/Character_Skin"){if (inputFace != null && JudgeSpace(inputFace)){o.transform.GetChild(i).name = inputFace;}else { o.transform.GetChild(i).name = "Face"; }}else { Debug.LogFormat("{0}没有SkinnedMeshRenderer组件", o.transform.GetChild(i)); }}}}//实例化物体保存为PrefabPrefabUtility.SaveAsPrefabAsset(o, (destPath + o.name + ".Prefab"));//实例化后场景留存物体->销毁//初始化静态变量inputParentPrefab = null;inputCloth = "Cloth";inputHair = "Hair";inputBody = "Body";inputFace = "Face";Object.DestroyImmediate(o);}private static bool JudgeSpace(string str)//判断空格{string strc = str;if (strc.Trim() == ""){return false;}else return true;}}
