Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: MaDDoX on April 05, 2011, 06:10:41 PM

Title: Get Scale
Post by: MaDDoX on April 05, 2011, 06:10:41 PM
Hey it looks as I'm the first ;) C'mon coders, stop saving up stuff for yourselves and share the goodies! This is a little request from another thread, a missing lil' action to get the scale of a given game object. It returns the "lossy scale" if you use world, read about it here: http://unity3d.com/support/documentation/ScriptReference/Transform-lossyScale.html

To add it, just copy and paste the code below into a new C# script (I generally do it straight inside the Unity editor for consistency sake), name it as "GetScale.cs", save and you're done. It'll show up in the Transform category inside the action browser. Enjoy:

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Transform)]
    [Tooltip("Gets the Scale of a Game Object and stores it in a Vector3 Variable or each Axis in a Float Variable")]
    public class GetScale : FsmStateAction
    {
        [RequiredField]
        public FsmOwnerDefault gameObject;
        [UIHint(UIHint.Variable)]
        public FsmVector3 vector;
        [UIHint(UIHint.Variable)]
        public FsmFloat xScale;
        [UIHint(UIHint.Variable)]
        public FsmFloat yScale;
        [UIHint(UIHint.Variable)]
        public FsmFloat zScale;
        public Space space;
        public bool everyFrame;

        public override void Reset()
        {
            gameObject = null;
            vector = null;
            xScale = null;
            yScale = null;
            zScale = null;
            space = Space.World;
            everyFrame = false;
        }

        public override void OnEnter()
        {
            DoGetScale();

            if (!everyFrame)
                Finish();
        }

        public override void OnUpdate()
        {
            DoGetScale();
        }

        void DoGetScale()
        {
            GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
            if (go == null) return;

            Vector3 scale;

            if (space == Space.World)
            {
                scale = go.transform.lossyScale;
            }
            else
            {
                scale = go.transform.localScale;
            }

            vector.Value = scale;
            xScale.Value = scale.x;
            yScale.Value = scale.y;
            zScale.Value = scale.z;
        }


    }
}
Title: Re: Get Scale
Post by: tobbeo on April 05, 2011, 06:47:43 PM
Thank you!
Title: Re: Get Scale
Post by: Alex Chouls on April 06, 2011, 12:30:33 AM
Thanks MaDDoX!!  ;D
Title: Re: Get Scale
Post by: flekoun on April 06, 2011, 12:49:51 AM
Thanks a lot! Was looking for it!
Title: Re: Get Scale
Post by: Steamgauge on April 06, 2011, 08:08:01 AM
Nice one MaDDoX! Thank you!