Browse Source

Merge pull request #341 from FungusGames/set-audio-pitch

Added Set Audio Pitch command #340
master
Chris Gregan 9 years ago
parent
commit
b28989f62d
  1. 55
      Assets/Fungus/Audio/Scripts/Commands/SetAudioPitch.cs
  2. 12
      Assets/Fungus/Audio/Scripts/Commands/SetAudioPitch.cs.meta
  3. 44
      Assets/Fungus/Audio/Scripts/MusicController.cs
  4. 283
      Assets/Tests/Audio/AudioTests.unity

55
Assets/Fungus/Audio/Scripts/Commands/SetAudioPitch.cs

@ -0,0 +1,55 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
[CommandInfo("Audio",
"Set Audio Pitch",
"Sets the global pitch level for audio played with Play Music and Play Sound commands.")]
[AddComponentMenu("")]
public class SetAudioPitch : Command
{
[Range(0,1)]
[Tooltip("Global pitch level for audio played using the Play Music and Play Sound commands")]
public float pitch = 1;
[Range(0,30)]
[Tooltip("Time to fade between current pitch level and target pitch level.")]
public float fadeDuration;
[Tooltip("Wait until the pitch change has finished before executing next command")]
public bool waitUntilFinished = true;
public override void OnEnter()
{
System.Action onComplete = () => {
if (waitUntilFinished)
{
Continue();
}
};
MusicController musicController = MusicController.GetInstance();
if (musicController != null)
{
musicController.SetAudioPitch(pitch, fadeDuration, onComplete);
}
if (!waitUntilFinished)
{
Continue();
}
}
public override string GetSummary()
{
return "Set to " + pitch + " over " + fadeDuration + " seconds.";
}
public override Color GetButtonColor()
{
return new Color32(242, 209, 176, 255);
}
}
}

12
Assets/Fungus/Audio/Scripts/Commands/SetAudioPitch.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 2d3c9751268124c9390e807040e57447
timeCreated: 1446825167
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

44
Assets/Fungus/Audio/Scripts/MusicController.cs

