Unity 使用this关键字进行函数拓展 - AudioSource
【摘要】
Example:
var audioSource = GetComponent<AudioSource>();audioSource .SetClip(bgm) .SetVolume(0.5f) .SetPriority(128) .SetPitch(1) .SetPanStereo(0) ...
Example:
-
var audioSource = GetComponent<AudioSource>();
-
audioSource
-
.SetClip(bgm)
-
.SetVolume(0.5f)
-
.SetPriority(128)
-
.SetPitch(1)
-
.SetPanStereo(0)
-
.SetSpatialBlend(0)
-
.SetPlayOnAwake(true)
-
.SetLoop(true)
-
.Play();
Extension:
-
using UnityEngine;
-
-
namespace SK.Framework
-
{
-
/// <summary>
-
/// 音源相关拓展
-
/// </summary>
-
public static class AudioSourceExtension
-
{
-
/// <summary>
-
/// 播放音频
-
/// </summary>
-
/// <param name="source">音源</param>
-
/// <param name="clip">音频</param>
-
/// <returns>音源</returns>
-
public static AudioSource Play(this AudioSource source, AudioClip clip)
-
{
-
source.clip = clip;
-
source.Play();
-
return source;
-
}
-
/// <summary>
-
/// 设置音频
-
/// </summary>
-
/// <param name="source">音源</param>
-
/// <param name="clip">音频</param>
-
/// <returns>音源</returns>
-
public static AudioSource SetClip(this AudioSource source, AudioClip clip)
-
{
-
source.clip = clip;
-
return source;
-
}
-
/// <summary>
-
/// 设置Pitch属性
-
/// </summary>
-
/// <param name="source">音源</param>
-
/// <param name="pitch"Pitch</param>
-
/// <returns>音源</returns>
-
public static AudioSource SetPitch(this AudioSource source, float pitch)
-
{
-
source.pitch = pitch;
-
return source;
-
}
-
/// <summary>
-
/// 设置优先级
-
/// </summary>
-
/// <param name="source">音源</param>
-
/// <param name="priority">优先级</param>
-
/// <returns>音源</returns>
-
public static AudioSource SetPriority(this AudioSource source, int priority)
-
{
-
source.priority = priority;
-
return source;
-
}
-
/// <summary>
-
/// 设置循环
-
/// </summary>
-
/// <param name="source">音源</param>
-
/// <param name="loop">是否循环</param>
-
/// <returns>音源</returns>
-
public static AudioSource SetLoop(this AudioSource source, bool loop)
-
{
-
source.loop = loop;
-
return source;
-
}
-
/// <summary>
-
/// 设置是否激活时播放
-
/// </summary>
-
/// <param name="source">音源</param>
-
/// <param name="playOnAwake">是否激活时播放</param>
-
/// <returns>音源</returns>
-
public static AudioSource SetPlayOnAwake(this AudioSource source, bool playOnAwake)
-
{
-
source.playOnAwake = playOnAwake;
-
return source;
-
}
-
/// <summary>
-
/// 设置音量
-
/// </summary>
-
/// <param name="source">音源</param>
-
/// <param name="volume">音量</param>
-
/// <returns>音源</returns>
-
public static AudioSource SetVolume(this AudioSource source, float volume)
-
{
-
source.volume = volume;
-
return source;
-
}
-
/// <summary>
-
/// 设置声道
-
/// </summary>
-
/// <param name="source">音源</param>
-
/// <param name="panStereo">声道</param>
-
/// <returns>音源</returns>
-
public static AudioSource SetPanStereo(this AudioSource source, float panStereo)
-
{
-
source.panStereo = panStereo;
-
return source;
-
}
-
/// <summary>
-
/// 设置空间混合
-
/// </summary>
-
/// <param name="source">音源</param>
-
/// <param name="spatialBlend">空间混合 0表示2D,1表示3D</param>
-
/// <returns>音源</returns>
-
public static AudioSource SetSpatialBlend(this AudioSource source, float spatialBlend)
-
{
-
source.spatialBlend = spatialBlend;
-
return source;
-
}
-
}
-
}
文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。
原文链接:coderz.blog.csdn.net/article/details/116755365
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)