Game Development Reference
In-Depth Information
Accessing the .asset files in the code
Now, if you don't want to assign the asset through the editor, there is a way to just load the
.asset file directly from the project.
Firstly, to do this, you will need to store your .asset files in a special folder named Re-
sources in your Asset folder. You can read them there directly using Unity's own re-
source functions once.
As an example, open the PositionManager script and add the following function:
public static PositionManager ReadPositionsFromAsset(string
Name)
{
string path = "/";
object o = Resources.Load(path + Name);
PositionManager retrievedPositions = (PositionManager)o;
return retrievedPositions;
}
This function, which is available from anywhere as it is static, will perform the following
tasks:
• Using the Name parameter, it will read the .asset file from the root of the Re-
sources folder
• It will convert the retrieved file to the correct object type
• It will return the deserialized object to the calling function
So now you can call up the data contained within your .asset file anywhere in your
game project.
Tip
The same kind of pattern can also be used to download the .asset files from the Web
for your project to add DLC or expand the levels of your game. A word to the wise
though; if you do go down this route, be sure to compress and encrypt your assets that are
meant for downloading to protect your IP.
Search WWH ::




Custom Search