playMaker

Author Topic: Get File Count  (Read 6052 times)

amaranth

  • Full Member
  • ***
  • Posts: 172
Get File Count
« on: February 12, 2013, 11:49:53 PM »
This script returns the number of files 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: Folder 1/Subfolder 1
  • File Count: returns the number of files in the specified directory.

Resources
-file 1
-file 2
-Folder 1
--file 1
--file 2
--Subfolder 1
---file 1

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 number of files in a resource directory.")]
public class GetFileCount : FsmStateAction
{

[Tooltip("The name of the folder.")]
public FsmString folderName;

[Tooltip("The number of files in the folder")]
[UIHint(UIHint.Variable)]
public FsmInt fileCount;


public override void Reset()
{
folderName = null;
fileCount = 0;
}

public override void OnEnter()
{
DoGetFileCount();

Finish();
}


void DoGetFileCount()
{
Object[] resourceDir = Resources.LoadAll(folderName.Value);
fileCount.Value = resourceDir.Length;
}

}
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15501
  • Official Playmaker Support
Re: Get File Count
« Reply #1 on: February 26, 2014, 01:11:44 PM »
Hi,

 Warning with this action, it does indeed LOAD all your resources, sot it can be quite demanding for the memory especially if on mobile.

bye,

 Jean

MS80

  • Junior Playmaker
  • **
  • Posts: 63
Re: Get File Count
« Reply #2 on: September 21, 2015, 08:45:52 AM »
How could I get the file count of a folder outside the resoures?? I need to get the file count and file names from a specific directory (f.e. C:/Game/Pictures/)! Then I would like to use this information with www action to load images from that directory...

I searched the ecosystem but it seems there is no action which could this right now?!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15501
  • Official Playmaker Support
Re: Get File Count
« Reply #3 on: September 25, 2015, 08:07:18 AM »
Hi,

 I made an action to do this, it's on the [img=http://j.mp/1Esn1mF]http://Ecosystem[/img]:

GetFileCount

I also put the action originally coming from this thread.

ResourcesCount

Bye,

 Jean

MS80

  • Junior Playmaker
  • **
  • Posts: 63
Re: Get File Count
« Reply #4 on: September 26, 2015, 04:27:37 PM »
Thank you Jean!

As you mentioned somewhere before, using "Resources" is a faster and more comfortable. I want to use both, load external (www object) and internal (resources load) files (sprites). Did not know about "ResourcesCount", this was one I was searching, too! :) I would like to count images in folders - internal or external - and after that store those images in a array!

I have a few problems so far, maybe you have a idea or maybe there already exists a action for it...

- "ResourcesCount" counts a sprite as 2 assets (image and its sprite)
- "Get File Name by Index" is a great action, unfortunatly it has the same issue like "ResourcesCount" as it gets 2 filenames per sprite!
- Is there a similar action like "Get File Name by Index" for external folders (not resources)?

Thanks for you help!!!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15501
  • Official Playmaker Support
Re: Get File Count
« Reply #5 on: September 28, 2015, 07:26:43 AM »
Hi,

 ok, I made two new actions:

GetFileInfoByIndex
ConvertBytesLengthToReadableSize ( to get nice file sizes :) )

Bye,

 Jean

MS80

  • Junior Playmaker
  • **
  • Posts: 63
Re: Get File Count
« Reply #6 on: September 28, 2015, 05:13:31 PM »
These are great Jean, thank you so much!
With these and the other actions you posted before everything is working.

Only one small issue with loading sprites from resources is left: "Get File Name by Index" work perfectly but give me the double count of sprites names in a existing resource directory. Using textures works like expected, but if I go through the sprite names per index every sprite name exists two times?!?

Is there a workaround for this issue or is it my mistake??

PS: it works if I add "typeof (Sprite)"

      void DoGetFileName()
      {
         Object[] resourceDir = Resources.LoadAll(folderName.Value, typeof(Sprite));
         fileName.Value = resourceDir[fileIndex.Value].name;
      }
« Last Edit: September 28, 2015, 06:38:42 PM by MS80 »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15501
  • Official Playmaker Support
Re: Get File Count
« Reply #7 on: September 29, 2015, 02:07:56 AM »
Hi,

 I am not sure there is a fix unless we make this action defining the type you want to get the number of items from.

 Would that do?

 We could make a getResourceCount given a list of types.

 I would however stress at this point that loading resources to get their type is really something you should be careful of and aware this is creating issues with memory consumption. It's only manageable up to a certain number of resources.

 I would strongly suggest you maintain a text based list and use xml or a arrayMaker to parse that list and get an abstract representation of what's available. Yes you loose the automated process, but if you publish on mobile, you may find yourself cornered as your project grows.


Bye,

 Jean

MS80

  • Junior Playmaker
  • **
  • Posts: 63
Re: Get File Count
« Reply #8 on: September 29, 2015, 04:44:27 AM »
I am not sure there is a fix unless we make this action defining the type you want to get the number of items from.

 Would that do?

Yeah, this would do the job! I tried it but failed to define a list of types variable...

I would however stress at this point that loading resources to get their type is really something you should be careful of and aware this is creating issues with memory consumption. It's only manageable up to a certain number of resources.

 I would strongly suggest you maintain a text based list and use xml or a arrayMaker to parse that list and get an abstract representation of what's available. Yes you loose the automated process, but if you publish on mobile, you may find yourself cornered as your project grows.
Thanks for pointing this out in detail. Yes, I just go this way because of the automation, beeing lazy has its price. I used DataMaker with great success in other projects, just wanted to try some kind of automation  :)
Luckily I do not need it for mobile, anyway I will use it with care.
« Last Edit: September 29, 2015, 04:53:12 AM by MS80 »