• Hi Guest!

    We have posted a new VaM2 dev log on Patreon, starting a monthly cadence of written progress updates between Beta releases. Highlights include the new Gizmos System, Selection Carousel, and Modes System with Context-Specific Editing. Beta1.2 is 15 of 21 items complete.

    Read the full post on Patreon, or follow progress on the public Trello roadmap.

Plugin focused event

fabio

Well-known member
Joined
Jan 30, 2021
Messages
77
Reactions
288
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