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() 이 함수로 에디터 상에서 얻어올 수 있다
'게임엔진(GameEngine) > Unity3D' 카테고리의 다른 글
모바일 기기의 Tile Based Rendering(타일 기반 렌더링) (0) | 2020.02.24 |
---|---|
[InitializeOnLoad] : 에티더 UnityEditor 에디터 스크립트 생성자 호출하기 (0) | 2019.09.07 |
컴포넌트를 추가 할때 호출 되는 함수 Reset() (0) | 2019.09.01 |
Coroutine 과 Thread 차이 (0) | 2019.06.20 |
Why does Mathf.Sign(0.0f) return 1? (0) | 2019.06.11 |