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")) アセット パスの下のすべてをフィルター処理し
ますプレハブとシーン

EditorUtility.DisplayProgressBar("読み込み中", "印刷中...", スライダー)プログレス バー

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 では、特殊効果の再生速度が 2 ではなく 0.2 になります。

役に立ったらいいね!

おすすめ

転載: blog.csdn.net/SmillCool/article/details/126883675