Game Development Community

dev|Pro Game Development Curriculum

Quick and dirty asset file generation

by Richard Ranft · 03/02/2013 (7:44 am) · 3 comments

Add these functions somewhere and you can call this from the console or script to generate starting asset.taml files for your assets. You'll still have to customize them but this should save a ton of typing.

/// <summary>
/// Call this function to scan a folder for images or sound files and create 
/// asset files for them.
/// </summary>
/// <param name="path">The path to scan - "^Assets/data/images/".</param>
/// <param name="type">The asset type - "image" or "sound".</param>
/// <param name="category">The asset category.  If none, leave this blank.</param>
function makeAssetFiles(%path, %type, %category)
{
    switch$(%type)
    {
        case "image":
            %imageFile = findFirstFile(expandPath(%path @ "*.png"));
            while (%imageFile !$= "")
            {
                createAsset(%imageFile, %path, %type, %category);
                %imagefile = findNextFile(expandPath(%path @ "*.png"));
            }

        case "sound":
            %audioFile = findFirstFile(expandPath(%path @ "*.wav"));
            while (%audioFile !$= "")
            {
                createAsset(%audioFile, %path, %type, %category);
                %audioFile = findNextFile(expandPath(%path @ "*.wav"));
            }
    }
}


/// <summary>
/// This function creates an individual asset file for a particular image or sound.
/// </summary>
/// <param name="file">The file to create the asset for.</param>
/// <param name="path">The path to the file - "^Assets/data/images/"</param>
/// <param name="type">The asset type - "image" or "sound"</param>
/// <param name="category">The asset category.  If none, leave this blank</param>
function createAsset(%file, %path, %type, %category)
{
    switch$(%type)
    {
        case "image":
            %asset = new ImageAsset();
            %asset.AssetName = fileBase(%file);
            %asset.AutoUnload = "0";
            %asset.ImageFile = %file;
            %asset.AssetCategory = %category;
            
        case "sound":
            %asset= new AudioAsset();
            %asset.AssetName = fileBase(%file);
            %asset.AutoUnload = "0";
            %asset.AudioFile = %file;
            %asset.AssetCategory = %category;
            
    }
    %assetFileName = expandPath(%path @ fileBase(%file) @ ".asset.taml");

    TamlWrite(%asset, %assetFileName);
}

Note that this does not register the new assets, it only makes the files. There are a number of ways you can register the new assets, either individually as you generate them (have to add that at the end of the createAsset() function) or by asking the AssetDatabase object to find them, or by reloading the module that contains them, or whatever.

About the author

I was a soldier, then a computer technician, an electrician, a technical writer, game programmer, and now software test/tools developer. I've been a hobbyist programmer since the age of 13.


#1
03/03/2013 (8:26 pm)
Nice! Features like this always help with bulk and tedious work.
#2
03/20/2013 (2:18 pm)
one of the things that made me sick of the eck pack was creating all the material entries, using the material editor at the time was a headache in itself with the random ;'s it used the ghost in there somehow and break everything, so I put all the materials in by hand, and theres like 416 or something sick total, plus then each layer on each terrain had an entry. it was just one of those things that made me want to mash my head against a brick wall for fun...
#3
03/29/2013 (8:30 am)
I don't remember where I read it - probably some Linux book - but "whenever possible, write code to write code...." If you get your generator code right, the generated code will always be right - avoiding those bleary-eyed 3:52am typos.