Hello!
I've done a new action. It counts the number of a certain color pixels in a texture. Any changes or suggestions to improve performance or functionality it are more than welcome!
(Note: part of the code I have taken from
https://hutonggames.com/playmakerforum/index.php?topic=11929.0 )
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("ArrayMaker/ArrayList")]
[Tooltip("Count the number of pixels of a certain color")]
[HelpUrl("http://hutonggames.com/playmakerforum/index.php?topic=11929.0")]
public class CountColoredPixelsInTextures : ArrayListActions
{
[ActionSection("Input")]
public FsmTexture texture;
public FsmColor colorToCount;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmInt storeResult;
[ActionSection("Event")]
[UIHint(UIHint.FsmEvent)]
public FsmEvent doneEvent;
[UIHint(UIHint.FsmEvent)]
[Tooltip("The event if error")]
public FsmEvent failureEvent;
private Color[] pix;
private Texture2D targetmaskedTexture;
public override void Reset()
{
storeResult = null;
failureEvent = null;
doneEvent= null;
texture = null;
}
public override void OnEnter()
{
bool noError = false;
if (texture.IsNone){
Debug.LogWarning("<color=#6B8E23ff>No texture. Please review!</color>", this.Owner);
noError = true;
}
targetmaskedTexture = null;
targetmaskedTexture = texture.Value as Texture2D;
try
{
targetmaskedTexture.GetPixel(0, 0);
}
catch(UnityException)
{
Debug.LogWarning("<color=#6B8E23ff>Please enable read/write on texture </color>"+"[" + texture.Value.name + "]", this.Owner);
noError = true;
}
if (noError == true){
Fsm.Event(failureEvent);
return;
}
else {
storeResult.Value = 0;
pix = targetmaskedTexture.GetPixels(0);
for (int i = 0; i < pix.Length; i++)
{
if (colorToCount.Value == pix[i]) {
storeResult.Value = storeResult.Value + 1;
}
}
pix = new Color[0];
}
Fsm.Event(doneEvent);
return;
}
}
}