void Start() { string dirPath = Path.Combine(Application.dataPath, "Resources"); string filepath = Path.Combine(dirPath, "ReadMe.txt"); if (!File.Exists(filepath)) { TextAsset ta = (TextAsset)Resources.Load("ReadMe"); using (StreamWriter writer = new StreamWriter(filepath)) { writer.WriteLine(ta.text); } } if (File.Exists(filepath)) { using (StreamReader reader = new StreamReader(filepath)) { mReadMeText = reader.ReadToEnd(); } } }
Tuesday, January 20, 2015
Unity: Load a file in the build resources folder
In unity, we may want to allow user to have access to the content of a particular file which they can modify and incorporate back into the game. The simple way is to let user create it (or let the game program to create it during the runtime) in the "Resources" folder under the data folder of the game build, which can then be loaded subsequently in the game. However, we may also want to provides user with a default version at the beginning so that they can modify based on the original version. This can be simply solved by having the original version of the file stored in a created folder name "Resources" under the Assets folder in the game development project. When the game is built, the contents in this "Resources" folder will be packaged into the game. When the game start, we can have a script to check whether a version of the file already exists in the "Resources" folder under the data folder of the game build, if not, the default version can be loaded from the game package and saved into that folder. Subsequently the user can then modify the file directly and have it loaded in the game. Below shows a demo script for this concept (in this case, the particular file is ReadMe.txt)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment