179 lines
6.3 KiB
C#
179 lines
6.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
|
|
public class AnimOverrideHandle
|
|
{
|
|
[MenuItem("Assets/*Custom/动画/自动设置展示动画机")]
|
|
private static void _AutoSetDisplayAnims()
|
|
{
|
|
var asset = Selection.activeObject as AnimatorOverrideController;
|
|
if (asset == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// 先进行checkout
|
|
var path = AssetDatabase.GetAssetPath(asset);
|
|
// VersionControlSystem.Checkout(path);
|
|
|
|
// 再进行设置
|
|
// 找到工程内所有clips
|
|
// display_ovrride_g01 找出 g01
|
|
var assetName = asset.name;
|
|
var splitNames = assetName.Split('_');
|
|
var wName = splitNames[2];
|
|
// 转大写
|
|
var wNameUpper = wName.ToUpper();
|
|
// 找到Assets/Art/Animations/Show 下所有文件夹
|
|
var parentDirPath = Application.dataPath + "/Art/Animations/Show";
|
|
var dirs = System.IO.Directory.GetDirectories(parentDirPath);
|
|
// 找到以wName开头的文件夹
|
|
var targetDir = "";
|
|
for (var i = 0; i < dirs.Length; ++i)
|
|
{
|
|
var dir = dirs[i];
|
|
var dirName = Path.GetFileName(dir);
|
|
if (dirName.StartsWith(wNameUpper))
|
|
{
|
|
targetDir = dirName;
|
|
break;
|
|
}
|
|
}
|
|
if (string.IsNullOrEmpty(targetDir))
|
|
{
|
|
Debug.LogError("未找到对应文件夹:" + wNameUpper);
|
|
return;
|
|
}
|
|
var combiePath = "Assets/Art/Animations/Show/" + targetDir;
|
|
Debug.Log("combiePath:" + combiePath);
|
|
var totalGuids = AssetDatabase.FindAssets("t:AnimationClip", new string[] { combiePath });
|
|
var totalClips = new List<AnimationClip>();
|
|
for (var i = 0; i < totalGuids.Length; ++i)
|
|
{
|
|
var guid = totalGuids[i];
|
|
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
|
var clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(assetPath);
|
|
totalClips.Add(clip);
|
|
}
|
|
|
|
var overrideClips = new List<KeyValuePair<AnimationClip, AnimationClip>>();
|
|
var assetClips = asset.animationClips;
|
|
for (int i = 0; i < assetClips.Length; i++)
|
|
{
|
|
var oldClip = assetClips[i];
|
|
// ani_g01_s_squatready001
|
|
var oldName = oldClip.name;
|
|
// 找到 squatready001
|
|
var splitOldNames = oldName.Split('_');
|
|
var endStr = splitOldNames[3];
|
|
var newName = $"ani_{wName}_s_{endStr}";
|
|
var searchClip = _SearchClip(totalClips, newName);
|
|
if (searchClip != null)
|
|
{
|
|
overrideClips.Add(new KeyValuePair<AnimationClip, AnimationClip>(oldClip, searchClip));
|
|
}
|
|
}
|
|
asset.ApplyOverrides(overrideClips);
|
|
|
|
AssetDatabase.Refresh();
|
|
}
|
|
|
|
[MenuItem("Assets/*Custom/动画/自动设置战场动画机")]
|
|
private static void _AutoSetFightAnims()
|
|
{
|
|
var asset = Selection.activeObject as AnimatorOverrideController;
|
|
if (asset == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
// 先进行checkout
|
|
// var path = AssetDatabase.GetAssetPath(asset);
|
|
// VersionControlSystem.Checkout(path);
|
|
|
|
// 再进行设置
|
|
// 找到工程内所有clips
|
|
var totalGuids = AssetDatabase.FindAssets("t:AnimationClip", new string[] { "Assets/Art/Animations/Battle" });
|
|
var totalClips = new List<AnimationClip>();
|
|
for (var i = 0; i < totalGuids.Length; ++i)
|
|
{
|
|
var guid = totalGuids[i];
|
|
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
|
|
var clip = AssetDatabase.LoadAssetAtPath<AnimationClip>(assetPath);
|
|
totalClips.Add(clip);
|
|
}
|
|
|
|
var controllerName = asset.name;
|
|
var splitNames = controllerName.Split('_');
|
|
var aName = splitNames[1];
|
|
var bName = splitNames[2];
|
|
|
|
var overrideClips = new List<KeyValuePair<AnimationClip, AnimationClip>>();
|
|
var assetClips = asset.animationClips;
|
|
for (var i = 0; i < assetClips.Length; ++i)
|
|
{
|
|
var oldClip = assetClips[i];
|
|
var oldName = oldClip.name;
|
|
if (oldName.StartsWith("common_"))
|
|
{
|
|
// 找到工程内ani_common_ 对应的动作
|
|
var newName = oldName.Replace("common_", "ani_common_");
|
|
var searchClip = _SearchClip(totalClips, newName);
|
|
if (searchClip != null)
|
|
{
|
|
overrideClips.Add(new KeyValuePair<AnimationClip, AnimationClip>(oldClip, searchClip));
|
|
}
|
|
}
|
|
else if (oldName.StartsWith("skill_"))
|
|
{
|
|
// 找到工程内ani_a_skill对应的动作
|
|
var newName = oldName.Replace("skill_", $"ani_{aName}_skill");
|
|
var searchClip = _SearchClip(totalClips, newName);
|
|
if (searchClip != null)
|
|
{
|
|
overrideClips.Add(new KeyValuePair<AnimationClip, AnimationClip>(oldClip, searchClip));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 找到工程内ani_b_对应的动作
|
|
var newName = $"ani_{bName}_{oldName}";
|
|
var searchClip = _SearchClip(totalClips, newName);
|
|
if (searchClip != null)
|
|
{
|
|
overrideClips.Add(new KeyValuePair<AnimationClip, AnimationClip>(oldClip, searchClip));
|
|
}
|
|
}
|
|
}
|
|
|
|
asset.ApplyOverrides(overrideClips);
|
|
}
|
|
|
|
private static AnimationClip _SearchClip(List<AnimationClip> clips, string searchName)
|
|
{
|
|
for (var i = 0; i < clips.Count; ++i)
|
|
{
|
|
var clip = clips[i];
|
|
if (clip.name == searchName)
|
|
{
|
|
return clip;
|
|
}
|
|
}
|
|
Debug.LogError("工程内未找到Clip:" + searchName);
|
|
return null;
|
|
}
|
|
|
|
// 验证方法,用于检查右键菜单是否可用
|
|
/*[MenuItem("Assets/LTGame/自动设置展示动画机", true)]
|
|
[MenuItem("Assets/LTGame/自动设置战场动画机", true)]
|
|
private static bool ValidateSettings()
|
|
{
|
|
return Selection.activeObject is AnimatorOverrideController;
|
|
}*/
|
|
|
|
|
|
}
|