playMaker

Author Topic: Set Visibility  (Read 12680 times)

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • View Profile
    • FluidPlay Studios
Set Visibility
« on: April 18, 2011, 01: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;
        }

}
}
« Last Edit: September 22, 2011, 12:19:23 PM by MaDDoX »
--
Breno "MaDDoX" Azevedo
@brenoazevedo

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
    • View Profile
Re: Set Visibility
« Reply #1 on: April 19, 2011, 04:53:51 PM »
Thanks MaDDoX, much appreciated!

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • View Profile
    • FluidPlay Studios
Re: Set Visibility
« Reply #2 on: April 19, 2011, 06: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.
--
Breno "MaDDoX" Azevedo
@brenoazevedo

paul.harden

  • Playmaker Newbie
  • *
  • Posts: 6
    • View Profile
Re: Set Visibility
« Reply #3 on: April 27, 2011, 05: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?

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • View Profile
    • FluidPlay Studios
Re: Set Visibility
« Reply #4 on: April 27, 2011, 01: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!
--
Breno "MaDDoX" Azevedo
@brenoazevedo

vonchor

  • 1.2 Beta
  • Playmaker Newbie
  • *
  • Posts: 28
    • View Profile
Re: Set Visibility
« Reply #5 on: July 25, 2011, 01: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....

joeyjr

  • 1.2 Beta
  • Playmaker Newbie
  • *
  • Posts: 12
    • View Profile
Re: Set Visibility
« Reply #6 on: August 09, 2011, 07:52:11 AM »
Thank you for this Action!  This is one that is definitely going to come in handy! 
:)

rocket5

  • Playmaker Newbie
  • *
  • Posts: 4
    • View Profile
    • Rocket 5 Studios
Re: Set Visibility
« Reply #7 on: August 29, 2011, 07: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.

Andrew_Raphael_Lukasik

  • Full Member
  • ***
  • Posts: 134
    • View Profile
    • my twitter @andrewlukasik
Re: Set Visibility
« Reply #8 on: September 04, 2011, 04:08:16 PM »
Thx! I was looking for this

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • View Profile
    • FluidPlay Studios
Re: Set Visibility
« Reply #9 on: September 22, 2011, 12: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.
--
Breno "MaDDoX" Azevedo
@brenoazevedo

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
    • View Profile
Re: Set Visibility
« Reply #10 on: September 22, 2011, 02:14:16 PM »
Thanks for the update Maddox, I use this all the time!

michela

  • Playmaker Newbie
  • *
  • Posts: 4
    • View Profile
Re: Set Visibility
« Reply #11 on: November 28, 2011, 05: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

qholmes

  • 1.2 Beta
  • Hero Member
  • *
  • Posts: 509
    • View Profile
Re: Set Visibility
« Reply #12 on: November 28, 2011, 08: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

wheretheidivides

  • Sr. Member
  • ****
  • Posts: 479
    • View Profile
Re: Set Visibility
« Reply #13 on: December 10, 2014, 09: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?

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • View Profile
    • FluidPlay Studios
Re: Set Visibility
« Reply #14 on: January 10, 2015, 10:21:24 AM »
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 ;)
--
Breno "MaDDoX" Azevedo
@brenoazevedo