At times the contents of external resource files may need to be changed after the game is compiled. Hosting resource files on the Web may not be an option. There is a method of manually storing and reading files from the Resources
folder of the compiled game. This allows for those files to be changed after the game's compilation.
The 0423_07_01
folder provides the externalTexture.jpg
texture image that you can use for this recipe.
// file: ReadManualResourceImageFile.cs using UnityEngine; using System.Collections; using System.IO; public class ReadManualResourceImageFile : MonoBehaviour { private string fileName = "externalTexture.jpg"; private string url; private Texture2D externalImage; private void Start () { url = "file:" + Application.dataPath; url = Path.Combine(url, "Resources"); url = Path.Combine(url, fileName); StartCoroutine( LoadWWW() ); } private void OnGUI() { GUILayout.Label ( "url = " + url ); GUILayout.Label ( externalImage ); } private IEnumerator LoadWWW(){ yield return 0; WWW www = new WWW (url); yield return www; externalImage = www.texture; } }
externalTexture.jpg
image into your standalone's Resources
folder.You will need to place the files in the Resources
folder manually after every compilation.
WINDOWS and LINUX: When you create a Windows or Linux standalone executable, there is also a _Data
folder created with the executable application file. The Resources
folder can be found inside this data folder.
MAC: A Mac standalone application executable looks like a single file, but it is actually a Mac OS package
folder. Right-click the executable file and select Show Package Contents. You will then find the standalone's Resources
folder inside the Contents
folder.
Note the need to use the System.IO
package for this recipe.
When the executable runs, the WWW
object spots that the URL starts with the word "file:" so Unity attempts to find the external resource file in its Resources
folder and then load its contents.
Here are some details you don't want to miss.
The filepath folder separator character is different for Windows and Mac filesystems ("" backslash for Windows, "/" forward slash for the Mac). However, Unity knows which kind of standalone you are compiling your project into, therefore the Path.Combine()
method will insert the appropriate separator slash character to form the file URL that is required.
To use this technique to load an external text file, you'll need a private string variable:
private string textFileContents = "(still loading file ...)";
Replace your LoadWWW()
method with one that extracts the text of the loaded resource and stores it in your private string variable:
private IEnumerator LoadWWW(){ yield return 0; WWW www = new WWW (url); yield return textFile; textFileContents = www.text; }
Then change OnGUI()
to display the string as a label:
private void OnGUI() { GUILayout.Label ( "url = " + url ); GUILayout.Label ( textFileContents ); }
The WWW
class defines several different properties and methods to allow downloaded media resource file data to be extracted into appropriate variables for use in the game. The most useful of these include:
For more information about the Unity WWW
class, see http://docs.unity3d.com/Documentation/ScriptReference/WWW.html.
3.14.144.229