public static void CopyFrom(this NodeBehaviour to, NodeBehaviour from) { if (from == null || to == null || from.bindNodes == null || from.bindNodes.Length == 0) { return; } to.FindBoneNodes(); to.FindSMRendererNodes(); NodeData[] fromBindNodes = from.bindNodes; List <NodeData> toBindNodes = new List <NodeData>(); Transform toTran = to.gameObject.transform; foreach (var node in fromBindNodes) { if (node == null || node.transform == null) { continue; } string tranName = node.transform.name; Transform targetTran = toTran.GetChildByName(tranName); if (targetTran != null) { NodeData data = new NodeData(); data.name = node.name; data.nodeType = NodeType.BindNode; data.transform = targetTran; toBindNodes.Add(data); } } to.bindNodes = toBindNodes.ToArray(); }
public static GameObject CreateSkeleton(AvatarSkeletonCreatorData data) { if (data == null) { Debug.LogError("AvatarCreatorUtil::CreateSkeleton->the data is null"); return(null); } if (data.fbx == null) { Debug.LogError("AvatarCreatorUtil::CreateSkeleton->The fbx is null"); return(null); } PrefabAssetType assetType = UnityEditor.PrefabUtility.GetPrefabAssetType(data.fbx); if (assetType != PrefabAssetType.Model) { Debug.LogError($"AvatarCreatorUtil::CreateSkeleton->The fbx is not a model.type = {assetType}"); return(null); } if (string.IsNullOrEmpty(data.outputFolder)) { Debug.LogError("AvatarCreatorUtil::CreateSkeleton->The outputFolder is empty"); return(null); } string outputDiskFolder = PathUtility.GetDiskPath(data.outputFolder); if (!Directory.Exists(outputDiskFolder)) { Directory.CreateDirectory(outputDiskFolder); } string skeletonPrefabAssetPath = data.GetSkeletonPrefabPath(); GameObject cachedPrefab = AssetDatabase.LoadAssetAtPath <GameObject>(skeletonPrefabAssetPath); NodeBehaviour cachedNodeBehaviour = cachedPrefab?.GetComponent <NodeBehaviour>(); GameObject instanceGameObject = GameObject.Instantiate <GameObject>(data.fbx); NodeBehaviour instanceNodeBehaviour = instanceGameObject.AddComponent <NodeBehaviour>(); instanceNodeBehaviour.FindBoneNodes(); instanceNodeBehaviour.FindSMRendererNodes(); if (cachedNodeBehaviour != null) { instanceNodeBehaviour.CopyFrom(cachedNodeBehaviour); } if (instanceNodeBehaviour.smRendererNodes != null && instanceNodeBehaviour.smRendererNodes.Length > 0) { foreach (var nodeData in instanceNodeBehaviour.smRendererNodes) { SkinnedMeshRenderer smr = nodeData.renderer; if (smr != null) { smr.sharedMaterials = new Material[0]; smr.rootBone = null; smr.sharedMesh = null; smr.bones = new Transform[0]; } } } UnityEditor.PrefabUtility.SaveAsPrefabAsset(instanceGameObject, skeletonPrefabAssetPath); GameObject.DestroyImmediate(instanceGameObject); AssetDatabase.ImportAsset(skeletonPrefabAssetPath); return(AssetDatabase.LoadAssetAtPath <GameObject>(skeletonPrefabAssetPath)); }