2013년 12월 17일 화요일

How to start and stop the particle by using code in Unity3D

Method of controlling by code for particle when create a particle at Unity3D....  If necessary, you can stop or play by code. 

It do not need play when never used. So, I will want play when is used.

When you produce particle, consider as following.

Particles must fabricated using ParticleSystem Component.
Use both Clear() and Stop() function for In order to disappear on the screen when stopping the particles.
If you want to start a particle, use the Play () function.

Sample code is as follows.

ParticleSystem testParticle         = null;

// 버튼이 눌러졌을때
if (Input.GetMouseButtonUp(0) == true)
{
    // 파티클이 있고
    if (testParticle)
    {
        // 파티클이 재생중이면 재생을 멈추고 지워줍니다
        if (testParticle.isPlaying == true)
        {
            testParticle.Stop();
            testParticle.Clear();   

            //Debug.Log("STOP");
        }
        // 재생중이 아니라면 재생해주고요
        else
        {
            testParticle.Play();

            //Debug.Log("PLAY");
        }
    }
    // 파티클이 없다면 새로 만들어주구요
    else
    {
        Vector3 pos       = Vector3.zero;
        pos.y               = 3;
        pos.x               = 3 - 30.0f;

        Transform particleObject        = (Transform)Instantiate(Resources.Load("Prefabs/Effects/pfBlockBomb", typeof(Transform)), pos, Quaternion.identity);

        testParticle        = (ParticleSystem)particleObject.GetComponent(typeof(ParticleSystem));

        //Debug.Log("CREATE");
    }

    return;
}

댓글 없음:

댓글 쓰기