playMaker

Author Topic: Get File Name By Index  (Read 1403 times)

amaranth

  • Full Member
  • ***
  • Posts: 172
    • View Profile
Get File Name By Index
« on: February 12, 2013, 09:55:19 PM »
This script returns the name of a file at a specific index in a Resource Folder. Fields are:

  • Folder Name: enter the path where files are located inside the Resources directory. Leave this field empty to get all files in the Resource folder. Example usage: Creatures/Monsters/Ghosts
  • File Index: Index (position) of the file in the folder. Starts at 0.
  • File Name: returns the number of the file at the specified index.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.

using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("Files")]
[Tooltip("Get the name of a file, using its index, in a specified resource folder.")]
public class GetFileNameByIndex : FsmStateAction
{
[Tooltip("The name of the folder where the file is located. If no folder name is entered, all Resource files are returned.")]
public FsmString folderName;

[Tooltip("The index of the file inside the folder. Index starts at 0.")]
public FsmInt fileIndex;

[Tooltip("The name of the file.")]
[UIHint(UIHint.Variable)]
public FsmString fileName;


public override void Reset()
{
folderName = null;
fileIndex = 0;
fileName = null;

}

public override void OnEnter()
{
DoGetFileName();
Finish();
}


void DoGetFileName()
{
Object[] resourceDir = Resources.LoadAll(folderName.Value);
fileName.Value = resourceDir[fileIndex.Value].name;
}

}
}