1.当制作小怪状态机时,小怪的动作数量和名称几乎相同,只需要制作一个状态机,然后复制改名称,利用脚本替换状态机中的动画为当前文件夹下同名的动画。
[MenuItem("Assets/替换为当前文件夹的动画")]
public static void ReplaceAnimatorClips()
{
Object animObj= Selection.activeObject;
AnimatorController animator = animObj as AnimatorController;
string folderpath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(animObj));
GetAllFiles(folderpath);
if (files.Count <= 0) return;
animationClips.Clear();
for (int i = 0; i < files.Count; i++)
{
string name = Path.GetFileNameWithoutExtension(files[i]);
string[] names = name.Split('@');
if (names.Length == 2)
{
string clipname = names[1];
animationClips[clipname.ToLower()] = files[i];
}
}
ChildAnimatorState[] childstates = animator.layers[0].stateMachine.states;
foreach (var childstate in childstates)
{
AnimatorState state = childstate.state;
Motion mo = state.motion;
if (mo == null)
{
Debug.LogError(state.name + "节点动作是空");
continue;
}
if (mo.GetType().Equals(typeof(BlendTree)))
{
BlendTree tree = (BlendTree)mo;
ReplaceMotionInLocalFolder(tree);
}
else
{
ReplaceMotionInLocalFolder(state);
}
}
AssetDatabase.SaveAssets();
}
static Dictionary<string, string> animationClips = new Dictionary<string, string>();
public static Motion GetLocalMotionFile(string name)
{
if (animationClips.ContainsKey(name.ToLower()))
{
AnimationClip mo = AssetDatabase.LoadAssetAtPath(animationClips[name.ToLower()], typeof(AnimationClip)) as AnimationClip;
if (mo != null)
{
return mo;
}
else
{
Debug.LogWarning("no this name clip in this folder" + name);
}
}
else
{
Debug.LogWarning("no this name clip in this folder" + name);
}
return null;
}
public static void ReplaceMotionInLocalFolder(BlendTree blendtree)
{
Dictionary<int, string> orderRecord = new Dictionary<int, string>();
Dictionary<string, Motion> motionRecord = new Dictionary<string, Motion>();
for (int i = 0; i < blendtree.children.Length; i++)
{
string name = blendtree.children[i].motion.name;
orderRecord[i] = name;
}
for (int i = 0; i < blendtree.children.Length; i++)
{
string name = blendtree.children[i].motion.name;
Motion oldmo = blendtree.children[i].motion;
if (GetLocalMotionFile(name) != null)
{
Motion newmo = GetLocalMotionFile(name);
if (blendtree.children[i].motion != newmo)
{
motionRecord[name] = newmo;
}
else
{
motionRecord[name] = oldmo;
}
}
else
{
motionRecord[name] = oldmo;
}
}
for (int i = blendtree.children.Length - 1; i >= 0; i--)
{
blendtree.RemoveChild(i);
}
for (int i = 0; i < orderRecord.Count; i++)
{
if (!motionRecord.ContainsKey(orderRecord[i]))
{
Debug.LogError("index " + i + "has no motion");
}
else
{
blendtree.AddChild(motionRecord[orderRecord[i]]);
}
}
blendtree.useAutomaticThresholds = true;
}
public static void ReplaceMotionInLocalFolder(AnimatorState state)
{
string name = state.motion.name;
if (GetLocalMotionFile(name) != null)
{
state.motion = GetLocalMotionFile(name);
}
}
//存在当前文件夹下所有的对应后缀的子物体。
static List<string> files = new List<string>(1000);
public static void GetAllFiles(string path)
{
files.Clear();
CollectAllFiles(path);
}
public static void CollectAllFiles(string path)
{
string[] localfiles = Directory.GetFiles(path);
string[] dirs = Directory.GetDirectories(path);
for (int i = 0; i < localfiles.Length; i++)
{
string filepath = localfiles[i];
if (filepath.Contains(".meta")) continue;
files.Add(filepath);
}
for (int j = 0; j < dirs.Length; j++)
{
CollectAllFiles(dirs[j]);
}
}
2.显示文件的相对路径
[MenuItem("Assets/显示路径")]
public static void ShowPath()
{
Object animObj = Selection.activeObject;
string path = AssetDatabase.GetAssetPath(animObj);
AnimationClip clip = AssetDatabase.LoadAssetAtPath(path,typeof(AnimationClip)) as AnimationClip;
if (clip != null)
{
Debug.LogError("get");
}
Debug.LogError(path);
}
3.批量修改文件夹内图片格式
[MenuItem("Assets/修改文件夹内的图片格式")]
static void EditorTextureInFolder()
{
Object fold = Selection.activeObject;
string relatepath = AssetDatabase.GetAssetPath(fold);
string folderpath = Path.GetFullPath(relatepath);
GetAllFiles(folderpath.Replace("\\","/").Replace(Application.dataPath, "Assets"));
for (int i = 0; i < files.Count; i++)
{
if (Path.GetExtension(files[i]).Equals(".png") || Path.GetExtension(files[i]).Equals(".jpg") || Path.GetExtension(files[i]).Equals(".tga"))
{
EditTexture(AssetDatabase.LoadAssetAtPath(files[i],typeof(Texture)) as Texture);
}
}
}
static void EditTexture(Object targetObj)
{
if (targetObj && targetObj is Texture)
{
string path = AssetDatabase.GetAssetPath(targetObj);
TextureImporter texture = AssetImporter.GetAtPath(path) as TextureImporter;
texture.textureType = TextureImporterType.Advanced;
texture.filterMode = FilterMode.Bilinear;
texture.mipmapEnabled = true;
texture.wrapMode = TextureWrapMode.Clamp;
texture.spriteImportMode = SpriteImportMode.None;
texture.textureFormat = TextureImporterFormat.AutomaticCompressed;
AssetDatabase.ImportAsset(path);
}
}