LeanTween Unity 4.6 UI and Anonymous Functions

LeanTween Unity 4.6 UI and Anonymous Functions

Oct 13

Unity 4.6 may only be in beta but the new UI system has everyone excited. It basically brings the UI Elements behavior much more inline with how gameObjects are managed. Every element is an object in the scene hierarchy with it’s own RecTransform that describes it’s positioning/scaling/rotation etc (much like the Transform for GameObject).

Since RectTransform’s behave so similarly to regular Transform’s it was relatively trivial to add support. Right now move, rotate, scale are supported but I plan on offering other options in the future.

Anonymous Methods!
If you want to tween other values, there is a new option to easily tween a value inline using what are called anonymous functions. So you can tween the color of a button like so

// C#
LeanTween.value(gameObject, Color.red, Color.green, 1f ).setOnUpdate(
  (Color val)=>{
  UnityEngine.UI.Image image = (UnityEngine.UI.Image)button.gameObject.GetComponent( typeof(UnityEngine.UI.Image) );
  image.color = val;
  }
);

// Javascript
LeanTween.value(gameObject, Color.red, Color.green, 1f ).setOnUpdateColor(
  function(val:Color){
  var image:UnityEngine.UI.Image = button.gameObject.GetComponent( UnityEngine.UI.Image );
  image.color = val;
}
);

Leave a Reply