반응형

 

 

hat I am trying to do is in my Start() method of a prefab GameObject in my scene is I want to get the location of where this prefab is as a string but I cannot figure out how to do this.

Example of how I am using this: I put a Sword Prefab GameObject in my scene and on Start() I would like to get the file directory of where this GameObject is in my project.

 

 

 

Include using UnityEditor;

NOTE: This will only work if the prefab is to to public.

Then use AssetDatabase.GetAssetPath to get the path of the prefab.

public GameObject prefab; void Start(){ string prefabPath = AssetDatabase.GetAssetPath(prefab); Debug.Log("Path: " + prefabPath); }

Edit:

I did some experiment with PrefabUtility.GetPrefabType, PrefabUtility.GetPrefabObject and PrefabUtility.GetPrefabParent. PrefabUtility.GetPrefabParent solved the problem. You don't need to make the prefab public with PrefabUtility.GetPrefabParent.

void Start() { GameObject prefab = GameObject.Find("PrebabTest"); Object GameObject2 = PrefabUtility.GetPrefabParent(prefab); string prefabPath = AssetDatabase.GetAssetPath(GameObject2); Debug.Log("Path: " + prefabPath); }

 

 

ref  : https://stackoverflow.com/questions/36850296/get-a-prefabs-file-location-in-unity

 

Get a prefabs file location in Unity

What I am trying to do is in my Start() method of a prefab GameObject in my scene is I want to get the location of where this prefab is as a string but I cannot figure out how to do this. Example ...

stackoverflow.com

 

 

프리팹을 가져오는 함수는 AssetDatabase.LoadAssetAtPath() 이 함수로 에디터 상에서 얻어올 수 있다

반응형

+ Recent posts