playMaker

Author Topic: Get File Name By Loop  (Read 2837 times)

amaranth

  • Full Member
  • ***
  • Posts: 172
Get File Name By Loop
« on: February 13, 2013, 12:03:44 AM »
This script returns the names of all files in a Resource Folder. You need to set this script up as a loop to extract each file name. I've attached a sample. 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 Name: returns name of the next file in the directory.
  • Loop Event: A file was found. Do something and return to the loop.
  • No Hit Event: No files were found in directory. Exit loop early.
  • Finished Event: The Loop Event has been called for all files in the loop. Exit the loop.

This script is based on Jean's Get Next Ray Cast All Hit script.

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 GetFileNameByLoop : 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 name of the file.")]
[UIHint(UIHint.Variable)]
public FsmString fileName;

[ActionSection("Hit")]

[Tooltip("Event to send to get the next child.")]
public FsmEvent loopEvent;

[Tooltip("Event to send if there is no hit at all")]
public FsmEvent noHitEvent;

[Tooltip("Event to send when there are no more hits to loop.")]
public FsmEvent finishedEvent;


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

loopEvent = null;
finishedEvent = null;
noHitEvent = null;
}

// cache the hits
private Object[] hits;

// increment a hit index as we loop through the hits
private int nextHitIndex;

public override void OnEnter()
{
if (nextHitIndex==0)
{
DoGetFileAll();
}

// no items found
if (hits.Length==0)
{
nextHitIndex = 0;
Fsm.Event(noHitEvent);
Fsm.Event(finishedEvent);
Finish();
return;
}

// final item
if (nextHitIndex>=hits.Length)
{
nextHitIndex = 0;
Fsm.Event(finishedEvent);
Finish();
return;
}

Debug.Log("getting index"+nextHitIndex );
fileName.Value = hits[nextHitIndex].name;

nextHitIndex++;

Fsm.Event(loopEvent);

Finish();
}


void DoGetFileAll()
{
hits = Resources.LoadAll(folderName.Value);
}

}
}