Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: MaDDoX on April 18, 2011, 04:31:23 PM

Title: Set Visibility
Post by: MaDDoX on April 18, 2011, 04:31:23 PM
Edited: New version now has a "recursive" option, which allows you to set all children objects of the selected Game Object visible/invisible as well.

This is one of those actions which, before I decided to make, I've scratched my head and thought to myself: "wait, there must be something like this already in Playmaker, I'm just being blind!". Well, aside from disabling the whole object (which might not be what you want) or trying to disable the component (and btw meshrenderer isn't listed), only thing I found was blink. Which is not only not very straightforward for casual visibility toggling usage but, to be frank, I never found a way to disable once turned on heh :) Just to be on the safe side that I wasn't going to be redundant, I've added some nice functionality to this action:

1 - Visible: Basic on/off toggle, can be set to a boolean variable.
2 - Toggle: If you set "toggle" the action will reverse the assigned gameobject visibility. This setting overrides the 'visible' setting.
3 - Reset On Exit: Reverts the visibility to the original visibility of the moment the state was entered.

BTW, the Reset On Exit option gave me some headache, it wasn't as obvious to code in as I initially thought it'd be. Anyways, it's working fine and it's a big help if you're trying to make one object visible or invisible just throughout the duration of a state.

Well, there you go, hope it's of help to you guys!

PS.: To install it just create a new c# script inside your project, preferrably inside Assets/playmaker/actions, rename it to 'SetVisibility' (without the quotes), double click to open it and paste the attached code.

PS/2: Hey, we've got a better 'code' font here in the forum! Great :)

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Material)]
    [Tooltip("Sets or toggle the visibility on a game object.")]
public class SetVisibility : FsmStateAction
{
[RequiredField]
//[CheckForComponent(typeof(Renderer))]
public FsmOwnerDefault gameObject;
        //[UIHint(UIHint.Variable)]
        [Tooltip("Should the object visibility be toggled?\nHas priority over the 'visible' setting")]
        public FsmBool toggle;
//[UIHint(UIHint.Variable)]
        [Tooltip("Should the object be set to visible or invisible?")]
        public FsmBool visible;
        [Tooltip("Resets to the initial visibility once\nit leaves the state")]
        public bool resetOnExit;
        public bool recursive;
        private bool initialVisibility;

public override void Reset()
{
gameObject = null;
            toggle = false;
visible = false;
            resetOnExit = true;
            initialVisibility = false;
    recursive = true;
}

public override void OnEnter()
{
            if (gameObject.OwnerOption == OwnerDefaultOption.UseOwner)
                DoSetVisibility(Owner);
            else
                DoSetVisibility(gameObject.GameObject.Value);
           
            Finish();
}

        void DoSetVisibility(GameObject go)
{

if (go == null) return;

            var renderers = go.GetComponentsInChildren<Renderer>();

            // 'memorizes' initial visibility
            bool isRootRenderer = (go.GetComponent<Renderer>() != null);

            if (isRootRenderer)
                initialVisibility = go.renderer.isVisible;
            else
                initialVisibility = false;               

            // if 'toggle' is not set, simply sets visibility to new value
            if (toggle.Value == false) {
                if (isRootRenderer)
                    go.renderer.enabled = visible.Value;
               
                if (!recursive) return;
                foreach (var renderer in renderers) {
                    renderer.enabled = visible.Value;
                }
                return;
            }

            // otherwise, toggles the visibility
            if (isRootRenderer)
                go.renderer.enabled = !go.renderer.isVisible;

            if (!recursive) return;
            foreach (var renderer in renderers) {
                renderer.enabled = visible.Value;
            }
            return;
        }

        public override void OnExit()
        {
            if (resetOnExit)
                ResetVisibility();
        }

        void ResetVisibility()
        {
            // uses the FSM to get the target object and resets its visibility
            GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
            if (go != null && go.renderer != null)
                go.renderer.enabled = initialVisibility;
        }

}
}
Title: Re: Set Visibility
Post by: tobbeo on April 19, 2011, 07:53:51 PM
Thanks MaDDoX, much appreciated!
Title: Re: Set Visibility
Post by: MaDDoX on April 19, 2011, 09:10:55 PM
NP, more in the oven :)

Tell me if you find it useful, due to the way I do vector (2.5D) animations inside Unity I'm using it a *lot* as a "frame" (actually object) switcher. Will probably extend it to some higher-level switch siblings visibility in the near future.
Title: Re: Set Visibility
Post by: paul.harden on April 27, 2011, 08:26:32 AM
Wow! it was exactly what I was looking for! Thank You MaDDoX!

