当前位置:   article > 正文

Unity Fbx转Prefab工具_unity的fbx转化为game

unity的fbx转化为game

1.功能描述

        a.可将fbx直接导出prefab。

        b.同时为prefab预先按需挂载脚本文件。

        c.同时为挂载的脚本设置参数,绑定资源等。

        d.批量化处理,递归遍历算法,高效可扩展。

 2.实现方式

 a.Editor类

  1. using Lean.Touch;
  2. using Main;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Reflection;
  8. using UnityEditor;
  9. using UnityEngine;
  10. public enum SystemBody
  11. {
  12. [Description("Bones")]
  13. guge=1,
  14. [Description("Muscle")]
  15. jirou,
  16. [Description("Vein")]
  17. jingmai,
  18. [Description("Aetery")]
  19. dongmai,
  20. [Description("Bones")]
  21. neifenmi=5,
  22. [Description("Bones")]
  23. shenjing,
  24. [Description("Skin")]
  25. pifu,
  26. [Description("Lymph")]
  27. lingba,
  28. [Description("GenitourinrySelectable")]
  29. miniaoshengzhi,
  30. [Description("Respiratory")]
  31. xiaohua=10,
  32. [Description("Joint")]
  33. guanjie,
  34. [Description("Respiratory")]
  35. huxi,
  36. }
  37. public static class EnumExpand
  38. {
  39. public static T ToEnum<T>(this string str)
  40. {
  41. return (T)Enum.Parse(typeof(T), str);
  42. }
  43. }
  44. public class Fbx2Prefab : Editor
  45. {
  46. [MenuItem("Assets/Fbx2Prefab/Create GuGe Prefab")]
  47. public static void CreateGuGePre()
  48. {
  49. GameObject obj = Selection.gameObjects[0];
  50. GameObject pre = MonoBehaviour.Instantiate(obj);
  51. List<GameObject> prelist = new List<GameObject>();
  52. for (int i = 0; i < pre.transform.childCount; i++)
  53. {
  54. prelist.Add(pre.transform.GetChild(i).gameObject);
  55. }
  56. GameObjectTree tree = MonoBehaviour.FindObjectOfType<GameObjectTree>();
  57. tree.scriptObjs.Clear();
  58. tree.rootTransform = pre.transform;
  59. tree.scriptObjs.Add(MonoScript.FromScriptableObject(CreateInstance(typeof(BonesSelectable))));
  60. tree.scriptObjs.Add(MonoScript.FromScriptableObject(CreateInstance(typeof(LeanSelectableByFinger))));
  61. tree.AddMonoScript();
  62. var mts = Resources.FindObjectsOfTypeAll<Material>();//绑定材质球
  63. tree.SetCommponentValue<BonesSelectable>(mts.ToList().Find(p=>p.name== SystemBody.guge.ToString()));
  64. foreach (var item in prelist)
  65. {
  66. PrefabUtility.SaveAsPrefabAsset(item, $"Assets/Prefab/{item.name}.prefab");
  67. }
  68. DestroyImmediate(pre);
  69. }
  70. [MenuItem("Assets/Fbx2Prefab/Create Jirou Prefab")]
  71. public static void CreateJirouPre()
  72. {
  73. SystemBody body = SystemBody.jirou;
  74. CreateNormalPre(body);
  75. }
  76. [MenuItem("Assets/Fbx2Prefab/Create Normal Prefab")]
  77. public static void CreateNormalPre()
  78. {
  79. SystemBody body = SystemBody.jirou;
  80. string n = Selection.gameObjects[0]?.name;
  81. if (string.IsNullOrEmpty(n))
  82. {
  83. Debug.LogError("未选中");
  84. }
  85. else
  86. {
  87. body = n.ToLower().ToEnum<SystemBody>();
  88. }
  89. CreateNormalPre(body);
  90. }
  91. public static void CreateNormalPre(SystemBody body)
  92. {
  93. GameObject obj = Selection.gameObjects[0];
  94. GameObject pre = MonoBehaviour.Instantiate(obj);
  95. List<GameObject> prelist = new List<GameObject>();
  96. for (int i = 0; i < pre.transform.childCount; i++)
  97. {
  98. prelist.Add(pre.transform.GetChild(i).gameObject);
  99. }
  100. GameObjectTree tree = MonoBehaviour.FindObjectOfType<GameObjectTree>();
  101. tree.scriptObjs.Clear();
  102. tree.rootTransform = pre.transform;
  103. Type t = Assembly.Load("Unity.ModelView").GetType($"Main.{body.GetDescName()}Selectable");
  104. tree.scriptObjs.Add(MonoScript.FromScriptableObject(Cr
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/81995
推荐阅读
相关标签
  

闽ICP备14008679号