Forum rules - please read before posting.

Canvas Visibility

I have 2 canvases in my heirarchy. Both of them have a remember visibility component on them. One of them is set to on and the other off. I have a button that's supposed to turn one of the canvases off and the other on when clicked but the issue is that both canvases show when the game starts. I don't know if that's how unity does it but is there a way to not have both canvases showing when the game starts? without using menus?

Comments

  • edited September 2023

    The Remember Visibility component won't record the state of Canvases, and disabling one will prevent it from being accessable by the save system.

    To control a Canvas's visibility so that the save system can record it, attach a Canvas Group component and set its Alpha value to 0/1 to hide/show it. The Interactable option can be used to make it interative / non-interactive.

    Using AC's provided save components, saving would be a case of animating the above values, and then making use of the Remember Animator component. That is, you'd attach an Animator that has an "On" state and an "Off" state, each of which affect the Canvas Group's Alpha and Interactable values accordingly. The Object: Animate Action would then be used to control which animation is played, which the Remember Animator component will record.

    Alternatively, you can make use of a custom Remember component that directly saves the state of the Canvas Group. A tutorial on this topic can be found here, but something along these lines ought to do it:

    using UnityEngine;
    using AC;
    
    [RequireComponent (typeof (CanvasGroup))]
    public class RememberCanvasGroup : Remember
    {
    
        public void TurnOn ()
        {
            CanvasGroup canvasGroup = GetComponent<CanvasGroup> ();
            canvasGroup.interactable = true;
            canvasGroup.alpha = 1f;
        }
    
        public void TurnOff ()
        {
            CanvasGroup canvasGroup = GetComponent<CanvasGroup> ();
            canvasGroup.interactable = false;
            canvasGroup.alpha = 0f;
        }
    
        public override string SaveData ()
        {
            CanvasGroupData data = new CanvasGroupData ();
            data.objectID = constantID;
            data.savePrevented = savePrevented;
    
            CanvasGroup canvasGroup = GetComponent<CanvasGroup> ();
            data.alpha = canvasGroup.alpha;
            data.isInteractable = canvasGroup.interactable;
    
            return Serializer.SaveScriptData <CanvasGroupData> (data);
        }
    
    
        public override void LoadData (string stringData)
        {
            CanvasGroupData data = Serializer.LoadScriptData <CanvasGroupData> (stringData);
            if (data == null) return;
            SavePrevented = data.savePrevented; if (savePrevented) return;
    
            CanvasGroup canvasGroup = GetComponent<CanvasGroup> ();
            canvasGroup.alpha = data.alpha;
            canvasGroup.interactable = data.isInteractable;
        }
    
    }
    
    [System.Serializable]
    public class CanvasGroupData : RememberData
    {
    
        public bool isInteractable;
        public float alpha;
    
        public CanvasGroupData () { }
    
    }
    
  • If I go the script route, will there be a way to control the state of the canvas groups from action lists? Like the object: visibility action? or would I still need to use the object: animate method you suggested?
  • You'd need to similarly use scripting. I've added a pair of TurnOn / TurnOff functions to the above script - you can use either the Object: Call event or Object: Send message Action to run them in ActionLists.

  • I used the Object: Send message Action and it runs perfectly! Thank you!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.