Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: amaranth on February 13, 2013, 12:03:44 AM

Title: Get File Name By Loop
Post by: amaranth 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:


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);
}

}
}