Hi All,
My first "working" custom action. Basically used already in place iTween actions to generate an action which tweens the alpha of an object over time.
E.g where this could be used in a game, is say a coin pick up, rather than disappearing instantly on pick up fade out over desired time.
Hope it helps someone.
Note: For this to work the object material renderer must support transparency.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("iTween")]
[Tooltip("Varies GameObject's Alpha over time by fading.")]
public class iTweenAlphaBy: iTweenFsmAction
{
[RequiredField]
public FsmOwnerDefault gameObject;
[Tooltip("iTween ID. If set you can use iTween Stop action to stop it by its id.")]
public FsmString id;
[RequiredField]
[Tooltip("A Alpha value that GameObject will tween to.")]
public FsmFloat Alpha;
[Tooltip("The time in seconds the animation will take to complete.")]
public FsmFloat time;
[Tooltip("The time in seconds the animation will wait before beginning.")]
public FsmFloat delay;
[Tooltip("Can be used instead of time to allow animation based on speed. When you define speed the time variable is ignored.")]
public FsmFloat speed;
[Tooltip("The shape of the easing curve applied to the animation.")]
public iTween.EaseType easeType = iTween.EaseType.linear;
[Tooltip("The type of loop to apply once the animation has completed.")]
public iTween.LoopType loopType = iTween.LoopType.none;
public override void Reset()
{
base.Reset();
id = new FsmString{UseVariable = true};
time = 1f;
delay = 0f;
loopType = iTween.LoopType.none;
Alpha = new FsmFloat { UseVariable = true };
speed = new FsmFloat { UseVariable = true };
}
public override void OnEnter()
{
base.OnEnteriTween(gameObject);
if(loopType != iTween.LoopType.none) base.IsLoop(true);
DoiTween();
}
public override void OnExit(){
base.OnExitiTween(gameObject);
}
void DoiTween()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;
// init position
float amount = 0f;
if (Alpha.IsNone) {
} else {
amount = Alpha.Value;
}
itweenType = "alpha";
iTween.FadeTo(go, iTween.Hash(
"amount", amount,
"name", id.IsNone ? "" : id.Value,
speed.IsNone ? "time" : "speed", speed.IsNone ? time.IsNone ? 1f : time.Value : speed.Value,
"delay", delay.IsNone ? 0f : delay.Value,
"easetype", easeType,
"looptype", loopType,
"oncomplete", "iTweenOnComplete",
"oncompleteparams", itweenID,
"onstart", "iTweenOnStart",
"onstartparams", itweenID,
"ignoretimescale", realTime.IsNone ? false : realTime.Value
));
}
}
}