playMaker

Author Topic: Tk2d Sprite Set Color To Children  (Read 1916 times)

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Tk2d Sprite Set Color To Children
« on: August 26, 2015, 04:06:07 PM »
Based on the script of setting material color to children here is a the same but setting all sprites to a color.

When finished it will fire off a finish so you can use the finished event.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.

using UnityEngine;
using System.Collections;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory("2D Toolkit/Sprite")]
    [Tooltip("Set the color of a sprite. \nNOTE: The Game Object must have a tk2dBaseSprite or derived component attached ( tk2dSprite, tk2dAnimatedSprite)")]
    public class Tk2dSpriteSetColorToChildren : FsmStateAction
    {
        [RequiredField]
        [Tooltip("The Game Object to work with. NOTE: The Game Object must have a tk2dBaseSprite or derived component attached ( tk2dSprite, tk2dAnimatedSprite).")]
 
        public FsmOwnerDefault gameObject;

        [Tooltip("The color")]
        [UIHint(UIHint.FsmColor)]
        public FsmColor color;

        [ActionSection("")]

        [Tooltip("Repeat every frame.")]

        private Hashtable _originalColors;
        private tk2dBaseSprite _sprite;

        private void _getSprite()
        {
            GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
            if (go == null)
            {
                return;
            }

            _sprite = go.GetComponent<tk2dBaseSprite>();
        }


        public override void Reset()
        {
            gameObject = null;
            color = null;
        }

        public override void OnEnter()
        {
            _getSprite();
            GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
            ApplyColorToHierarchy(go.transform);

        }

        public override void OnUpdate()
        {
            GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
            ApplyColorToHierarchy(go.transform);
        }

        void ApplyColorToHierarchy(Transform parent)
        {
            DoSetSpriteColor(parent.gameObject);

            for (int i = 0; i < parent.childCount; i++)
            {
                ApplyColorToHierarchy(parent.GetChild(i));
                if(i == parent.childCount - 1)
                    {
                        Finish();
                    }
            }
        }

        void DoSetSpriteColor(GameObject go)
        {
            _sprite = go.GetComponent<tk2dBaseSprite>();
            if (_sprite == null)
            {
                LogWarning("Missing tk2dBaseSprite component");
                return;
            }

            if (_sprite.color != color.Value)
            {
                _sprite.color = color.Value;
            }
        }


    }
}
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez