Skip to content

Commit c1bdaff

Browse files
committed
feat(Util): Add custom play audio at point
1 parent 15a9095 commit c1bdaff

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Assets/JCSUnity/Scripts/Util/JCS_Util.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,30 @@ public static float GetLayerWeight(Animator ator, int index)
12431243

12441244
#endregion
12451245

1246+
#region Audio
1247+
1248+
/// <summary>
1249+
/// Same with function `AudioSource.PlayClipAtPoint` with different
1250+
/// default `spatialBlend` value.
1251+
/// </summary>
1252+
public static void PlayClipAtPoint(
1253+
AudioClip clip,
1254+
Vector3 position,
1255+
float volume,
1256+
float spatialBlend)
1257+
{
1258+
var gameObject = new GameObject("One shot audio");
1259+
gameObject.transform.position = position;
1260+
var audioSource = gameObject.AddComponent<AudioSource>();
1261+
audioSource.clip = clip;
1262+
audioSource.spatialBlend = spatialBlend;
1263+
audioSource.volume = volume;
1264+
audioSource.Play();
1265+
Object.Destroy(gameObject, clip.length * ((Time.timeScale < 0.01f) ? 0.01f : Time.timeScale));
1266+
}
1267+
1268+
#endregion
1269+
12461270
#region Gameplay
12471271

12481272
/// <summary>

docs/ScriptReference/Util/JCS_Util.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ All code utility is stored here.
5353
| IsSceneExists | Returns true if the scene 'name' exists and is in your Build settings, false otherwise. |
5454
| SetLayerWeight | Sets the weight of the layer at the given name. |
5555
| GetLayerWeight | Returns the weight of the layer at the specified name. |
56+
| PlayClipAtPoint | Same with function `AudioSource.PlayClipAtPoint` with different default `spatialBlend` value. |
5657
| IsSameTribe | Check if the live object is the same tribe. |

0 commit comments

Comments
 (0)