Unity 編輯器下控制粒子播放跟隨位移,unity粒子

來源:互聯網
上載者:User

Unity 編輯器下控制粒子播放跟隨位移,unity粒子
        在之前的文章《Unity 編輯器下控制播放粒子》講到在Unity編輯器的Scene視圖進行控制播放粒子ParticleSystem,但是當這個粒子是掛載在人物身體部位的時候,會有可能出現不跟隨位移的情況。尋找原因,發現是Resimulate 被勾選中了,這個選項是指當粒子參數改變時,立即更新粒子效果。要讓粒子也能跟隨移動,必須將這個選項取消掉。

可以簡單的在編輯器下,取消掉這個選項,如下:


但是,對於其他人員可能不知道這個原因,手動設定不夠智能,需要進一步在代碼中主動控制。操縱這個選項,需要取得編輯器類,通過反射來得到,如下:
 C# Code 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Reflection;
using UnityEditor;

public static class EditorParticleSystemHelper
{
    private static Type realType;
    private static PropertyInfo property_editorResimulation;

    public static void InitType()
    {
        if (realType == null)
        {
            var assembly = Assembly.GetAssembly(typeof(Editor));
            realType = assembly.GetType("UnityEditor.ParticleSystemEditorUtils");

            property_editorResimulation = realType.GetProperty("editorResimulation", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
        }
    }

    /// <summary>
    /// 必須禁止粒子的Resimulation,這樣才能讓粒子跟隨位移
    /// </summary>
    public static bool editorResimulation
    {
        set
        {
            InitType();
            property_editorResimulation.SetValue(null, value, null);
        }
    }
}

那麼只要在代碼中合適的地方,使用如下即可: C# Code 
1
EditorParticleSystemHelper.editorResimulation = false;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.