@ -63,7 +63,49 @@ namespace Fungus
*/
public virtual void SetAudioVolume(float volume, float duration)
{
iTween.AudioTo(gameObject, volume, 1f, duration);
AudioSource audio = GetComponent<AudioSource>();
if (duration == 0f)
{
audio.volume = volume;
return;
}
LeanTween.value(gameObject,
audio.volume,
volume,
duration).setOnUpdate( (v) => {
audio.volume = v;
});
}
/**
* Shifts the game music pitch to required value over a period of time.
* @param volume The new music pitch value
* @param duration The length of time in seconds needed to complete the pitch change.
* @param onComplete A delegate method to call when the pitch shift has completed.
*/
public virtual void SetAudioPitch(float pitch, float duration, System.Action onComplete)
{
AudioSource audio = GetComponent<AudioSource>();
if (duration == 0f)
{
audio.pitch = pitch;
return;
}
LeanTween.value(gameObject,
audio.pitch,
pitch,
duration).setOnUpdate( (p) => {
audio.pitch = p;
}).setOnComplete( () => {
if (onComplete != null)
{
onComplete();
}
});
}
/**

283
Assets/Tests/Audio/AudioTests.unity

@ -8,25 +8,25 @@ SceneSettings:
m_PVSPortalsArray: []
m_OcclusionBakeSettings:
smallestOccluder: 5
smallestHole: .25
smallestHole: 0.25
backfaceThreshold: 100
--- !u!104 &2
RenderSettings:
m_ObjectHideFlags: 0
serializedVersion: 6
m_Fog: 0
m_FogColor: {r: .5, g: .5, b: .5, a: 1}
m_FogColor: {r: 0.5, g: 0.5, b: 0.5, a: 1}
m_FogMode: 3
m_FogDensity: .00999999978
m_FogDensity: 0.01
m_LinearFogStart: 0
m_LinearFogEnd: 300
m_AmbientSkyColor: {r: .211999997, g: .226999998, b: .259000003, a: 1}
m_AmbientEquatorColor: {r: .114, g: .125, b: .133000001, a: 1}
m_AmbientGroundColor: {r: .0469999984, g: .0430000015, b: .0350000001, a: 1}
m_AmbientSkyColor: {r: 0.212, g: 0.227, b: 0.259, a: 1}
m_AmbientEquatorColor: {r: 0.114, g: 0.125, b: 0.133, a: 1}
m_AmbientGroundColor: {r: 0.047, g: 0.043, b: 0.035, a: 1}
m_AmbientIntensity: 1
m_AmbientMode: 3
m_SkyboxMaterial: {fileID: 0}
m_HaloStrength: .5
m_HaloStrength: 0.5
m_FlareStrength: 1
m_FlareFadeSpeed: 3
m_HaloTexture: {fileID: 0}
@ -40,7 +40,7 @@ RenderSettings:
--- !u!157 &4
LightmapSettings:
m_ObjectHideFlags: 0
serializedVersion: 5
serializedVersion: 6
m_GIWorkflowMode: 1
m_LightmapsMode: 1
m_GISettings:
@ -66,7 +66,7 @@ LightmapSettings:
m_FinalGather: 0
m_FinalGatherRayCount: 1024
m_ReflectionCompression: 2
m_LightmapSnapshot: {fileID: 0}
m_LightingDataAsset: {fileID: 0}
m_RuntimeCPUUsage: 25
--- !u!196 &5
NavMeshSettings:
@ -74,15 +74,15 @@ NavMeshSettings:
m_ObjectHideFlags: 0
m_BuildSettings:
serializedVersion: 2
agentRadius: .5
agentRadius: 0.5
agentHeight: 2
agentSlope: 45
agentClimb: .400000006
agentClimb: 0.4
ledgeDropHeight: 0
maxJumpAcrossDistance: 0
accuratePlacement: 0
minRegionArea: 2
cellSize: .166666672
cellSize: 0.16666667
manualCellSize: 0
m_NavMeshData: {fileID: 0}
--- !u!1 &76140891
@ -134,14 +134,14 @@ Camera:
m_Enabled: 1
serializedVersion: 2
m_ClearFlags: 1
m_BackGroundColor: {r: .192156866, g: .301960796, b: .474509805, a: .0196078438}
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0.019607844}
m_NormalizedViewPortRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
near clip plane: .300000012
near clip plane: 0.3
far clip plane: 1000
field of view: 60
orthographic: 1
@ -157,7 +157,7 @@ Camera:
m_HDR: 0
m_OcclusionCulling: 1
m_StereoConvergence: 10
m_StereoSeparation: .0219999999
m_StereoSeparation: 0.022
m_StereoMirrorMode: 0
--- !u!4 &76140896
Transform:
@ -604,7 +604,7 @@ Transform:
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 641282520}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -.137771219, y: -.16843766, z: 0}
m_LocalPosition: {x: -0.13777122, y: -0.16843766, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1602367573}
@ -669,7 +669,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 2
m_RootOrder: 3
--- !u!114 &665292077
MonoBehaviour:
m_ObjectHideFlags: 0
@ -704,6 +704,56 @@ MonoBehaviour:
other: {fileID: 0}
otherPropertyPath:
constantValueGeneric: 1
--- !u!1 &741990852
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 741990854}
- 114: {fileID: 741990853}
m_Layer: 0
m_Name: SetAudioPitchTest
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &741990853
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 741990852}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b1dba0b27b0864740a8720e920aa88c0, type: 3}
m_Name:
m_EditorClassIdentifier:
timeout: 5
ignored: 0
succeedAfterAllAssertionsAreExecuted: 1
expectException: 0
expectedExceptionList:
succeedWhenExceptionIsThrown: 0
includedPlatforms: -1
platformsToIgnore: []
dynamic: 0
dynamicTypeName:
--- !u!4 &741990854
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 741990852}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 1370059601}
m_Father: {fileID: 0}
m_RootOrder: 6
--- !u!114 &809764427
MonoBehaviour:
m_ObjectHideFlags: 0
@ -748,7 +798,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 61dddfdc5e0e44ca298d8f46f7f5a915, type: 3}
m_Name:
m_EditorClassIdentifier:
selectedFlowchart: {fileID: 154271130}
selectedFlowchart: {fileID: 1370059605}
--- !u!4 &845793761
Transform:
m_ObjectHideFlags: 1
@ -760,7 +810,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_RootOrder: 1
--- !u!1 &1280611022
GameObject:
m_ObjectHideFlags: 0
@ -784,7 +834,7 @@ Transform:
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1280611022}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -.320000172, y: -13.6900005, z: 0}
m_LocalPosition: {x: -0.32000017, y: -13.690001, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 2109416473}
@ -829,6 +879,7 @@ AudioSource:
tangentMode: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
panLevelCustomCurve:
serializedVersion: 2
m_Curve:
@ -839,6 +890,7 @@ AudioSource:
tangentMode: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
spreadCustomCurve:
serializedVersion: 2
m_Curve:
@ -849,6 +901,7 @@ AudioSource:
tangentMode: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
reverbZoneMixCustomCurve:
serializedVersion: 2
m_Curve:
@ -859,6 +912,7 @@ AudioSource:
tangentMode: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
--- !u!114 &1297950424
MonoBehaviour:
m_ObjectHideFlags: 0
@ -876,6 +930,181 @@ MonoBehaviour:
other: {fileID: 0}
otherPropertyPath:
constantValueGeneric: 1
--- !u!1 &1370059600
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 142980, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a, type: 2}
m_PrefabInternal: {fileID: 0}
serializedVersion: 4
m_Component:
- 4: {fileID: 1370059601}
- 114: {fileID: 1370059605}
- 114: {fileID: 1370059604}
- 114: {fileID: 1370059603}
- 114: {fileID: 1370059602}
- 114: {fileID: 1370059607}
- 114: {fileID: 1370059606}
- 114: {fileID: 1370059608}
m_Layer: 0
m_Name: Flowchart
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1370059601
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 467082, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a, type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1370059600}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 741990854}
m_RootOrder: 0
--- !u!114 &1370059602
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1370059600}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2d3c9751268124c9390e807040e57447, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 1
errorMessage:
indentLevel: 0
pitch: 0
fadeDuration: 1
waitUntilFinished: 1
--- !u!114 &1370059603
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 11462346, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1370059600}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d2f6487d21a03404cb21b245f0242e79, type: 3}
m_Name:
m_EditorClassIdentifier:
parentBlock: {fileID: 1370059604}
--- !u!114 &1370059604
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 11433304, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1370059600}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3d3d73aef2cfc4f51abf34ac00241f60, type: 3}
m_Name:
m_EditorClassIdentifier:
nodeRect:
serializedVersion: 2
x: 67
y: 70
width: 120
height: 40
itemId: 0
blockName: Start
description:
eventHandler: {fileID: 1370059603}
commandList:
- {fileID: 1370059607}
- {fileID: 1370059602}
- {fileID: 1370059606}
- {fileID: 1370059608}
--- !u!114 &1370059605
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 11430050, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a,
type: 2}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1370059600}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 7a334fe2ffb574b3583ff3b18b4792d3, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 1.0
scrollPos: {x: 0, y: 0}
variablesScrollPos: {x: 0, y: 0}
variablesExpanded: 1
blockViewHeight: 400
zoom: 1
scrollViewRect:
serializedVersion: 2
x: -343
y: -340
width: 1114
height: 859
selectedBlock: {fileID: 0}
selectedCommands: []
variables: []
description: 'This is a manual test to check that the Set Audio Pitch command
changes the pitch of a sound played using the PlaySound command.'
stepPause: 0
colorCommands: 1
hideComponents: 1
saveSelection: 1
localizationId:
hideCommands: []
--- !u!114 &1370059606
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1370059600}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 2d3c9751268124c9390e807040e57447, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 3
errorMessage:
indentLevel: 0
pitch: 0
fadeDuration: 0.25
waitUntilFinished: 0
--- !u!114 &1370059607
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1370059600}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: bd2f6be51b3d7490ba5633ee095d0391, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 2
errorMessage:
indentLevel: 0
soundClip: {fileID: 8300000, guid: 476f002f45c7655408b9e7b1fa186f26, type: 3}
volume: 1
waitUntilFinished: 0
--- !u!114 &1370059608
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1370059600}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4920f47cde1a84b11ad07b7317568494, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 4
errorMessage:
indentLevel: 0
--- !u!1 &1602367571
GameObject:
m_ObjectHideFlags: 0
@ -926,7 +1155,7 @@ Transform:
- {fileID: 1716415744}
- {fileID: 641282521}
m_Father: {fileID: 0}
m_RootOrder: 1
m_RootOrder: 2
--- !u!1 &1716415743
GameObject:
m_ObjectHideFlags: 0
@ -1238,7 +1467,7 @@ MonoBehaviour:
m_SubmitButton: Submit
m_CancelButton: Cancel
m_InputActionsPerSecond: 10
m_RepeatDelay: .5
m_RepeatDelay: 0.5
m_ForceModuleActive: 0
--- !u!114 &1972173460
MonoBehaviour:
@ -1265,7 +1494,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_RootOrder: 5
--- !u!1 &2109416471
GameObject:
m_ObjectHideFlags: 0
@ -1318,7 +1547,7 @@ Transform:
- {fileID: 1280611023}
- {fileID: 353260578}
m_Father: {fileID: 0}
m_RootOrder: 3
m_RootOrder: 4
--- !u!1 &2113119598
GameObject:
m_ObjectHideFlags: 0
@ -1342,7 +1571,7 @@ Transform:
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 2113119598}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -.320000172, y: -13.6900005, z: 0}
m_LocalPosition: {x: -0.32000017, y: -13.690001, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 2109416473}
@ -1387,6 +1616,7 @@ AudioSource:
tangentMode: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
panLevelCustomCurve:
serializedVersion: 2
m_Curve:
@ -1397,6 +1627,7 @@ AudioSource:
tangentMode: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
spreadCustomCurve:
serializedVersion: 2
m_Curve:
@ -1407,6 +1638,7 @@ AudioSource:
tangentMode: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
reverbZoneMixCustomCurve:
serializedVersion: 2
m_Curve:
@ -1417,3 +1649,4 @@ AudioSource:
tangentMode: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4

Loading…
Cancel
Save