0

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.

9
  • What's wrong with just a "using ARCoreBackgroundRenderer;"? Commented Dec 13, 2019 at 0:44
  • @BernieG. The type or namespace name 'ARCoreBackgroundRenderer' could not be found (are you missing a using directive or an assembly reference?) because its in another namespace. Commented Dec 13, 2019 at 1:07
  • Did you read my question? Put "using ARCoreBackgroundRenderer;" at the top of your code. Commented Dec 13, 2019 at 1:09
  • I did. but using that is producing that error. I wrote the error as well! I can not use that using. because it is in another namespace! Commented Dec 13, 2019 at 1:12
  • That's the point of a using statement... to import another namespace. Is the specified namespace/class included in your solution/project? Commented Dec 13, 2019 at 1:13

1 Answer 1

3

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

When I type using GoogleARCore it shows me that error. I can't call that namespace.
did you go through all steps of Quickstart for Android or Quickstart for iOS?
I did actually, but here I can not call using GoogelARCore to use that namespace. it tells me The type or namespace name GoogleArCore could not be found (are you missing a using directive or an assembly reference?
It is not suggesting to me using that namespace.
No but it continues in the Enable ARCore and finally reaches the API which clearly says that the type is GoogleARCore.ARCoreBackgroundRenderer which means you need this namespace. If it isn't recognized correctly something is wrong with the steps before. Check if everything is imported and in your buildsettings if you are on the correct target platform ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.