It would be very nice if we could also point to a group and show/hide it's contents instead
of acting on the individual meshes, could save some time and, yeah.. what about showing/hiding
objects by Tag?
Title: Re: Set Visibility
Post by: MaDDoX on April 27, 2011, 04:38:31 PM
Thanks Paul, I appreciate ;) As for "pointing to a group" do you mean with an array of objects or by applying the visibility setting to every child of a certain object? Both would be interesting tbh, just like your "by tag" idea, although I think it would be best applied to a separate action. Adding it to my to-do list!
Title: Re: Set Visibility
Post by: vonchor on July 25, 2011, 04:52:55 PM
 :D Instant Gratification and Salutations to you!  I was scratching my head trying to figure out why this isn't available in the stock Playmaker....
Title: Re: Set Visibility
Post by: joeyjr on August 09, 2011, 10:52:11 AM
Thank you for this Action!  This is one that is definitely going to come in handy! 
:)
Title: Re: Set Visibility
Post by: rocket5 on August 29, 2011, 10:41:27 PM
Thanks, MaDDoX this is just what I was looking for.  I'm really surprised there isn't a built in Action to enable/disable the Renderer in Playmaker and I don't see one listed in the 1.2 update.
Title: Re: Set Visibility
Post by: Andrew.Lukasik on September 04, 2011, 07:08:16 PM
Thx! I was looking for this
Title: Re: Set Visibility
Post by: MaDDoX on September 22, 2011, 03:13:22 PM
Thanks for the nice word guys :) This is a very very useful action indeed, I've updated it recently to a more robust version which can enable/disabled visibility of children items as well. Hope it's useful, grab it from the initial post.
Title: Re: Set Visibility
Post by: tobbeo on September 22, 2011, 05:14:16 PM
Thanks for the update Maddox, I use this all the time!
Title: Re: Set Visibility
Post by: michela on November 28, 2011, 07:35:28 AM
Is there a trick to getting Set Visibility to work on an object with an FSM attached?

I'm trying to hide an object (not deactivate it) containing a Draw Texture action and there doesn't appear to be a way of referencing the Playmaker created state. I get an error about there not being a renderer attached to the object.  I tried adding a Mesh Renderer but that's nothing to do with Playmaker I can see.

Cheers

Michela
Title: Re: Set Visibility
Post by: qholmes on November 28, 2011, 10:49:03 AM
Well the Draw Texture Action has to be active to actually draw the texture so all you need to do is change to a different State. Set up some logic that sends an event to go to a different state and the Draw Texture will no longer draw. If you want to animate the leaving then switch to a state where it is drawing the same texture but use iTween actions to animate the values in the Draw Texture Action. Then when it is done switch to one that is not drawing the texture.

Just my thoughts on how i would do it.

Q
Title: Re: Set Visibility
Post by: wheretheidivides on December 10, 2014, 11:31:53 PM
"Edited: New version now has a "recursive" option, which allows you to set all children objects of the selected Game Object visible/invisible as well."


I do not see the recursive option.  Am I missing something?
Title: Re: Set Visibility
Post by: MaDDoX on January 10, 2015, 12:21:24 PM
Quote
I do not see the recursive option.  Am I missing something?
Yup, you're missing the new version :) The stock version is the older one, simply edit SetVisibility.cs (with MonoDevelop, Notepad or whatever text editor you use) and overwrite the text with what's in the first topic code block.

Maybe Alex or Jean would be kind enough to update the official version?

PS.: I've just tested it and it works perfectly, even with the new 4.6 UI Sprite Renderers. It's lovely to realize you wrote code years ago which stood the test of time ;)
Title: Re: Set Visibility
Post by: wheretheidivides on January 11, 2015, 08:45:37 PM
cool.  thanks.
Title: Re: Set Visibility
Post by: jeanfabre on January 12, 2015, 03:07:31 AM
Hi,

 I pushed it to the Ecosystem (https://hutonggames.fogbugz.com/default.asp?W1181) for convenience, I named it "SetVisibility2" to avoid future conflicts as well. If Alex implements it in the original action then I'll remove it.

you can download it directly below as well:

SetVisibility2 (https://github.com/jeanfabre/PlayMakerCustomActions_U3/raw/b2d1f9ee3abae918961aabeaf4374a9842b242ab/Assets/PlayMaker%20Custom%20Actions/Material/SetVisibility2.cs)

 Bye,

 Jean