FMOD.RESULT EventInstanceCreatedCallback(EVENT_CALLBACK_TYPE type, IntPtr instancePtr, IntPtr parameterPtr) { EventInstance instance = new EventInstance(instancePtr); IntPtr registreePointer; instance.getUserData(out registreePointer); GCHandle registreeHandle = GCHandle.FromIntPtr(registreePointer); DelayedSpatialAudioRegistree delayedRegistree = registreeHandle.Target as DelayedSpatialAudioRegistree; switch (type) { case EVENT_CALLBACK_TYPE.CREATED: { float maxDistance = ResonanceAudioSourceUtility.GetResonanceAudioSourceMaxDistance(instance); if (maxDistance == -1) { if (delayedRegistree != null) { delayedRegistree.isInvalid = true; } } else { if (instance.isValid() && delayedRegistree != null) { delayedRegistree.maxDistance = maxDistance; delayedRegistree.instanceCreationComplete = true; } else if (delayedRegistree != null) { delayedRegistree.isInvalid = true; } } } break; case EVENT_CALLBACK_TYPE.DESTROYED: { registreeHandle.Free(); } break; default: break; } return(FMOD.RESULT.OK); }
private void InstantiateAudioObject(Transform follow = null, List <AudioObjectParameter> parameters = null) { if (destructionInProgress || !initializationSuccesfull) { return; } if (singleton && instantiations.Count > 0) { return; } int index = 0; if (eventReferences.Count > 1) { index = SelectRandomIndex(); } EventInstance eventInstance; eventInstance = RuntimeManager.CreateInstance(eventReferences[index]); if (!eventInstance.isValid()) { Debug.LogError("Creating an event instance failed for Audio Object '" + gameObject.name + "'."); return; } if (parameters != null) { for (int i = 0; i < parameters.Count; i++) { AudioObjectParameter parameter = parameters[i]; FMOD.RESULT result = eventInstance.setParameterByName(parameter.name, parameter.value); if (result != FMOD.RESULT.OK) { Debug.LogWarning("Setting a value for local parameter '" + parameter.name + "' failed for '" + gameObject.name + "'. Fmod error: " + result); } } } EventDescription eventDescription = RuntimeManager.GetEventDescription(eventReferences[index]); bool is3D; eventDescription.is3D(out is3D); Transform transformToFollow = null; if (is3D) { // Transform follow priority: // 1. Transform optionally provided with the event instantiation // 2. Transform optionally provided in the inspector // 3. The Transform of this Audio Object. if (follow != null) { transformToFollow = follow; } else if (followTransform) { transformToFollow = followTransform; } else { transformToFollow = gameObject.transform; } // If the followed game object has a rigibody, retrieve it and pass it to FMOD RuntimeManager for velocity updates. Rigidbody rb = transformToFollow.gameObject.GetComponent <Rigidbody>(); RuntimeManager.AttachInstanceToGameObject(eventInstance, transformToFollow, rb); } if (spatialAudioRoomAware && SpatialAudioManager.Instance != null) { if (is3D) { if (_usesResonanceAudioSource) { var delayedRegistree = new DelayedSpatialAudioRegistree(); delayedRegistree.transformToFollow = transformToFollow; delayedRegistree.eventInstance = eventInstance; delayedSpatialAudioRegistrees.Add(delayedRegistree); GCHandle registreeHandle = GCHandle.Alloc(delayedRegistree, GCHandleType.Pinned); eventInstance.setUserData(GCHandle.ToIntPtr(registreeHandle)); eventInstance.setCallback(instanceCreatedCallback, EVENT_CALLBACK_TYPE.CREATED | EVENT_CALLBACK_TYPE.DESTROYED); return; } else { float maxDistance; eventDescription.getMaximumDistance(out maxDistance); SpatialAudioManager.Instance.RegisterRoomAwareInstance(eventInstance, transformToFollow, maxDistance, initialRoom, false); } } } instantiations.Add(eventInstance); eventInstance.start(); }