FbxImportSettingTool.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.IO;
  6. namespace ChivaXR
  7. {
  8. public class FbxImportSettingTool : EditorWindow
  9. {
  10. protected static FbxImportSettingTool s_instance = null;
  11. internal static FbxImportSettingTool instance
  12. {
  13. get
  14. {
  15. if (s_instance == null)
  16. {
  17. s_instance = GetWindow<FbxImportSettingTool>();
  18. }
  19. return s_instance;
  20. }
  21. }
  22. #region 匹配贴图工具
  23. /// <summary>
  24. /// 在模型和贴图的父级调用,会根据材质球名称和贴图名称检索自动添加贴图
  25. /// </summary>
  26. [MenuItem("ChivaXR/Tool/自动匹配材质贴图文件")]
  27. static void UpdateFbxMaterials()
  28. {
  29. instance.LoadInit();
  30. Object[] objs = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
  31. List<string> pngandtga = new List<string>();
  32. for (int i = 0; i < objs.Length; i++)
  33. {
  34. Object obj = objs[i];
  35. string url = AssetDatabase.GetAssetPath(obj);
  36. if (string.IsNullOrEmpty(url))
  37. continue;
  38. string ext = Path.GetExtension(url);
  39. if (ext == ".tga" || ext == ".TGA" || ext == ".png" || ext == ".PNG")
  40. {
  41. pngandtga.Add(url);
  42. continue;
  43. }
  44. if (ext != ".fbx" && ext != ".FBX")
  45. continue;
  46. //如果是fbx加载材质球
  47. instance.LoadMaterials(url);
  48. }
  49. for (int i = 0; i < pngandtga.Count; i++)
  50. {
  51. string url = pngandtga[i];
  52. instance.LoadTexture(url);
  53. }
  54. instance.SettingMaterials();
  55. AssetDatabase.Refresh();
  56. }
  57. private List<Material> mListMaterial = new List<Material>();
  58. private List<string> mListMaterialName = new List<string>();
  59. private Dictionary<string, MaterialsDate> mDicTexture = new Dictionary<string, MaterialsDate>();
  60. /// <summary>
  61. /// 颜色贴图 -贴图的命名规则(材质_目标贴图后缀)
  62. /// </summary>
  63. private string[] AlbedoTexture = new string[] { "_AlbedoTransparency", "_Albedo" };
  64. /// <summary>
  65. /// 高光贴图
  66. /// </summary>
  67. private string[] MetallicTexture = new string[] { "_MetallicSmoothness", "_Metallic" };
  68. /// <summary>
  69. /// 法线贴图
  70. /// </summary>
  71. private string[] NormalTexture = new string[] { "_Normal" };
  72. /// <summary>
  73. /// Ao贴图
  74. /// </summary>
  75. private string[] OcclusionTexture = new string[] { "_Ao", "_AO", "_Occlusion", "_AmbientOcclusion" };
  76. /// <summary>
  77. /// 初始化
  78. /// </summary>
  79. private void LoadInit()
  80. {
  81. mListMaterial.Clear();
  82. mListMaterialName.Clear();
  83. mDicTexture.Clear();
  84. }
  85. /// <summary>
  86. /// 加载所有PBX文件的Material(材质球)
  87. /// </summary>
  88. /// <param name="url"></param>
  89. private void LoadMaterials(string url)
  90. {
  91. ModelImporter modelImporter = ModelImporter.GetAtPath(url) as ModelImporter;
  92. modelImporter.materialImportMode = ModelImporterMaterialImportMode.ImportViaMaterialDescription;
  93. modelImporter.materialName = ModelImporterMaterialName.BasedOnMaterialName;
  94. modelImporter.materialSearch = ModelImporterMaterialSearch.Everywhere;
  95. AssetDatabase.ImportAsset(url);
  96. Object obj = AssetDatabase.LoadAssetAtPath<Object>(url);
  97. GameObject go = obj as GameObject;
  98. Renderer[] skins = go.GetComponentsInChildren<MeshRenderer>(true);
  99. if (skins.Length == 0)
  100. return;
  101. for (int i = 0; i < skins.Length; i++)
  102. {
  103. Renderer renderer = skins[i];
  104. if (renderer.sharedMaterials != null && renderer.sharedMaterials.Length > 0)
  105. {
  106. for (int j = 0; j < renderer.sharedMaterials.Length; j++)
  107. {
  108. Material mat = renderer.sharedMaterials[j];
  109. if (mat != null)
  110. {
  111. mListMaterial.Add(mat);
  112. mat.shader = Shader.Find("Standard");
  113. if (!mListMaterialName.Contains(mat.name))
  114. {
  115. mListMaterialName.Add(mat.name);
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. /// <summary>
  123. /// 加载文件中的所有Texture(贴图)
  124. /// </summary>
  125. /// <param name="url"></param>
  126. private void LoadTexture(string url)
  127. {
  128. Texture2D TempTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(url);
  129. string TempTextureName = TempTexture.name;
  130. TextureImporter texture = AssetImporter.GetAtPath(url) as TextureImporter;
  131. for (int i = 0; i < AlbedoTexture.Length; i++)
  132. {
  133. if (DateTexture(AlbedoTexture[i], TextureType.Albedo, TempTexture))
  134. return;
  135. }
  136. for (int i = 0; i < MetallicTexture.Length; i++)
  137. {
  138. if (DateTexture(MetallicTexture[i], TextureType.Metallic, TempTexture))
  139. return;
  140. }
  141. for (int i = 0; i < OcclusionTexture.Length; i++)
  142. {
  143. if (DateTexture(OcclusionTexture[i], TextureType.Occlusion, TempTexture))
  144. return;
  145. }
  146. for (int i = 0; i < NormalTexture.Length; i++)
  147. {
  148. if (DateTexture(NormalTexture[i], TextureType.Normal, TempTexture))
  149. {
  150. TextureImporter tempTexture = AssetImporter.GetAtPath(url) as TextureImporter;
  151. tempTexture.textureType = TextureImporterType.NormalMap;
  152. return;
  153. }
  154. }
  155. }
  156. private bool DateTexture(string varTextureName, TextureType varTextureType, Texture Texture)
  157. {
  158. string TempTextureName = Texture.name;
  159. string key = GetMaterialsBuyTexture(TempTextureName, varTextureName);
  160. if (mListMaterialName.Contains(key))
  161. {
  162. MaterialsDate Date = new MaterialsDate();
  163. if (mDicTexture.ContainsKey(key))
  164. {
  165. Date = mDicTexture[key];
  166. }
  167. switch (varTextureType)
  168. {
  169. case TextureType.Albedo:
  170. Date.Albedo = Texture;
  171. break;
  172. case TextureType.Metallic:
  173. Date.Metallic = Texture;
  174. break;
  175. case TextureType.Normal:
  176. Date.Normal = Texture;
  177. break;
  178. case TextureType.Occlusion:
  179. Date.Occlusion = Texture;
  180. break;
  181. default:
  182. break;
  183. }
  184. if (mDicTexture.ContainsKey(key))
  185. {
  186. mDicTexture[key] = Date;
  187. }
  188. else
  189. {
  190. mDicTexture.Add(key, Date);
  191. }
  192. return true;
  193. }
  194. else
  195. {
  196. return false;
  197. }
  198. }
  199. /// <summary>
  200. /// 移除指定后缀获取对应材质的名称
  201. /// </summary>
  202. private string GetMaterialsBuyTexture(string TextureName, string varTextureName)
  203. {
  204. string key = TextureName;
  205. if (TextureName.Contains(varTextureName))
  206. {
  207. int index = TextureName.LastIndexOf(varTextureName);
  208. key = TextureName.Substring(0, index);
  209. }
  210. return key;
  211. }
  212. /// <summary>
  213. /// 给材质贴上指定的贴图
  214. /// </summary>
  215. private void SettingMaterials()
  216. {
  217. for (int i = 0; i < mListMaterial.Count; i++)
  218. {
  219. string MaterialName = mListMaterial[i].name;
  220. Material mat = mListMaterial[i];
  221. if (mat.GetTexture("_MainTex") != null)
  222. {
  223. //如果有颜色贴图,则将贴图颜色设为白色
  224. mat.SetColor("_Color", Color.white);
  225. }
  226. if (mDicTexture.ContainsKey(MaterialName))
  227. {
  228. var date = mDicTexture[MaterialName];
  229. if (date.Albedo != null)
  230. mat.SetTexture("_MainTex", date.Albedo);
  231. if (date.Metallic != null)
  232. mat.SetTexture("_MetallicGlossMap", date.Metallic);
  233. if (date.Normal != null)
  234. mat.SetTexture("_BumpMap", date.Normal);
  235. if (date.Occlusion != null)
  236. mat.SetTexture("_OcclusionMap", date.Occlusion);
  237. }
  238. else
  239. {
  240. Debug.LogError("Materials :" + MaterialName + "Texture Not Existent");
  241. }
  242. }
  243. }
  244. public struct MaterialsDate
  245. {
  246. public Texture Albedo;
  247. public Texture Metallic;
  248. public Texture Normal;
  249. public Texture Occlusion;
  250. }
  251. public enum TextureType
  252. {
  253. Albedo,
  254. Metallic,
  255. Normal,
  256. Occlusion,
  257. }
  258. #endregion
  259. #region UnityAnimation 动画片段切分工具
  260. /// <summary>
  261. /// FBX动画自动分段工具
  262. /// 使用:需要将配置表和模型放入同一个文件夹
  263. /// 配置表与模型同名,格式为(.txt):
  264. /// 配置表内容 片段名称(nullOrEmpty会自动填充为step+"n"),0-100
  265. /// ,101-200
  266. /// ,201-300类推
  267. /// </summary>
  268. [MenuItem("Assets/Chiva/动画片段切分")]
  269. static void AnimationClipCreator()
  270. {
  271. try
  272. {
  273. Object obj = Selection.activeObject;
  274. string AssetPath = AssetDatabase.GetAssetPath(obj);
  275. List<FbxModel> fbxModelList = new List<FbxModel>();
  276. //替换后缀
  277. string text = Path.ChangeExtension(AssetPath, ".txt");
  278. StreamReader file = new StreamReader(text);
  279. string clipInfo = file.ReadToEnd();
  280. file.Close();
  281. string[] clipInfosArr = clipInfo.Split('\n');
  282. Debug.Log("length:" + clipInfosArr.Length);
  283. for (int i = 0; i < clipInfosArr.Length; i++)
  284. {
  285. string[] infos = clipInfosArr[i].Split(',');
  286. string[] frameInfo = infos[1].Split('-');
  287. FbxModel fbxModel = new FbxModel();
  288. fbxModel.firstFrame = int.Parse(frameInfo[0]);
  289. fbxModel.lastFrame = int.Parse(frameInfo[1]);
  290. if (string.IsNullOrEmpty(infos[0]))
  291. {
  292. fbxModel.name = "step" + (i + 1);
  293. }
  294. else
  295. {
  296. fbxModel.name = infos[0];
  297. }
  298. //bool isLoop = info[3] == "0" ? true : false;
  299. //bool isLoop = bool.Parse(info[3]);
  300. //Debug.Log("是否循环" + isLoop);
  301. //fbxModel.loopTime = isLoop;
  302. //fbxModel.loopPose = isLoop;
  303. fbxModelList.Add(fbxModel);
  304. }
  305. //模型导入器
  306. ModelImporter modelImporter = ModelImporter.GetAtPath(AssetPath) as ModelImporter;
  307. ArrayList clipsList = new ArrayList();
  308. for (int i = 0; i < fbxModelList.Count; i++)
  309. {
  310. //动画片段
  311. ModelImporterClipAnimation clipAnimation = new ModelImporterClipAnimation();
  312. clipAnimation.name = fbxModelList[i].name;
  313. clipAnimation.firstFrame = fbxModelList[i].firstFrame;
  314. clipAnimation.lastFrame = fbxModelList[i].lastFrame;
  315. clipAnimation.loopTime = fbxModelList[i].loopTime;
  316. clipAnimation.loopPose = fbxModelList[i].loopPose;
  317. clipsList.Add(clipAnimation);
  318. }
  319. modelImporter.clipAnimations = (ModelImporterClipAnimation[])clipsList.ToArray(typeof(ModelImporterClipAnimation));
  320. AssetDatabase.ImportAsset(AssetPath);
  321. AssetDatabase.Refresh();
  322. }
  323. catch (System.Exception e)
  324. {
  325. EditorUtility.DisplayDialog("导入错误", e.ToString(), "关闭");
  326. }
  327. }
  328. #endregion
  329. }
  330. public class FbxModel
  331. {
  332. public string name;
  333. public int firstFrame;
  334. public int lastFrame;
  335. public bool loopTime;
  336. public bool loopPose;
  337. }
  338. }