2017-01-27 1 views
2

프리 패브를로드하기 위해 파일 이름이있는 파일이 있습니다. File.ReadAllLines (hardPathToFile)로 파일을로드했지만 빌드 할 때 작동하지 않습니다. "Resource"폴더를 사용해야합니다. 그래서 Resource.Load(fileName)을 사용해 보았지만 목록이나 배열을 가져 오지 않았습니다. 나는 그 다음에 File.ReadAllLines(Resource.Load(fileName, typeOf(TextAsset)) as TextAsset))을 시도했지만로드 된 리소스가 문자열 파일 경로가 아니기 때문에 작동하지 않습니다.File.ReadAllLines에서 리소스 텍스트 파일을 사용하려면 어떻게해야합니까?

void getList(string filePath) 
{ 
    prefabObjects.Clear(); 
    //Read all the lines from the file. File loaded from resources 

    print((Resources.Load(filePath, typeof(TextAsset)) as TextAsset).text); 

    var prefabs = File.ReadAllLines((Resources.Load(filePath, typeof(TextAsset)) as TextAsset).text); 
    print(prefabs); 
    foreach (var prefab in prefabs) 
    { 
     print(prefab); 
     GameObject newPrefab = Resources.Load(prefab, typeof(GameObject)) as GameObject; 
     prefabObjects.Add(newPrefab); 
    } 
} 

가 어떻게이 작업을 얻을 수 :

여기에 전체 코드는? 이전 파일을 하드 경로를 사용하는 경우

모든 도움 감사합니다, 감사합니다 :)

+1

@Fredrik가, 그래 지금 파일 이름 만입니다. 하드 경로를 사용했을 때부터 그냥 남아 있습니다. – crabcrabcam

+0

나는 문제가 다른 곳에 있다는 것을 안다! – Maakep

+0

@Slai, 나는 그렇게 생각하지 않는다. 그것은 Unity가 아닌 C# 리소스에 관한 것입니다. 적어도 내 질문에는 대답하지 않습니다. – crabcrabcam

답변

2

, 나는 당신이 /path/to/file.txt 같은 짓 같은데요. Resources.Load()을 사용하면 파일 끝 (file.txt ->file)을 지정하지 않습니다.

여기에 내가 TEXTFILE의 내용을 읽어 내 프로젝트에서 무슨 짓을했는지 :

var api = Resources.Load("ConnectionString") as TextAsset; 
var apiKey = api.text; 
+1

'apiKey' 변수는'File.ReadAllLines()'와 같은리스트로 나눌 수 있습니까 아니면 그냥 문자열입니까? – crabcrabcam

+1

전체 내용 : https://docs.unity3d.com/ScriptReference/TextAsset-text.html 예를 들어 줄 바꿈에서 .Split()을 사용할 수 있습니다. http : // stackoverflow .com/questions/1547476/가장 쉬운 방법 - 분할 - 문자열 -에 - 뉴 라인에서 - 그물 – Maakep

+1

나는 그것을 관리 할 수있어'.Split()'감사합니다 :) – crabcrabcam

관련 문제