1.功能描述
a.可将fbx直接导出prefab。
b.同时为prefab预先按需挂载脚本文件。
c.同时为挂载的脚本设置参数,绑定资源等。
d.批量化处理,递归遍历算法,高效可扩展。
2.实现方式文章来源:https://www.toymoban.com/news/detail-593752.html
a.Editor类文章来源地址https://www.toymoban.com/news/detail-593752.html
using Lean.Touch;
using Main;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public enum SystemBody
{
[Description("Bones")]
guge=1,
[Description("Muscle")]
jirou,
[Description("Vein")]
jingmai,
[Description("Aetery")]
dongmai,
[Description("Bones")]
neifenmi=5,
[Description("Bones")]
shenjing,
[Description("Skin")]
pifu,
[Description("Lymph")]
lingba,
[Description("GenitourinrySelectable")]
miniaoshengzhi,
[Description("Respiratory")]
xiaohua=10,
[Description("Joint")]
guanjie,
[Description("Respiratory")]
huxi,
}
public static class EnumExpand
{
public static T ToEnum<T>(this string str)
{
return (T)Enum.Parse(typeof(T), str);
}
}
public class Fbx2Prefab : Editor
{
[MenuItem("Assets/Fbx2Prefab/Create GuGe Prefab")]
public static void CreateGuGePre()
{
GameObject obj = Selection.gameObjects[0];
GameObject pre = MonoBehaviour.Instantiate(obj);
List<GameObject> prelist = new List<GameObject>();
for (int i = 0; i < pre.transform.childCount; i++)
{
prelist.Add(pre.transform.GetChild(i).gameObject);
}
GameObjectTree tree = MonoBehaviour.FindObjectOfType<GameObjectTree>();
tree.scriptObjs.Clear();
tree.rootTransform = pre.transform;
tree.scriptObjs.Add(MonoScript.FromScriptableObject(CreateInstance(typeof(BonesSelectable))));
tree.scriptObjs.Add(MonoScript.FromScriptableObject(CreateInstance(typeof(LeanSelectableByFinger))));
tree.AddMonoScript();
var mts = Resources.FindObjectsOfTypeAll<Material>();//绑定材质球
tree.SetCommponentValue<BonesSelectable>(mts.ToList().Find(p=>p.name== SystemBody.guge.ToString()));
foreach (var item in prelist)
{
PrefabUtility.SaveAsPrefabAsset(item, $"Assets/Prefab/{item.name}.prefab");
}
DestroyImmediate(pre);
}
[MenuItem("Assets/Fbx2Prefab/Create Jirou Prefab")]
public static void CreateJirouPre()
{
SystemBody body = SystemBody.jirou;
CreateNormalPre(body);
}
[MenuItem("Assets/Fbx2Prefab/Create Normal Prefab")]
public static void CreateNormalPre()
{
SystemBody body = SystemBody.jirou;
string n = Selection.gameObjects[0]?.name;
if (string.IsNullOrEmpty(n))
{
Debug.LogError("未选中");
}
else
{
body = n.ToLower().ToEnum<SystemBody>();
}
CreateNormalPre(body);
}
public static void CreateNormalPre(SystemBody body)
{
GameObject obj = Selection.gameObjects[0];
GameObject pre = MonoBehaviour.Instantiate(obj);
List<GameObject> prelist = new List<GameObject>();
for (int i = 0; i < pre.transform.childCount; i++)
{
prelist.Add(pre.transform.GetChild(i).gameObject);
}
GameObjectTree tree = MonoBehaviour.FindObjectOfType<GameObjectTree>();
tree.scriptObjs.Clear();
tree.rootTransform = pre.transform;
Type t = Assembly.Load("Unity.ModelView").GetType($"Main.{body.GetDescName()}Selectable");
tree.scriptObjs.Add(MonoScript.FromScriptableObject(Cr
到了这里,关于Unity Fbx转Prefab工具的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!