Using Resources Folder in Unity

后端 未结 1 746
青春惊慌失措
青春惊慌失措 2020-11-22 02:49

I have am developing a HoloLens project that needs to reference .txt files. I have the files stored in Unity\'s \'Resources\' folder and have them working perfectly fine (wh

相关标签:
1条回答
  • 2020-11-22 03:10

    You can't read the Resources directory with the StreamReader or the File class. You must use Resources.Load.

    1.The path is relative to any Resources folder inside the Assets folder of your project.

    2.Do not include the file extension names such as .txt, .png, .mp3 in the path parameter.

    3.Use forward slashes instead of back slashes when you have another folder inside the Resources folder. backslashes won't work.

    Text files:

    TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
    string tileFile = txtAsset.text;
    

    Supported TextAsset formats:

    txt .html .htm .xml .bytes .json .csv .yaml .fnt

    Sound files:

    AudioClip audio = Resources.Load("soundFile", typeof(AudioClip)) as AudioClip;
    

    Image files:

    Texture2D texture = Resources.Load("textureFile", typeof(Texture2D)) as Texture2D;
    

    Sprites - Single:

    Image with Texture Type set to Sprite (2D and UI) and

    Image with Sprite Mode set to Single.

    Sprite sprite = Resources.Load("spriteFile", typeof(Sprite)) as Sprite;
    

    Sprites - Multiple:

    Image with Texture Type set to Sprite (2D and UI) and

    Image with Sprite Mode set to Multiple.

    Sprite[] sprite = Resources.LoadAll<Sprite>("spriteFile") as Sprite[];
    

    Video files (Unity >= 5.6):

    VideoClip video = Resources.Load("videoFile", typeof(VideoClip)) as VideoClip;
    

    GameObject Prefab:

    GameObject prefab = Resources.Load("shipPrefab", typeof(GameObject)) as GameObject;
    

    3D Mesh (such as FBX files)

    Mesh model = Resources.Load("yourModelFileName", typeof(Mesh)) as Mesh;
    

    3D Mesh (from GameObject Prefab)

    MeshFilter modelFromGameObject = Resources.Load("yourGameObject", typeof(MeshFilter)) as MeshFilter;
    Mesh loadedMesh = modelFromGameObject.sharedMesh; //Or   design.mesh
    

    3D Model (as GameObject)

    GameObject loadedObj = Resources.Load("yourGameObject") as GameObject;
    //MeshFilter meshFilter = loadedObj.GetComponent<MeshFilter>();
    //Mesh loadedMesh = meshFilter.sharedMesh;
    
    GameObject object1 = Instantiate(loadedObj) as GameObject;
    

    Accessing files in a sub-folder:

    For example, if you have a shoot.mp3 file which is in a sub-folder called "Sound" that is placed in the Resources folder, you use the forward slash:

    AudioClip audio = Resources.Load("Sound/shoot", typeof(AudioClip)) as AudioClip;
    

    Asynchronous Loading:

    IEnumerator loadFromResourcesFolder()
    {
        //Request data to be loaded
        ResourceRequest loadAsync = Resources.LoadAsync("shipPrefab", typeof(GameObject));
    
        //Wait till we are done loading
        while (!loadAsync.isDone)
        {
            Debug.Log("Load Progress: " + loadAsync.progress);
            yield return null;
        }
    
        //Get the loaded data
        GameObject prefab = loadAsync.asset as GameObject;
    }
    

    To use: StartCoroutine(loadFromResourcesFolder());

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题