Unity - basic methods of using various special effects

Special effects are an indispensable part of game production. As a game developer, the most important job is to add special effects to the game and play them at the right time and at the right place. At the same time, you must also pay attention to the management and destruction of special effects. .

Certain types of special effects, such as motion effects and decals, also require writing script code for more detailed control. Therefore, this article will briefly introduce how to use various special effects.


There is nothing difficult about the playback of special effects and animations. In many cases, you only need to instantiate objects.

1. Create particle effects

There is a set of free particle resources suitable for learning and using in the Asset Store, called Unity Particle Pack. After importing this set of materials, you may be prompted to restart Unity. Just follow the prompts. This set of materials is used for explanation below.

After importing the resource package, find the prefab of the particle material, such as EarthShatter. Drag it into the scene to instantly preview the effect. When a special effect object is selected, a small tool window will appear in the scene, allowing you to easily pause, play and replay particles, and adjust the preview speed.

Check this resource file and find that its extension is ".prefab". After selecting it, you can see that it is indeed just an ordinary prefab file. This object has a Particle System (particle component) mounted on it, and has 5 sub-objects, each of which has a particle component mounted on it. By disabling each sub-object individually, you can see what each sub-object does. For example, Rock Spike is the main body of the generated rock, Fire Embers are the flying sparks, Fire Ball is the fireball moving along the path, etc. If you analyze more particle effects, you will find that special effects designers often achieve gorgeous and complex special effects through the superposition of multiple small special effects.

From the perspective of using special effects, since the special effects material is just a prefab, the only thing to do is to instantiate it, which is no different from creating any object.

Create an empty object and mount the following code. When running the game, press the space bar to play particles.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreatParticle : MonoBehaviour
{
    public GameObject prefabParticle;
    void Update()
    {
        if(Input.GetButtonDown("Jump"))
        {
            GameObject particle = Instantiate(prefabParticle);
        }
    }
}

2. Life cycle of particle effects

The life cycle of an object mainly refers to the process from creation to destruction of the object. As a technical developer, you don't have to care about the production of resources, but you must care about the management of the resource life cycle.

The particle component has a certain playback time, and life cycle-related options are added to set loop playback, stop playback, or destroy itself after playback is completed. These options are generally in the first edit bar of the particle component.

Most of the options of the particle component affect the playback effect of the particles and are the responsibility of the special effects designer. Some options are related to the particle life cycle and require the responsibility of the technical developer.

  1. Duration, the total time of particle playback
  2. Looping, whether to loop. This option is used for certain persistent effects, such as bonfires
  3. Play On Awake, play immediately when created. If this option is not checked, the Play function needs to be called after the script creates the sub-object.
  4. Stop Action, the behavior after the particle playback is completed
  5. Ring Buffer Mode, whether to enable the ring buffer. The ring buffer is a program optimization technology similar to an object pool, which can realize resource recycling and reuse, greatly reducing the cost of creating and destroying resources. This option is related to Max Particles (maximum number of particles) and is an optimization for a large number of particles in the current particle object.

If the particles do not play automatically after they are created, the code for script playback is as follows:

GameObject particle = Instantiate(prefabParticle);
//如果粒子没有勾选Play On Awake选项,就需要手动调用Play
ParticleSystem ps = particle.GetComponent<ParticleSystem>();
ps.play();

The Stop Action in the particle options has the following options:

  1. None, do nothing
  2. Disable, disable the current object. If you want to play it again later, you can let the particles be automatically disabled first
  3. Destroy, destroy the current object. This is the simplest and easiest way to destroy particles without writing a script.
  4. Callback, calls the script method to let the script proceed to the next step. The script needs to be mounted on the object of the particle component. The callback method of the particle is as follows
    public void OnParticle SystemStopped() {
    Debug.Log("粒子停止");
    }

 Generally speaking, if the particles are useless after playing once, the most common method should be to set the Stop Action option to Destroy to automatically destroy the object. If there are other special needs, there are many ways to destroy them. In addition, you can also let the script perform destruction regularly, and the time is Duration; you can also choose Callback to notify the script after the particle playback is completed, and further process it in the OnParticleSystemStopped method of the script.

3. Create animated special effects

You only need to use a script to create the corresponding object.

4. Destroy animations or animations regularly

To allow animations or animations to be destroyed after a certain period of time, you can use componentization to design a universal script, specify the destruction time in advance, and mount all created particles or animations.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TimeDestroy : MonoBehaviour
{
    public float time = 1;
    void Start()
    {
        invoke("Destroy", time);
    }
    void Destroy()
    {
        Destroy(gameObject);
    }
 
}

The above code uses the Invoke method to delay the call, and can also be implemented using coroutines. This idea is very general and can be used on various objects such as particles and bullets in the game.

Of course, you can also use the "animation frame event" method for animation. Add a frame event to the last frame of the animation and call the script to destroy the animation, but editing a large number of animations will be cumbersome.

5. Adjust animation and particle parameters

 In most cases, there is no need to adjust the parameters of animation and particles, just play it directly. Sometimes there are some special requirements, such as animation acceleration and deceleration playback, particles changing the overall time, etc. Actual examples are as follows

GameObject particle = Instantiate(prefabParticle);
//如果粒子没有勾选Play On Awake选项,就需要手动调用Play
ParticleSystem ps = particle.GetComponent<ParticleSystem>();
//注意!不要再播放后修改参数,不支持
//因此测试本脚本必须取消勾选粒子的Play On Awake选项

//获取主参数,即粒子组件界面上的第一组参数
ParticleSystem.MainMoudle main = ps.main;
main.duartion = 1;      //改变总持续时间
main.startSpeed = 40;   //改变初始速度
main.stopAction = ParticleSystemStopAction.Destroy;   //改变播放一次后的行为

//同理,获取发射参数
ParticleSystem.EmissionModule emission = ps.emission;
emission.rateOverTime = 1000;    //加大发射频率

ps.Play();

As shown in the above code, most parameters of particles can be modified through scripts. Due to changes in related functions, many developers encountered compilation errors.

In the above code, each parameter group must be assigned to a temporary variable (such as main or emission) before modifying the variable. However, parameters cannot be modified directly with a one-line expression. This is probably because types such as MainModule and Emmision are structures, and the particle system uses special syntax to bind structure data and particle components, resulting in this rare syntax. Phenomenon.

Modifying the animation playback speed is also a common requirement. The sample code is as follows

//创建动画prefab
GameObejct obj = Instantiate(prefab);
//给都规划加上定时销毁脚本,并定时1秒
obj.AddComponent<TimeDestroy>.time = 1;

//修改动画播放速度为2倍速
Animator anim = obj.GetComponent<Animator>();
anim.speed = 2;

 

 

Guess you like

Origin blog.csdn.net/m0_63024355/article/details/132274296