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。

如果对你有用点个赞吧!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一梭键盘任平生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值