Hefaz
Hefaz

Reputation: 671

Access public method from another namespace in Unity

I am trying to access a public method from the ARCoreBackgroundRenderer class which is a MonoBehavior and is located in GoogleARCore namespace, in another class which is also MonoBehavior and named UIController and is located in another namespace called CompanyController. Is this possible? How can I do it?

the method I am trying to call in my second class is:

public void DisableARBackgroundRendering()
    {
        if (m_CommandBuffer == null || m_Camera == null)
        {
            return;
        }

        m_Camera.clearFlags = m_CameraClearFlags;

        .......

    }

I want to call this method in my second Class in a simple method.

Upvotes: 0

Views: 3593

Answers (1)

derHugo
derHugo

Reputation: 90714

In general it helps to refer to the according API


ARCoreBackgroundRenderer (API Reference) as you said yourself is a class in the namespace GoogleARCore .. so import it via

using GoogleARCore;

or use

GoogleARCore.ARCoreBackgroundRenderer

as type in your code in UIController.

using UnityEngine;
using GoogleARCore;

namespace CompanyController
{
    public class UIController : MonoBehaviour
    {
        // Reference this via the Inspector by drag and drop 
        // [SerializeField] simply allows to serialize also private fields in Unity
        [SerializeField] private ARCoreBackgroundRenderer arRenderer;

        // Alternatively you could as said use the type like
        //[SerializeField] private GoogleARCore.ARCoreBackgroundRenderer arRenderer;

        private void Awake ()
        {
            // As a fallback find it on the scene
            if(!arRenderer) arRenderer = FindObjectOfType<ARCoreBackgroundRenderer>();
        }

        public void DisableARBackgroundRendering()
        {
            // Now use any public method of the arRenderer 
            arRenderer.SomePublicMethod();
        }
    }
}

However, you can also see that the method DisableARBackgroundRendering is private and you won't be able to use it. Also m_Camera and m_CommandBuffer are both private so you won't be able to access them.

What you can and - if you look closer into the implementation of ARBackgroundRenderer - want to do here is simply enabling and disabling the according component:

public void EnableARBackgroundRendering(bool enable)
{
    arRenderer.enabled = enable;
}

since internally it will take care of the rest itself and you don't need any further access to its methods, fields and properties:

private void OnEnable()
{
    if (BackgroundMaterial == null)
    {
        Debug.LogError("ArCameraBackground:: No material assigned.");
        return;
    }

    LifecycleManager.Instance.OnSessionSetEnabled += _OnSessionSetEnabled;

    m_Camera = GetComponent<Camera>();

    m_TransitionImageTexture = Resources.Load<Texture2D>("ViewInARIcon");
    BackgroundMaterial.SetTexture("_TransitionIconTex", m_TransitionImageTexture);

    EnableARBackgroundRendering(); // AS YOU SEE IT ALREADY CALLS THIS ANYWAY
}

private void OnDisable()
{
    LifecycleManager.Instance.OnSessionSetEnabled -= _OnSessionSetEnabled;
    m_TransitionState = BackgroundTransitionState.BlackScreen;
    m_CurrentStateElapsed = 0.0f;

    m_Camera.ResetProjectionMatrix();

    DisableARBackgroundRendering(); // AS YOU SEE IT ALREADY CALLS THIS ANYWAY
}

Upvotes: 3

Related Questions