Unity

[유니티] 효과 넣기

Skull Crusher 2021. 5. 6. 15:32
728x90

1. 효과 입력

JMO Assets -> CartoonFX -> CFX3 Prefabs -> Misc -> CFX3 Hit Smoke Puff 선택

2. 속도 수정

3. Prefab으로 만들기

ItemGenerator 스크립트 수정

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

//코루틴


public class ItemGenerator : MonoBehaviour
{
    public GameObject itemPrefab;

    public float itemGenMinTime;
    public float itemGenMaxTime;

    public GameObject genEffectPrefab;

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine("ItemGenCoroutine");
        //StartCoroutine("SampleCoroutine", "코루틴1");
        //StartCoroutine("SampleCoroutine", "코루틴2");

    }

    //아이템 생성 코루틴
    IEnumerator ItemGenCoroutine()
    {
        while (true)
        {
            float genDelayTime = Random.Range(itemGenMinTime, itemGenMaxTime);

            Debug.Log(genDelayTime + " 시간 뒤에 아이템을 생성함");

            yield return new WaitForSeconds(genDelayTime);

            float posX = Random.Range(-20f, 20f);
            float posZ = Random.Range(-20f, 20f);

            Vector3 genPos = new Vector3(posX, 0f, posZ);

            // 이펙트 생성
            Instantiate(genEffectPrefab, genPos, Quaternion.identity);
            //아이템 생성
            GameObject item = Instantiate(itemPrefab, genPos, Quaternion.identity);
            Debug.Log(item.transform.position + " 위치에 아이템이 생성됨");
        }
    }

    // 코루틴 문법
    //IEnumerator 코루틴메소드명(매개변수)
    // 여러개 사용할 수 있음
    IEnumerator SampleCoroutine(string name)
    {
        int count = 1;

        // 랜덤한 지연시간을 추첨
        float delayTime = Random.Range(1f, 10f);   
        Debug.Log(name + " => " + delayTime + " 초 지연 시작");
        // 지연 객체를 생성하여 코루틴을 지연함
        yield return new WaitForSeconds(delayTime);
        Debug.Log(name + " => " + (count++) + "번째 지연 코드 실행!!!");

        delayTime = Random.Range(1f, 10f);
        Debug.Log(delayTime + " 초 지연 시작");
        yield return new WaitForSeconds(delayTime);
        Debug.Log(name + " => " + (count++) + "번째 지연 코드 실행!!!");

        delayTime = Random.Range(1f, 10f);
        Debug.Log(delayTime + " 초 지연 시작");
        yield return new WaitForSeconds(delayTime);
        Debug.Log(name + " => " + (count++) + "번째 지연 코드 실행!!!");



    }
}

 

4. 효과에 코루틴 사용

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

//코루틴


public class ItemGenerator : MonoBehaviour
{
    public GameObject itemPrefab;

    public float itemGenMinTime;
    public float itemGenMaxTime;

    public GameObject genEffectPrefab;

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine("ItemGenCoroutine");
        //StartCoroutine("SampleCoroutine", "코루틴1");
        //StartCoroutine("SampleCoroutine", "코루틴2");

    }

    //아이템 생성 코루틴
    IEnumerator ItemGenCoroutine()
    {
        while (true)
        {
            float genDelayTime = Random.Range(itemGenMinTime, itemGenMaxTime);

            Debug.Log(genDelayTime + " 시간 뒤에 아이템을 생성함");

            yield return new WaitForSeconds(genDelayTime);

            float posX = Random.Range(-20f, 20f);
            float posZ = Random.Range(-20f, 20f);

            Vector3 genPos = new Vector3(posX, 0f, posZ);

            // 이펙트 생성
            Instantiate(genEffectPrefab, genPos, Quaternion.identity);

            // 이펙트 생성 후 기다리기
            yield return new WaitForSeconds(0, 2f);

            //아이템 생성
            GameObject item = Instantiate(itemPrefab, genPos, Quaternion.identity);
            //Debug.Log(item.transform.position + " 위치에 아이템이 생성됨");
        }
    }
 
    // 코루틴 문법
    //IEnumerator 코루틴메소드명(매개변수)
    // 여러개 사용할 수 있음
    IEnumerator SampleCoroutine(string name)
    {
        int count = 1;

        // 랜덤한 지연시간을 추첨
        float delayTime = Random.Range(1f, 10f);   
        Debug.Log(name + " => " + delayTime + " 초 지연 시작");
        // 지연 객체를 생성하여 코루틴을 지연함
        yield return new WaitForSeconds(delayTime);
        Debug.Log(name + " => " + (count++) + "번째 지연 코드 실행!!!");

        delayTime = Random.Range(1f, 10f);
        Debug.Log(delayTime + " 초 지연 시작");
        yield return new WaitForSeconds(delayTime);
        Debug.Log(name + " => " + (count++) + "번째 지연 코드 실행!!!");

        delayTime = Random.Range(1f, 10f);
        Debug.Log(delayTime + " 초 지연 시작");
        yield return new WaitForSeconds(delayTime);
        Debug.Log(name + " => " + (count++) + "번째 지연 코드 실행!!!");



    }
}