【问题标题】:Access public method from another namespace in Unity从 Unity 中的另一个命名空间访问公共方法
【发布时间】:2020-04-06 10:19:48
【问题描述】:

我正在尝试从 ARCoreBackgroundRenderer 类访问公共方法,该类是 MonoBehavior,位于 GoogleARCore 命名空间中,在另一个也是 MonoBehavior 的类中,名为 UIController,位于另一个名为 @ 的命名空间中987654324@。这可能吗?我该怎么做?

我在第二堂课中尝试调用的方法是:

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

        m_Camera.clearFlags = m_CameraClearFlags;

        .......

    }

我想用一个简单的方法在我的第二个 Class 中调用这个方法。

【问题讨论】:

  • “使用 ARCoreBackgroundRenderer;”有什么问题?
  • @BernieG。 The type or namespace name 'ARCoreBackgroundRenderer' could not be found (are you missing a using directive or an assembly reference?) 因为它在另一个命名空间中。
  • 您阅读我的问题了吗?把“使用 ARCoreBackgroundRenderer;”在代码的顶部。
  • 我做到了。但使用它会产生该错误。我也写了错误!我不能使用它。因为它在另一个命名空间中!
  • 这就是 using 语句的重点......导入另一个命名空间。指定的命名空间/类是否包含在您的解决方案/项目中?

标签: c# unity3d namespaces


【解决方案1】:

一般来说,参考API会有所帮助


ARCoreBackgroundRenderer (API Reference) 正如你所说自己是命名空间GoogleARCore 中的一个 .. 所以通过

导入它
using GoogleARCore;

或使用

GoogleARCore.ARCoreBackgroundRenderer

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();
        }
    }
}

但是,您也可以看到方法DisableARBackgroundRenderingprivate,您将无法使用它。另外m_Cameram_CommandBuffer 都是private,因此您将无法访问它们。

可以和 - 如果您仔细研究 ARBackgroundRenderer 的实现 - 想要在这里做的只是启用和禁用相应的组件:

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

因为它会在内部自行处理其余部分,您不需要进一步访问它的方法、字段和属性:

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
}

【讨论】:

  • 当我使用 GoogleARCore 输入时,它会显示该错误。我不能调用那个命名空间。
  • 您是否经过Quickstart for AndroidQuickstart for iOS的所有步骤?
  • 我确实做到了,但在这里我不能调用using GoogelARCore 来使用该命名空间。它告诉我The type or namespace name GoogleArCore could not be found (are you missing a using directive or an assembly reference?
  • 不建议我使用那个命名空间。
  • 否,但它在Enable ARCore 中继续,最后到达API,它清楚地表明类型是GoogleARCore.ARCoreBackgroundRenderer,这意味着您需要这个命名空间。如果无法正确识别,则之前的步骤有问题。如果您在正确的目标平台上,请检查所有内容是否已导入并在您的构建设置中...
猜你喜欢
  • 2011-06-04
  • 2015-10-11
  • 1970-01-01
  • 2012-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
相关资源
最近更新 更多