Hello. I made a panel that should appear with a transparency animation and close with a transparency animation
<engine:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:engine="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<Style src="project://database/Assets/UI%20Toolkit/MyPanelStyles.uss?fileID=7433441132597879392&guid=aab3325e9f53f4b0cb2fe74c5dce7609&type=3#MyPanelStyles" />
<engine:VisualElement name="MyPanel" class="my-panel">
</engine:VisualElement>
</engine:UXML>
.my-panel {
opacity: 0;
transition: opacity 0.2s ease;
}
.my-panel.visible {
opacity: 1;
}
public class MyPanelController : MonoBehaviour
{
public UIDocument MyPanelUIDocument { get; private set; }
private VisualElement rootVisualElement;
private void OnEnable()
{
MyPanelUIDocument = GetComponent<UIDocument>();
rootVisualElement = MyPanelUIDocument.rootVisualElement;
}
public void HidePanel()
{
var panelElement = rootVisualElement.Q<VisualElement>("MyPanel");
if (panelElement != null)
{
panelElement.RemoveFromClassList("visible");
Debug.Log($"Panel opacity before hiding: {panelElement.resolvedStyle.opacity}");
if (MyPanelUIDocument.enabled)
{
Debug.Log("Starting coroutine to disable UIDocument.");
StartCoroutine(DisableUIDocumentAfterDelay(0.2f)); // 200ms
}
}
}
private IEnumerator DisableUIDocumentAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
Debug.Log("Disabling UIDocument.");
MyPanelUIDocument.enabled = false;
}
public void ShowPanel()
{
MyPanelUIDocument.enabled = true;
var panelElement = rootVisualElement.Q<VisualElement>("MyPanel");
if (panelElement == null)
{
Debug.LogError("MyPanel not found.");
return;
}
if (panelElement.ClassListContains("visible"))
{
Debug.LogWarning("Panel is already visible.");
return;
}
panelElement.RemoveFromClassList("visible");
StartCoroutine(AddVisibleClassWithDelay(panelElement, 0.01f));
}
private IEnumerator AddVisibleClassWithDelay(VisualElement panelElement, float delay)
{
yield return new WaitForSeconds(delay);
Debug.Log("Adding 'visible' class to panel.");
panelElement.AddToClassList("visible");
}
}
UIDocument component is initially disabled on game object. I have two buttons, first calls ShowPanel(), the second calls HidePanel(). I start the game, press the first button, the document component is enabled, the panel appears with a opacity animation, I press the second, the panel disappears with a opacity animation, the document component is disabled. If I press the first button again, the document component turns on, but there is no opacity animation. Same problem when using method panelElement.styleSheets.Add(MyVisibleStyleSheet); and separate style file MyVisibleStyleSheet.uss.
I also use coroutines because this method when called again, the UIDocument component cannot be disabled when re-enabled in the ShowPanel():
public void HidePanel()
{
var panelElement = rootVisualElement.Q<VisualElement>("MyPanel");
if (panelElement != null)
{
panelElement.RemoveFromClassList("visible");
rootVisualElement.schedule.Execute(() =>
{
MyPanelUIDocument.enabled = false;
}).ExecuteLater(200);
}
}
Also there is some bug with MyPanelUIDocument.rootVisualElement. if I make any changes in the controller code, MyPanelUIDocument.rootVisualElement returns null on the first game launch after changing the code, if I re-launch the game without changes, rootVisualElement is not null.
private void OnEnable()
{
MyPanelUIDocument = GetComponent<UIDocument>();
rootVisualElement = MyPanelUIDocument.rootVisualElement;
MyButton = rootVisualElement.Q<Button>("MyButton");
}
Unity 6000.0.27.