playMaker

Author Topic: Texture-related actions  (Read 4441 times)

vonchor

  • Guest
Texture-related actions
« on: September 07, 2011, 11:33:57 AM »
As I tried to use Textures in PM 1.2 beta, I created a few actions to add some capabilities I needed.

CopyTextureToTexture is pretty self-explanatory. To use LoadTextureFromResource and LoadAllTexturesFromResourceFolder you'll need to read up on using a Unity Resource Folder; but basically these let you load a texture from a folder named Resources in your project.

Code: [Select]
// by Jeff Sasmor aka Vonchor
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Material)]
[Tooltip("Copys one texture object to another texture object.")]
public class CopyTextureToTexture : FsmStateAction
{
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmTexture source;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmTexture destination;

public override void Reset()
{
source = null;
destination = null;
}

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

void DoCopy()
{
destination.Value = null;
destination.Value = source.Value;
}
}
}

and

Code: [Select]
// By Jeff Sasmor AKA Vonchor

using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Material)]
[Tooltip("Load a Texture into a Texture obj. Source is 'Resources' folder: use correct path!")]

//See unity docs for Resources.Load
public class LoadTextureFromResource : FsmStateAction
{
[RequiredField]
[Tooltip("use path into 'Resources' folder like 'cars/mustang'")]
public FsmString path;
[RequiredField]
public FsmTexture destination;

public override void Reset()
{
path = null;
destination = null;
}

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

void DoLoad()
{
destination.Value = (Texture2D) Resources.Load(path.Value,typeof(Texture2D));
if (destination.Value == null)
ActionHelpers.RuntimeError(this, "Missing file in texture load!");


}
}
}


and

Code: [Select]
//by Jeff Sasmor AKA Vonchor

using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Material)]
[Tooltip("Load all Textures in a resource folder. Source is 'Resources' folder: use correct path! Be sure number of destinations=items in resource folder!")]

//See unity docs for Resources.Load
public class LoadAllTexturesFromResourceFolder : FsmStateAction
{
[RequiredField]
[Tooltip("use path into 'Resources' folder like 'cars/sedans'")]
public FsmString pathToFolder;
[RequiredField]
[Tooltip("Ensure # destinations is at least as big as number of elements in the Resource Folder")]
public FsmTexture[] destinations;


public override void Reset()
{
pathToFolder = null;
destinations = new FsmTexture[3];
}

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

void DoLoad()
{
Object[] textures = Resources.LoadAll(pathToFolder.Value,typeof(Texture2D));
int numObjects = textures.Length;
int numDestinations = destinations.Length;

//note: using for rather than foreach so can test for overwrite of target in same statement
for (int i=0;i<numObjects && i<numDestinations;i++)
{
destinations[i].Value =  (Texture2D) textures[i];
}


}
}
}

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
Re: Texture-related actions
« Reply #1 on: September 07, 2011, 11:39:41 AM »
Great stuff!!!!

Thanks for sharing!!

Q

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
Re: Texture-related actions
« Reply #2 on: September 07, 2011, 01:53:11 PM »
Seconded, thanks for sharing!