Unity 3D如何找到实例化隐藏组件的游戏对象

时间:2018-07-25 13:02:15

标签: c# unity3d photon

我有一个与Photon Unity Network(PUN)和Photon Audio一起运行的多用户应用程序。使用光子音频,音频将流传输到PUN服务器,然后发送到游戏中的所有玩家。 Photon Audio在Unity核心音频类之上具有包装器类,以进行流传输。

我正在使用来自这个论坛Photon-Voice Forum的指导,其中说要记录完整的应用程序输出混合,如果在带有AudioListener的对象上实现,则可以使用MonoBehaviour.OnAudioFilterRead。

问题是我找不到已经创建AudioListener实例的游戏对象,所以我无法使用AudioListener创建自己的游戏对象,因为它作为第二个实例出现。

我尝试使用:

var foundObjects = FindObjectsOfType<AudioListener>();
Debug.Log("**** " + foundObjects + " : " + foundObjects.Length)

返回说有一个AudioListener的活动实例:

**** UnityEngine.AudioListener[] : 1
0x000000014179D92B (Unity) StackWalker::GetCurrentCallstack
0x000000014179F5DF (Unity) StackWalker::ShowCallstack
0x00000001417796A0 (Unity) GetStacktrace
0x0000000140D3F23B (Unity) DebugStringToFile
0x0000000140D3FA1C (Unity) DebugStringToFile
0x00000001413FE562 (Unity) DebugLogHandler_CUSTOM_Internal_Log
0x000000003C8F2F5B (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,string,UnityEngine.Object)
0x000000003C8F2E44 (Mono JIT Code) [DebugLogHandler.cs:9] UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[]) 
0x000000003C8F27D5 (Mono JIT Code) [Logger.cs:41] UnityEngine.Logger:Log (UnityEngine.LogType,object) 
0x000000003C8F2016 (Mono JIT Code) [DebugBindings.gen.cs:103] UnityEngine.Debug:Log (object) 
0x00000000137EDA2B (Mono JIT Code) [PUNManagerStarter.cs:57] PUNManagerStarter:Update () 
0x00000000008C7392 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007FFD0CA55703 (mono) [mini.c:4937] mono_jit_runtime_invoke 
0x00007FFD0C9A8425 (mono) [object.c:2623] mono_runtime_invoke 
0x00000001410657E5 (Unity) scripting_method_invoke
0x000000014105B461 (Unity) ScriptingInvocation::Invoke
0x0000000141498F5B (Unity) MonoBehaviour::CallMethodIfAvailable
0x000000014149D05F (Unity) MonoBehaviour::CallUpdateMethod
0x0000000140AC4A86 (Unity) BaseBehaviourManager::CommonUpdate<BehaviourManager>
0x0000000140AC567F (Unity) BehaviourManager::Update
0x0000000140D74BC7 (Unity) PlayerLoop
0x000000014171983E (Unity) Application::UpdateScene
0x000000014171B00F (Unity) Application::UpdateSceneIfNeeded
0x000000014172373A (Unity) Application::TickTimer
0x00000001417EEA1C (Unity) CrashCallback
0x00000001417F0664 (Unity) WinMain
0x0000000141AD38E4 (Unity) strnlen
0x00007FFD4A4A3034 (KERNEL32) BaseThreadInitThunk
0x00007FFD4CF21431 (ntdll) RtlUserThreadStart

和:

var al = FindObjectOfType<AudioListener>();
Debug.Log("**** " + al + " : " + al.name);

返回:

    **** Head Target (UnityEngine.AudioListener) : Head Target
0x000000014179D92B (Unity) StackWalker::GetCurrentCallstack
0x000000014179F5DF (Unity) StackWalker::ShowCallstack
0x00000001417796A0 (Unity) GetStacktrace
0x0000000140D3F23B (Unity) DebugStringToFile
0x0000000140D3FA1C (Unity) DebugStringToFile
0x00000001413FE562 (Unity) DebugLogHandler_CUSTOM_Internal_Log
0x000000003C8F2F5B (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,string,UnityEngine.Object)
0x000000003C8F2E44 (Mono JIT Code) [DebugLogHandler.cs:9] UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[]) 
0x000000003C8F27D5 (Mono JIT Code) [Logger.cs:41] UnityEngine.Logger:Log (UnityEngine.LogType,object) 
0x000000003C8F2016 (Mono JIT Code) [DebugBindings.gen.cs:103] UnityEngine.Debug:Log (object) 
0x00000000137EDBF2 (Mono JIT Code) [PUNManagerStarter.cs:60] PUNManagerStarter:Update () 
0x00000000008C7392 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007FFD0CA55703 (mono) [mini.c:4937] mono_jit_runtime_invoke 
0x00007FFD0C9A8425 (mono) [object.c:2623] mono_runtime_invoke 
0x00000001410657E5 (Unity) scripting_method_invoke
0x000000014105B461 (Unity) ScriptingInvocation::Invoke
0x0000000141498F5B (Unity) MonoBehaviour::CallMethodIfAvailable
0x000000014149D05F (Unity) MonoBehaviour::CallUpdateMethod
0x0000000140AC4A86 (Unity) BaseBehaviourManager::CommonUpdate<BehaviourManager>
0x0000000140AC567F (Unity) BehaviourManager::Update
0x0000000140D74BC7 (Unity) PlayerLoop
0x000000014171983E (Unity) Application::UpdateScene
0x000000014171B00F (Unity) Application::UpdateSceneIfNeeded
0x000000014172373A (Unity) Application::TickTimer
0x00000001417EEA1C (Unity) CrashCallback
0x00000001417F0664 (Unity) WinMain
0x0000000141AD38E4 (Unity) strnlen
0x00007FFD4A4A3034 (KERNEL32) BaseThreadInitThunk
0x00007FFD4CF21431 (ntdll) RtlUserThreadStart

有关如何找到实例化AudioListener的游戏对象的任何帮助,将不胜感激。

更新1::AudioListener可能被场景中的唯一摄像机实例化,并且该摄像机可能来自我正在使用的名为InstantVR的外部库。但是,它的发生与AudioListener组件相同:我找不到实例化摄像机的脚本。使用var cam = FindObjectOfType<Camera>();,我得到:Headcam (UnityEngine.Camera) : Headcam

1 个答案:

答案 0 :(得分:0)

在我正在调试的应用程序中,将[HideInInspector]HideFlags.HideInHierarchyHideFlags.HideInInspector的组合应用于第三方库中的某些游戏对象和组件。

我使用以下代码找到了所需的游戏对象和组件,并在应用程序运行时检查了层次结构和检查器:

void Update()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        GameObject[] foundObjects = FindObjectsOfType<GameObject>();
        Debug.Log("**** " + foundObjects + " : " + foundObjects.Length);
        var count = 0;
        if (foundObjects != null)
        {
            foreach (GameObject foundObject in foundObjects)
            {
                count++;
                if (foundObject.hideFlags == HideFlags.HideInHierarchy || foundObject.hideFlags == HideFlags.HideInInspector)
                {
                    foundObject.hideFlags = HideFlags.None;
                    Debug.Log("**** foundObject: " + foundObject + " : foundObject Name: " + foundObject.name + " - Count: " + count);
                }
            }
        }
    }
}

然后可以从层次结构访问隐藏的对象。希望对别人有帮助。