using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using FluffyUnderware.DevTools.Extensions;
public class ModelSet : EditorWindow
{
private Avatar _srcAvatarPrefab;
//%:ctrl,#:shift,&:alt
[MenuItem("Tools/ArtTools/动画设置 %#&X")]
static void Init()
{
EditorWindow.GetWindow(typeof(ModelSet));
}
void OnGUI()
{
_srcAvatarPrefab = EditorGUILayout.ObjectField(_srcAvatarPrefab,typeof(Avatar)) as Avatar;
if (GUILayout.Button("设置AvatarMask"))
{
if (_srcAvatarPrefab != null)
{
ModelImporterSet();
}
}
EditorGUILayout.EndScrollView();
}
void ModelImporterSet()
{
if (Selection.objects == null || Selection.objects.Length == 0)
{
Debug.LogWarning("You must select an object first");
return;
}
bool isLoop = EditorUtility.DisplayDialog("Loop Time","选择是否循环", "是","否"); //---弹窗设置Loop Time
foreach (Object obj in Selection.objects)
{
string assetPath = AssetDatabase.GetAssetPath(obj);
ModelImporter modelImp = AssetImporter.GetAtPath(assetPath) as ModelImporter;
if (modelImp != null)
{
RigSet(modelImp,_srcAvatarPrefab);
AnimSetup(modelImp, obj.name, isLoop);
MatSet(modelImp);
modelImp.SaveAndReimport();
ExtractAnim(assetPath);
}
}
AssetDatabase.SaveAssets();
bool isCloseWindow = EditorUtility.DisplayDialog("完成", "设置完成", "关闭窗口","不关闭窗口");
if (isCloseWindow)
{
EditorWindow.GetWindow(typeof(ModelSet)).Close();
}
}
// 提取动画
void ExtractAnim(string assetPath)
{
var assets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
foreach (Object asset in assets)
{
AnimationClip animationClip = asset as AnimationClip;
if (animationClip == null)
continue;
if (animationClip.name.StartsWith("__preview__"))
continue;
string dstPath = Path.GetDirectoryName(assetPath) + "/" + animationClip.name + ".anim";
AssetDatabase.CreateAsset(AnimationClip.Instantiate(asset), dstPath);
}
}
//---Animation设置
void AnimSetup(ModelImporter modelImp, string name, bool isLoop)
{
modelImp.importAnimation = true; //---Import Animation 勾选
modelImp.motionNodeName = "<Root Transform>";
int count = modelImp.defaultClipAnimations.Length; //---Clips动画数量
ModelImporterClipAnimation[] newClips = new ModelImporterClipAnimation[count]; //---创建动画容器数组
for (int i = 0; i < count; i++)
{
ModelImporterClipAnimation oldClip = modelImp.defaultClipAnimations[i];
ModelImporterClipAnimation newClip = new ModelImporterClipAnimation
{
name = name,
takeName = name,
curves = oldClip.curves,
firstFrame = oldClip.firstFrame,
lastFrame = oldClip.lastFrame,
lockRootRotation = false,
lockRootHeightY = false,
lockRootPositionXZ = false,
loopTime = isLoop,
maskType = ClipAnimationMaskType.None,
};
newClips[i] = newClip;
}
modelImp.clipAnimations = newClips;
}
//--Rig设置
void RigSet(ModelImporter modelImp,Avatar avatar)
{
modelImp.animationType = ModelImporterAnimationType.Generic;
modelImp.avatarSetup = ModelImporterAvatarSetup.CopyFromOther;
modelImp.sourceAvatar = avatar;
modelImp.skinWeights = ModelImporterSkinWeights.Standard;
}
//--Materials设置
void MatSet(ModelImporter modelImp)
{
modelImp.materialImportMode = ModelImporterMaterialImportMode.None;
}
}
|