• Hi Guest!

    We are extremely excited to announce the release of our first Beta1.1 and the first release of our Public AddonKit!
    To participate in the Beta, a subscription to the Entertainer or Creator Tier is required. For access to the Public AddonKit you must be a Creator tier member. Once subscribed, download instructions can be found here.

    Click here for information and guides regarding the VaM2 beta. Join our Discord server for more announcements and community discussion about VaM2.
  • Hi Guest!

    VaM2 Resource Categories have now been added to the Hub! For information on posting VaM2 resources and details about VaM2 related changes to our Community Forums, please see our official announcement here.

Plugin focused event

fabio

Well-known member
Joined
Jan 30, 2021
Messages
78
Reactions
286
Hi. I wonder if it is possible to implement an event that is called every time the plugin is displayed/focused/activated on the screen. I would like to update a plugin each time it receives focus (I just want to get rid of an eventual Sync button if possible).

MonoBehavior has an OnBecameVisible() event but isn't being called. Any alternative?
 
You can detect when the UI transform is enabled/disabled:


C#:
using UnityEngine;
using System;

class UnityEventsListener : MonoBehaviour
{
    public bool IsEnabled { get; private set; }
    public Action enabledHandlers;
    public Action disabledHandlers;

    private void OnEnable()
    {
        IsEnabled = true;
        enabledHandlers?.Invoke();
    }

    private void OnDisable()
    {
        IsEnabled = false;
        disabledHandlers?.Invoke();
    }
}

// MVRScript:

private UnityEventsListener _pluginUIEventsListener;

public override void InitUI()
{
    base.InitUI();
    if(this.UITransform == null)
    {
        return;
    }

    if(_pluginUIEventsListener == null)
    {
        _pluginUIEventsListener = UITransform.gameObject.AddComponent<UnityEventsListener>();
        _pluginUIEventsListener.enabledHandlers += OnUIEnabled;
        _pluginUIEventsListener.disabledHandlers += OnUIDisabled;
    }
}

private void OnUIEnabled()
{
    // ...
}

private void OnUIDisabled()
{
    // ...
}

private void OnDestroy()
{
    if(_pluginUIEventsListener != null)
    {
        UnityEngine.Object.DestroyImmediate(_pluginUIEventsListener);
        _pluginUIEventsListener = null;
    }
}
 
Last edited:
Back
Top Bottom