2014-11-13 5 views
3

Resource.Load을 사용하여 Texture2D (.png) 리소스를로드하려고합니다. 나는 경로 패턴 다음 시도했다 :올바른 Resource.Load 경로는 무엇입니까?

Assets/CaseSensitivePath/TextureName 
CaseSensitivePath/TextureName 
Assets/CaseSensitivePath/TextureName.png 
CaseSensitivePath/TextureName.png 

때마다, Resource.Load(path, typeof(Texture2D))는 null를 돌려줍니다. 이것은 내 코드 및 오류 처리입니다 :

public class LazyResource<T> where T : UnityEngine.Object 
{ 

    //Path is read-only 
    public string path { get { return _path; } } 
    private string _path = ""; 
    //Whether NOT FOUND warning was thrown 
    //in that case, further load attemts are ommited and the resource returns always null... 
    public bool failed = false; 
    //Constructor uses the path as first parameter 
    public LazyResource(string path) { 
     _path = path; 
    } 
    //Cached resource 
    private T cache = null; 

    public T res 
    { 
     get 
     { 
      //Does not re-try if it failed before 
      if (cache == null && !failed) 
      { 
       //Load the proper type of resource 
       cache = (T)Resources.Load(_path, typeof(T)); 
       //Throw warning (once) 
       if (cache == null) 
       { 
        Debug.LogWarning("Icon not found at '" + _path + "'!"); 
        failed = true; 
       } 
      } 
      //Can return null 
      return cache; 
     } 
    } 
} 

오류 :

Icon not found at 'Textures/GUI/Build/egg'! 
UnityEngine.Debug:LogWarning(Object) 
LazyResource`1:get_res() (at Assets/WorldObject/LazyResource.cs:28) 
Actions.Action:GetMenuIcon() (at Assets/WorldObject/Action.cs:203) 
HUD:DrawActions(Action[]) (at Assets/Player/HUD/HUD.cs:115) 
HUD:DrawOrdersBar() (at Assets/Player/HUD/HUD.cs:85) 
HUD:OnGUI() (at Assets/Player/HUD/HUD.cs:63) 

Unity3D 프로젝트에 텍스처를로드 할 올바른 경로는 무엇입니까?

답변

4

경로가 반드시 필요한 것은 아닙니다. 당신이 다음 Resources.Load를 사용하려면

Resources.Load<Texture2D>("MyFolder/TextureName"); 

당신이 자원 폴더에 자원을 넣어해야합니다 TextureName이 폴더 안에 있으면 당신은 단순히 다음 사용자가 입력

Resources.Load<Texture2D>("TextureName"); 

를 입력 할 수 있습니다 . 이것은 편집기에서 보이는 방법입니다.

enter image description here

+1

'Assets/GUI/Build/egg.png'에 있습니다. 문제는 내가 [Resources 폴더] (http://answers.unity3d.com/questions/14748/c-resourcesload-problem.html)를 사용해야한다는 것을 몰랐다는 것입니다. 내가 원하는대로 파일을 정리하고 싶습니다. –

+1

'Resources.Load'를 사용하려면 자원을 ** Resource 폴더 **에 넣어야합니다. – FunctionR

+0

다른 것을 사용하겠습니다. –

관련 문제