TIL(2024,06,26)Unity 되돌아보기

김보근·2024년 6월 26일

Unity

목록 보기
25/113
post-thumbnail

오늘은 Unity 되돌아보는시간을 가져볼것이다.

Unity

ScriptableObject

ScriptableObject는 데이터 관리와 공유를 쉽게 하기 위해 사용되는 클래스입니다. 게임 설정, 아이템 데이터 등 다양한 용도로 활용할 수 있습니다.

스크립트

using UnityEngine;

[CreateAssetMenu(fileName = "NewItem", menuName = "Inventory/Item")]
public class Item : ScriptableObject
{
    public string itemName;
    public Sprite icon;
    public int value;
}

ScriptableObject를 생성한 후, 프로젝트 창에서 우클릭 -> Create -> Inventory -> Item을 선택합니다.
생성된 아이템의 속성을 설정합니다.

Particle System

Particle System은 불꽃, 연기, 폭발 등 다양한 효과를 구현하는 데 사용됩니다. 유니티의 파티클 시스템은 매우 강력하고 유연합니다.

스크립트

using UnityEngine;

public class PlayParticleEffect : MonoBehaviour
{
    public ParticleSystem particleSystem;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // 파티클 효과 재생
            particleSystem.Play();
        }
    }
}

AudioSource 컴포넌트

AudioSource는 오디오를 재생하는 데 사용됩니다. 배경음악, 효과음 등을 재생할 수 있습니다.

스크립트

using UnityEngine;

public class PlaySound : MonoBehaviour
{
    public AudioSource audioSource;
    public AudioClip clip;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // 오디오 클립 재생
            audioSource.PlayOneShot(clip);
        }
    }
}

Coroutines

Coroutines는 비동기적으로 실행되는 메소드를 작성할 때 사용됩니다. 시간 지연, 반복적인 작업 등에 유용합니다.

using UnityEngine;
using System.Collections;

public class CoroutineExample : MonoBehaviour
{
    void Start()
    {
        StartCoroutine(ExampleCoroutine());
    }

    IEnumerator ExampleCoroutine()
    {
        Debug.Log("코루틴 시작");
        yield return new WaitForSeconds(2);
        Debug.Log("2초 후 실행");
    }
}

Raycasting

Raycasting은 화면에서 특정 지점 또는 방향으로 가상의 레이를 발사하여 충돌을 감지하는 데 사용됩니다. 주로 시선 추적, 사격 게임 등에서 사용됩니다.

using UnityEngine;

public class RaycastExample : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                Debug.Log("충돌한 오브젝트: " + hit.collider.name);
            }
        }
    }
}

Event System

Event System은 UI 이벤트를 관리하는 시스템입니다. 버튼 클릭, 마우스 오버 등의 이벤트를 처리할 수 있습니다.

using UnityEngine;
using UnityEngine.UI;

public class ButtonClickHandler : MonoBehaviour
{
    public Button myButton;

    void Start()
    {
        myButton.onClick.AddListener(OnButtonClick);
    }

    void OnButtonClick()
    {
        Debug.Log("버튼 클릭됨!");
    }
}

이렇게 오늘도 다양한 기법과 여러 컴포넌트들에 대해서 자세히 알아보았다.

profile
게임개발자꿈나무

0개의 댓글