Plugin focused event

fabio

Well-known member
Messages
70
Reactions
263
Points
53
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