Files
Main/Assets/Editor/DIY/AnimChangeEditor/AnimChangeEditor.cs
2025-01-25 04:38:09 +08:00

361 lines
14 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Thousandto.Core.Asset;
using Thousandto.Launcher.ExternalLibs;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Thousandto.DIY.UniScene
{
//修改animation工具修改为Playable播放器支持的格式
public class AnimChangeEditor : EditorWindow
{
[MenuItem("Ares/AnimToPlayableSelect")]
static void AnimToPlayableSelect()
{
var selectObj = UnityEditor.Selection.gameObjects;
if (selectObj == null || selectObj.Length <= 0)
return;
for(int i = 0; i < selectObj.Length;++i)
{
var go = selectObj[i];
var anim = go.GetComponent<Animation>();
if (anim != null)
{
var meshRender = go.GetComponent<SkinnedMeshRenderer>();
if (meshRender == null)
{
meshRender = go.GetComponentInChildren<SkinnedMeshRenderer>();
}
Avatar av = null;
if (meshRender != null)
{
var fbxPath = AssetDatabase.GetAssetPath(meshRender.sharedMesh);
if (!string.IsNullOrEmpty(fbxPath))
{
av = AssetDatabase.LoadAssetAtPath(fbxPath, typeof(Avatar)) as Avatar;
if (av == null)
{
//输出错误日志
Debug.LogErrorFormat("转换失败,请修改FBX动作的导入方式 {0}", fbxPath);
}
}
else
{
Debug.LogErrorFormat("SkinnedMeshRenderer获取错误 {0}", go.name);
}
}
if (av != null)
{
var animList = go.RequireComponent<AnimListScript>();
var animClipArray = AnimationUtility.GetAnimationClips(go);
if (animClipArray != null && animClipArray.Length > 0)
{
animList.NewAnimList = new List<AnimInfo>(animClipArray.Length);
for (int j = 0; j < animClipArray.Length; ++j)
{
animList.AddClip(animClipArray[j]);
}
}
var animator = go.RequireComponent<Animator>();
animator.avatar = av;
//移除动画组件
GameObject.DestroyImmediate(anim, true);
}
}
FillSlots(go);
}
}
[MenuItem("Ares/AnimToPlayable")]
static void Open()
{
var win = (AnimChangeEditor)EditorWindow.GetWindow(typeof(AnimChangeEditor));
win.Show();
}
private string _selectPath = string.Empty;
private void OnGUI()
{
EditorGUILayout.BeginVertical();
GUILayout.Label("选择路径");
if (GUILayout.Button("浏览", GUILayout.Width(50)))
{
_selectPath = EditorUtility.OpenFolderPanel("Change Layer Prefab Path", _selectPath, "");
}
if (string.IsNullOrEmpty(_selectPath))
{
EditorGUILayout.LabelField("null...");
}
else
{
EditorGUILayout.LabelField(_selectPath);
}
if (GUILayout.Button("转换", GUILayout.Width(50)))
{
DirectoryInfo direction = new DirectoryInfo(_selectPath);
FileInfo[] files = direction.GetFiles("*.prefab", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; ++i)
{
var path = files[i].FullName.Replace('\\', '/');
var starIndex = path.IndexOf("/Assets/", 0);
path = path.Remove(0, starIndex + 1);
var go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
if (go != null)
{
var save = false;
var anim = go.GetComponent<Animation>();
if (anim != null)
{
var meshRender = go.GetComponent<SkinnedMeshRenderer>();
if (meshRender == null)
{
meshRender = go.GetComponentInChildren<SkinnedMeshRenderer>();
}
Avatar av = null;
if (meshRender != null)
{
var fbxPath = AssetDatabase.GetAssetPath(meshRender.sharedMesh);
if (!string.IsNullOrEmpty(fbxPath))
{
av = AssetDatabase.LoadAssetAtPath(fbxPath, typeof(Avatar)) as Avatar;
if (av == null)
{
//输出错误日志
Debug.LogErrorFormat("转换失败,请修改FBX动作的导入方式 {0}", path);
}
}
}
if (av != null)
{
var animList = go.RequireComponent<AnimListScript>();
var animClipArray = AnimationUtility.GetAnimationClips(go);
if (animClipArray != null && animClipArray.Length > 0)
{
animList.NewAnimList = new List<AnimInfo>(animClipArray.Length);
for (int j = 0; j < animClipArray.Length; ++j)
{
animList.AddClip(animClipArray[j]);
}
}
if (av != null)
{
var animator = go.RequireComponent<Animator>();
animator.avatar = av;
//移除动画组件
GameObject.DestroyImmediate(anim, true);
}
save = true;
}
}
save |= FillSlots(go);
if(save)
{
AssetDatabase.SaveAssets();
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
}
}
}
}
if (GUILayout.Button("去掉legacy", GUILayout.Width(100)))
{
CheckLegacy(_selectPath);
}
EditorGUILayout.EndVertical();
}
public static void CheckLegacyEvent()
{
var checkPath = Application.dataPath + "/GameAssets/Resources/Prefab";
CheckLegacy(checkPath);
}
public static bool CheckSlot(GameObject go)
{
if(FillSlots(go))
{
AssetDatabase.SaveAssets();
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
return true;
}
return false;
}
private static void CheckLegacy(string selectPath)
{
DirectoryInfo direction = new DirectoryInfo(selectPath);
FileInfo[] files = direction.GetFiles("*.prefab", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; ++i)
{
var path = files[i].FullName.Replace('\\', '/');
var starIndex = path.IndexOf("/Assets/", 0);
path = path.Remove(0, starIndex + 1);
var go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
if (go == null)
continue;
var anim = go.GetComponent<AnimListScript>();
if (anim == null)
continue;
var saveGo = FillSlots(go);
var animList = anim.GetAnimListByEditor();
for (int j = 0; animList != null && j < animList.Count; ++j)
{
if (animList[j] != null)
{
var animClip = AssetDatabase.LoadAssetAtPath<AnimationClip>(AssetDatabase.GetAssetPath(animList[j]));
if (animClip.legacy)
{
animClip.legacy = false;
EditorUtility.SetDirty(animClip);
}
}
}
animList = anim.GetSelfAnimList();
for (int j = 0; animList != null && j < animList.Count; ++j)
{
if (animList[j] != null)
{
var animClip = AssetDatabase.LoadAssetAtPath<AnimationClip>(AssetDatabase.GetAssetPath(animList[j]));
if (animClip.legacy)
{
animClip.legacy = false;
EditorUtility.SetDirty(animClip);
}
}
}
if (saveGo)
{
EditorUtility.SetDirty(go);
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
}
}
private static void FillBuildin(Transform trans, List<GameObject> level1, List<GameObject> level2, List<GameObject> level3)
{
if(trans.name == "p_m_buildin")
{
var trans1 = trans.Find("[1]");
var trans2 = trans.Find("[2]");
var trans3 = trans.Find("[3]");
if (trans1 != null) level1.Add(trans1.gameObject);
if (trans2 != null) level2.Add(trans2.gameObject);
if (trans3 != null) level3.Add(trans3.gameObject);
}
for(int i =0;i< trans.childCount; ++i)
{
FillBuildin(trans.GetChild(i), level1, level2, level3);
}
}
public static bool FillSlots(GameObject go)
{
if (go == null)
return false;
var script = go.GetComponent<AnimListScript>();
if (script == null)
{
script = go.AddComponent<AnimListScript>();
}
var newTrans = new Transform[(int)Slot.MaxCount];
for (int i = 0; i <(int)Slot.MaxCount; ++i)
{
newTrans[i] = SlotUtils.FindSlotTransform(go.transform, (Slot)i);
}
var isChanged = false;
if(script.SlotTrans == null || script.SlotTrans.Length != newTrans.Length)
{
script.SlotTrans = newTrans;
isChanged = true;
}
else
{
for (int i = 0; i < script.SlotTrans.Length; ++i)
{
if (script.SlotTrans[i] != newTrans[i])
{
isChanged = true;
}
if (newTrans[i] != null && newTrans[i].localScale != Vector3.one)
{
newTrans[i].localScale = Vector3.one;
isChanged = true;
}
}
}
var level1 = new List<GameObject>();
var level2 = new List<GameObject>();
var level3 = new List<GameObject>();
FillBuildin(go.transform, level1, level2, level3);
if(script.BuildinLevel1 == null || script.BuildinLevel1.Length != level1.Count)
{
isChanged = true;
}
else
{
for(int i =0;i< script.BuildinLevel1.Length; ++i)
{
if (script.BuildinLevel1[i] != level1[i])
{
isChanged = true;
break;
}
}
}
if (script.BuildinLevel2 == null || script.BuildinLevel2.Length != level2.Count)
{
isChanged = true;
}
else
{
for (int i = 0; i < script.BuildinLevel2.Length; ++i)
{
if (script.BuildinLevel2[i] != level2[i])
{
isChanged = true;
break;
}
}
}
if (script.BuildinLevel3 == null || script.BuildinLevel3.Length != level3.Count)
{
isChanged = true;
}
else
{
for (int i = 0; i < script.BuildinLevel3.Length; ++i)
{
if (script.BuildinLevel3[i] != level3[i])
{
isChanged = true;
break;
}
}
}
if (isChanged)
{
script.SlotTrans = newTrans;
script.BuildinLevel1 = level1.ToArray();
script.BuildinLevel2 = level2.ToArray();
script.BuildinLevel3 = level3.ToArray();
return true;
}
return false;
}
}
}