상세 컨텐츠

본문 제목

[Unity] Resources.Load

유니티

by simstealer 2022. 9. 29. 17:14

본문

코드로 Prefab을 지정한 경로에서 찾아 유니티 컴포넌트에 삽입할 수 있는 방법입니다.

 

C:\SMS\mergeTest\Assets\Resources\Prefabs 

 

- Assets/Resources까지를 기본 경로로 사용하고 아래의 폴더는 사용자 임의대로 생성해줍니다.

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

public class SMS_ResourcesText : MonoBehaviour
{
    public GameObject cube;
    
    void Start()
    {
    	// <GameObject> : 게임오브젝트 타입의 데이터를 가져온다는 것
        cube = Resources.Load<GameObject>("Prefabs/Cube(SMS)");
        // Prefabs폴더 밑에 "Cube(SMS)"의 프리펩을 cube 객체에 붙여준다.
        
        // 화면에 보여주기 위해 복제를 했다.
        Instantiate(cube);
    }
}

관련글 더보기

댓글 영역