音频Action

This commit is contained in:
shenjianxing 2024-12-19 09:47:07 +08:00
parent b2ec38ac26
commit 7414d53f40
7 changed files with 249 additions and 2 deletions

View File

@ -166,6 +166,11 @@ public class ActionHelper
var strAction = (XMLTool.StringListAction)act;
return LoadResAction.Allocate(act.Value, strAction.args[0]);
}
case "Audio":
{
var strAction = (XMLTool.StringListAction)act;
return AudioAction.Allocate(act.Value, strAction.args[0], strAction.args[1], strAction.args[2], strAction.args[3], strAction.args[4]);
}
default:
Debug.LogError($"没有找到此Action的类型{act.Type}");
break;

View File

@ -0,0 +1,167 @@
using System;
using UnityEngine;
namespace QFramework
{
internal class AudioAction : IAction
{
public System.Action OnFinished { get; set; }
private AudioAction()
{
}
private static readonly SimpleObjectPool<AudioAction> mPool =
new SimpleObjectPool<AudioAction>(() => new AudioAction(), null, 10);
public string path;
public string audioType;
bool loop = false;
public bool waitFinished = true;
bool isPlay = true;
float volume;
ResLoader loader;
public static AudioAction Allocate(string path, string audioType, string loop, string waitFinished, string volume, string isPlay, System.Action OnFinished = null)
{
var retNode = mPool.Allocate();
retNode.ActionID = ActionKit.ID_GENERATOR++;
retNode.Deinited = false;
retNode.Reset();
retNode.path = path;
retNode.audioType = audioType;
bool.TryParse(loop, out retNode.loop);
bool.TryParse(waitFinished, out retNode.waitFinished);
retNode.OnFinished = OnFinished;
bool.TryParse(isPlay, out retNode.isPlay);
if (string.IsNullOrEmpty(volume))
{
retNode.volume = AudioKit.Settings.MusicVolume.Value;
}
else
{
float.TryParse(volume, out retNode.volume);
}
return retNode;
}
public ulong ActionID { get; set; }
public ActionStatus Status { get; set; }
public void OnStart()
{
if (isPlay)
{
loader = ResLoader.Allocate();
string audioPath = Global.audioPath + path;
loader.Add2Load(audioPath.ToLocalAudioResName(), (success, res) =>
{
if (success)
{
AudioClip clip = res.Asset.As<AudioClip>();
switch (audioType)
{
case "Music":
if (waitFinished == false)
{
this.Finish();
AudioKit.PlayMusic(clip, loop: loop);
}
else
{
AudioKit.PlayMusic(clip, loop: loop, onEndCallback: OnPlayFinished);
}
break;
case "Voice":
if (waitFinished == false)
{
this.Finish();
AudioKit.PlayVoice(clip, loop: loop);
}
else
{
AudioKit.PlayVoice(clip, loop: loop, onEndedCallback: OnPlayFinished);
}
break;
case "Sound":
if (waitFinished == false)
{
this.Finish();
AudioKit.PlaySound(clip, loop: loop);
}
else
{
AudioKit.PlaySound(clip, loop: loop, callBack: OnSoundPlayFinished);
}
break;
}
}
});
loader.LoadAsync();
}
else
{
switch (audioType)
{
case "Music":
AudioKit.StopMusic();
break;
case "Voice":
AudioKit.StopVoice();
break;
case "Sound":
AudioKit.StopAllSound();
break;
}
}
}
public void OnPlayFinished()
{
Debug.LogError("OnPlayFinished");
this.Finish();
}
public void OnSoundPlayFinished(AudioPlayer player)
{
OnPlayFinished();
}
public void OnExecute(float dt)
{
}
public void OnFinish()
{
}
public void Reset()
{
Status = ActionStatus.NotStart;
Paused = false;
}
public bool Paused { get; set; }
public void Deinit()
{
if (!Deinited)
{
OnFinished = null;
Deinited = true;
//loader.Recycle2Cache();
mPool.Recycle(this);
}
}
public bool Deinited { get; set; }
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 701ece2f3b3a0ab448541d3fbfc67087
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -699,6 +699,58 @@ namespace XMLTool
newAction = act;
}
break;
case "Audio":
{
//string path, string audioType, string loop, string finishedEvent, string volume,
var act = new StringListAction();
XAttribute audioType = action.Attribute("audioType");
if (audioType != null)
{
act.args.Add(audioType.Value);
}
else
{
act.args.Add("Sound");
}
XAttribute loop = action.Attribute("loop");
if (loop != null)
{
act.args.Add(loop.Value);
}
else
{
act.args.Add("false");
}
XAttribute waitFinished = action.Attribute("waitFinished");
if (waitFinished != null)
{
act.args.Add(waitFinished.Value);
}
else
{
act.args.Add("");
}
XAttribute volume = action.Attribute("volume");
if (volume != null)
{
act.args.Add(volume.Value);
}
else
{
act.args.Add("");
}
XAttribute isPlay = action.Attribute("isPlay");
if (isPlay != null)
{
act.args.Add(isPlay.Value);
}
else
{
act.args.Add("true");
}
newAction = act;
}
break;
default:
newAction = new Action();
break;

View File

@ -541,6 +541,7 @@
<Action type="Parallel">
<!--房间墙壁 暂时隐藏-->
<Action type="Show" value="SM_QvanChangJing/sence/pPlane1" isShow="false"></Action>
<Action type="Show" value="SM_QvanChangJing/SM_JianZiLei/zuzhiqian" isShow="false"></Action>
@ -638,7 +639,11 @@
</Reset>
<Start>
<Action type="Sequence">
<!--string audioType, string loop, string waitFinished, string volume, string isPlay,-->
<Action type="Audio" audioType="Voice" value="q001.mp3" loop="false" waitFinished="true" volumen="1" isPlay="true"></Action>
<Action type="Log" value="0000"></Action>
<Action type="Video" value="test.mp4" size="700,700" offset="100,100" finishedEvent="finished" closeEvent="close"/>
<Condition type="StrEvent" value="close"></Condition>

View File

@ -55,7 +55,14 @@
<Action type="HighLight" value="路径" isHigh="true" color="0,255,0,255"></Action>
<!--延迟 value是秒-->
<Action type="Delay" value="2"></Action>
<!--
* audioType="Music" 播放背景音乐,同一时间只能播放一个音乐,播放别的音乐会直接卸载掉正在播放的音乐。
* audioType="Sound" 播放音效,同一时间可以播放多个音效,当多人说话时,也可以用来播放人声。
* audioType="Voice" 播放人声,与播放背景音乐一致,同一时间只能播放一个人声,用于播放一些旁白之类的声音非常适合。
volumen 是播放音量 默认是跟随设置中的背景音乐的音量
waitFinished = false 则不会等待播放完成就往下进行
-->
<Action type="Audio" audioType="Voice" value="q001.mp3" loop="false" waitFinished="true" volumen="1" isPlay="true"></Action>
<!--判断UI点击-->
<Condition type="UIClick" value="UI路径 可以使用快捷键Ctrl+Q获取"></Condition>

View File

@ -120,7 +120,7 @@ QualitySettings:
globalTextureMipmapLimit: 0
textureMipmapLimitSettings: []
anisotropicTextures: 1
antiAliasing: 8
antiAliasing: 2
softParticles: 0
softVegetation: 1
realtimeReflectionProbes: 1