• Hi Guest!

    Please be aware that we have released another critical security patch for VaM. We strongly recommend updating to version 1.22.0.12 using the VaM_Updater found in your installation folder.

    Details about the security patch can be found here.

Plugin focused event

fabio

Well-known member
Messages
78
Reactions
282
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