Unity 使用this关键字进行函数拓展 - AudioSource

举报
CoderZ1010 发表于 2022/09/25 06:44:44 2022/09/25
【摘要】 Example: var audioSource = GetComponent<AudioSource>();audioSource .SetClip(bgm) .SetVolume(0.5f) .SetPriority(128) .SetPitch(1) .SetPanStereo(0) ...

Example:


  
  1. var audioSource = GetComponent<AudioSource>();
  2. audioSource
  3. .SetClip(bgm)
  4. .SetVolume(0.5f)
  5. .SetPriority(128)
  6. .SetPitch(1)
  7. .SetPanStereo(0)
  8. .SetSpatialBlend(0)
  9. .SetPlayOnAwake(true)
  10. .SetLoop(true)
  11. .Play();

Extension:


  
  1. using UnityEngine;
  2. namespace SK.Framework
  3. {
  4. /// <summary>
  5. /// 音源相关拓展
  6. /// </summary>
  7. public static class AudioSourceExtension
  8. {
  9. /// <summary>
  10. /// 播放音频
  11. /// </summary>
  12. /// <param name="source">音源</param>
  13. /// <param name="clip">音频</param>
  14. /// <returns>音源</returns>
  15. public static AudioSource Play(this AudioSource source, AudioClip clip)
  16. {
  17. source.clip = clip;
  18. source.Play();
  19. return source;
  20. }
  21. /// <summary>
  22. /// 设置音频
  23. /// </summary>
  24. /// <param name="source">音源</param>
  25. /// <param name="clip">音频</param>
  26. /// <returns>音源</returns>
  27. public static AudioSource SetClip(this AudioSource source, AudioClip clip)
  28. {
  29. source.clip = clip;
  30. return source;
  31. }
  32. /// <summary>
  33. /// 设置Pitch属性
  34. /// </summary>
  35. /// <param name="source">音源</param>
  36. /// <param name="pitch"Pitch</param>
  37. /// <returns>音源</returns>
  38. public static AudioSource SetPitch(this AudioSource source, float pitch)
  39. {
  40. source.pitch = pitch;
  41. return source;
  42. }
  43. /// <summary>
  44. /// 设置优先级
  45. /// </summary>
  46. /// <param name="source">音源</param>
  47. /// <param name="priority">优先级</param>
  48. /// <returns>音源</returns>
  49. public static AudioSource SetPriority(this AudioSource source, int priority)
  50. {
  51. source.priority = priority;
  52. return source;
  53. }
  54. /// <summary>
  55. /// 设置循环
  56. /// </summary>
  57. /// <param name="source">音源</param>
  58. /// <param name="loop">是否循环</param>
  59. /// <returns>音源</returns>
  60. public static AudioSource SetLoop(this AudioSource source, bool loop)
  61. {
  62. source.loop = loop;
  63. return source;
  64. }
  65. /// <summary>
  66. /// 设置是否激活时播放
  67. /// </summary>
  68. /// <param name="source">音源</param>
  69. /// <param name="playOnAwake">是否激活时播放</param>
  70. /// <returns>音源</returns>
  71. public static AudioSource SetPlayOnAwake(this AudioSource source, bool playOnAwake)
  72. {
  73. source.playOnAwake = playOnAwake;
  74. return source;
  75. }
  76. /// <summary>
  77. /// 设置音量
  78. /// </summary>
  79. /// <param name="source">音源</param>
  80. /// <param name="volume">音量</param>
  81. /// <returns>音源</returns>
  82. public static AudioSource SetVolume(this AudioSource source, float volume)
  83. {
  84. source.volume = volume;
  85. return source;
  86. }
  87. /// <summary>
  88. /// 设置声道
  89. /// </summary>
  90. /// <param name="source">音源</param>
  91. /// <param name="panStereo">声道</param>
  92. /// <returns>音源</returns>
  93. public static AudioSource SetPanStereo(this AudioSource source, float panStereo)
  94. {
  95. source.panStereo = panStereo;
  96. return source;
  97. }
  98. /// <summary>
  99. /// 设置空间混合
  100. /// </summary>
  101. /// <param name="source">音源</param>
  102. /// <param name="spatialBlend">空间混合 0表示2D,1表示3D</param>
  103. /// <returns>音源</returns>
  104. public static AudioSource SetSpatialBlend(this AudioSource source, float spatialBlend)
  105. {
  106. source.spatialBlend = spatialBlend;
  107. return source;
  108. }
  109. }
  110. }

文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。

原文链接:coderz.blog.csdn.net/article/details/116755365

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。