Unity自定义工具之特效播放速度修改工具

目录

前言:

准备:

知识点:

完整代码:

效果:

修改前:

修改后:


前言:

我们在游戏开发过程中,有一些特效需要动态修改播放速度,也有一些特效需要在导入unity后修改速度,而特效子节点太多,手动修改太过麻烦,写一个一键式工具。


准备:

using System;
using System.Runtime.InteropServices;//调用外部库,需要引用命名空间

/// <summary>
/// 为了调用外部库脚本
/// </summary>
public class EditorMessage
{
    [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern int MessageBox(IntPtr handle, String message, String title, int type);//具体方法
}

一个提示窗口

知识点:

Selection.assetGUIDs 所有选中物体的GUID

AssetDatabase.GUIDToAssetPath 利用GUID得到路径

Directory.GetFiles("Assets", "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".prefab") || s.EndsWith(".unity"))
筛选Assets路径下所有的prefab和场景

EditorUtility.DisplayProgressBar("Loading", "打印中......", slider) 进度条

var main = particleSystem.main;
main.simulationSpeed=speed;
修改特效播放速度

完整代码:

using System;
using System.IO;
using UnityEditor;
using UnityEngine;

public class EffectSpeedTool : EditorWindow {
    [MenuItem("SelfTools/EffectSpeedChangeWindow")]
    public static void OpenEffectSpeedChangeWindow() {
        EffectSpeedTool win = EditorWindow.GetWindow<EffectSpeedTool>("EffectSpeedTool");
        win.Show();
    }
    public string speedStr;
    private float speed;
    private void OnGUI() {
        GUILayout.BeginHorizontal();
        speedStr = EditorGUILayout.TextField("输入调整速度", speedStr);
        if(!string.IsNullOrEmpty(speedStr) && !float.TryParse(speedStr, out speed)) {
            EditorMessage.MessageBox(IntPtr.Zero, "输入错误", "确认", 0);
            speedStr = "";
        }
        GUILayout.EndHorizontal();
        GUILayout.Space(10);

        GUILayout.BeginHorizontal();
        if(GUILayout.Button("开始修改", "buttonleft", GUILayout.Width(250), GUILayout.Height(50))) {
            if(string.IsNullOrEmpty(speedStr)) return;
            string[] selectGuids = Selection.assetGUIDs;
            for(int i = 0; i < selectGuids.Length; i++) {
                string path = AssetDatabase.GUIDToAssetPath(selectGuids[i]);
                if(Directory.Exists(path)) {
                    string[] objspath = Directory.GetFiles(path, "*.prefab*");
                    for(int j = 0; j < objspath.Length; j++) {
                        GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(objspath[j]);
                        if(obj == null) continue;
                        ModificationSpeed(obj);
                    }
                } else {
                    GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                    if(obj == null) continue;
                    ModificationSpeed(obj);
                }
            }
            AssetDatabase.SaveAssets();
            EditorMessage.MessageBox(IntPtr.Zero, "修改完成", "确认", 0);
        };
        GUILayout.EndHorizontal();
        GUILayout.Space(10);
    }

    void ModificationSpeed(GameObject obj) {
        ParticleSystem particleSystem = obj.transform.GetComponent<ParticleSystem>();
        if(particleSystem != null) {
            var main = particleSystem.main;
            main.simulationSpeed *= speed;
        }

        for(int i = 0; i < obj.transform.childCount; i++) {
            if(obj.transform.GetChild(i).GetComponent<ParticleSystem>() != null) {
                ModificationSpeed(obj.transform.GetChild(i).gameObject);
            }
        }
    }
}

效果:

修改前:

修改前特效的播放速度是1

修改后:

 

修改后特效的播放速度是0.1

注:此工具修改的是特效播放速度的倍率,不是值,比如说一个特效的本来播放速度是0.1,此时使用工具输入调整速度为2时,特效播放速度会变成0.2,而不是变成2。

如果对你有用点个赞吧!

猜你喜欢

转载自blog.csdn.net/SmillCool/article/details/126883675