유니티

[유니티] Prefab을 Script에서 불러오기(Resource 폴더 사용)

HorseDragon 2023. 3. 16. 00:05

Prefab을 별도의 드래그 없이, Script에서 불러올 때 쓰는 방법.

 

아래와 같이 지정하면, 이미 해당 경로에 저장된 prefab을 불러올 수 있다.

Assets/Resources/buildEffect01.prefab

여기서 중요한 것은, Assets/Resources 폴더 안에 prefab을 넣어놔야 한다는 점.

 

GameObject를 가져오는 것이기 때문에 (GameObject)를 이용해서 불러오면 된다.

public GameObject buildEffect;

void Start() 
    {
        buildEffect = (GameObject)Resources.Load("buildEffect01");        
    }

자세한 내용은 아래 링크를 참고했음.

 

Unity - Scripting API: Resources.Load (unity3d.com)

 

Unity - Scripting API: Resources.Load

This method returns the asset at path if it can be found, otherwise it returns null. Note that the path is case insensitive and must not contain a file extension. All asset names and paths in Unity use forward slashes, so using backslashes in the path will

docs.unity3d.com