Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Johnconstine
minigame-unity-webgl-transform
提交
a004d13f
M
minigame-unity-webgl-transform
项目概览
Johnconstine
/
minigame-unity-webgl-transform
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
minigame-unity-webgl-transform
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
a004d13f
编写于
11月 10, 2022
作者:
U
uhu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 优化示例
上级
55a487e9
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
134 addition
and
51 deletion
+134
-51
Demo/API/Assets/Scenes/InnerAudio.unity
Demo/API/Assets/Scenes/InnerAudio.unity
+8
-9
Demo/API/Assets/Scripts/AudioManager.cs
Demo/API/Assets/Scripts/AudioManager.cs
+114
-42
Demo/API/ProjectSettings/EditorBuildSettings.asset
Demo/API/ProjectSettings/EditorBuildSettings.asset
+6
-0
Demo/API/UserSettings/EditorUserSettings.asset
Demo/API/UserSettings/EditorUserSettings.asset
+6
-0
未找到文件。
Demo/API/Assets/Scenes/InnerAudio.unity
浏览文件 @
a004d13f
...
...
@@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity
:
1
m_CustomReflection
:
{
fileID
:
0
}
m_Sun
:
{
fileID
:
0
}
m_IndirectSpecularColor
:
{
r
:
0.446578
74
,
g
:
0.49641275
,
b
:
0.5748172
,
a
:
1
}
m_IndirectSpecularColor
:
{
r
:
0.446578
15
,
g
:
0.49641192
,
b
:
0.57481617
,
a
:
1
}
m_UseRadianceAmbientProbe
:
0
---
!u!157
&3
LightmapSettings
:
...
...
@@ -194,7 +194,7 @@ MonoBehaviour:
m_HorizontalOverflow
:
0
m_VerticalOverflow
:
0
m_LineSpacing
:
1
m_Text
:
Play
2
m_Text
:
Play
Short
5
---
!u!222
&475772372
CanvasRenderer
:
m_ObjectHideFlags
:
0
...
...
@@ -296,7 +296,7 @@ GameObject:
-
component
:
{
fileID
:
594407492
}
-
component
:
{
fileID
:
594407491
}
m_Layer
:
5
m_Name
:
Play2
m_Name
:
Short
5
m_TagString
:
Untagged
m_Icon
:
{
fileID
:
0
}
m_NavMeshLayer
:
0
...
...
@@ -369,8 +369,8 @@ MonoBehaviour:
m_Calls
:
-
m_Target
:
{
fileID
:
2105647632
}
m_TargetAssemblyTypeName
:
AudioManager, Assembly-CSharp
m_MethodName
:
play
AfterDownload
m_Mode
:
3
m_MethodName
:
play
Short
m_Mode
:
1
m_Arguments
:
m_ObjectArgument
:
{
fileID
:
0
}
m_ObjectArgumentAssemblyTypeName
:
UnityEngine.Object, UnityEngine
...
...
@@ -786,7 +786,7 @@ MonoBehaviour:
m_HorizontalOverflow
:
0
m_VerticalOverflow
:
0
m_LineSpacing
:
1
m_Text
:
Play
Random
m_Text
:
Play
Long
5
---
!u!222
&1260414954
CanvasRenderer
:
m_ObjectHideFlags
:
0
...
...
@@ -902,7 +902,7 @@ GameObject:
-
component
:
{
fileID
:
1302124278
}
-
component
:
{
fileID
:
1302124277
}
m_Layer
:
5
m_Name
:
Random
m_Name
:
Long
5
m_TagString
:
Untagged
m_Icon
:
{
fileID
:
0
}
m_NavMeshLayer
:
0
...
...
@@ -975,7 +975,7 @@ MonoBehaviour:
m_Calls
:
-
m_Target
:
{
fileID
:
2105647632
}
m_TargetAssemblyTypeName
:
AudioManager, Assembly-CSharp
m_MethodName
:
play
Random
m_MethodName
:
play
Long
m_Mode
:
1
m_Arguments
:
m_ObjectArgument
:
{
fileID
:
0
}
...
...
@@ -1267,7 +1267,6 @@ MonoBehaviour:
m_Script
:
{
fileID
:
11500000
,
guid
:
4f231c4fb786f3946a6b90b886c48677
,
type
:
3
}
m_Name
:
m_EditorClassIdentifier
:
m_SendPointerHoverToParent
:
1
m_HorizontalAxis
:
Horizontal
m_VerticalAxis
:
Vertical
m_SubmitButton
:
Submit
...
...
Demo/API/Assets/Scripts/AudioManager.cs
浏览文件 @
a004d13f
...
...
@@ -13,6 +13,13 @@ public class AudioManager : MonoBehaviour
// 当前场景需要预下载的音频列表
private
static
string
[]
audioList
=
{
// 短音频
"https://res.wx.qq.com/wechatgame/product/webpack/userupload/20221108/194356/1.mp3"
,
"https://res.wx.qq.com/wechatgame/product/webpack/userupload/20221108/194517/2.mp3"
,
"https://res.wx.qq.com/wechatgame/product/webpack/userupload/20221108/194523/3.mp3"
,
"https://res.wx.qq.com/wechatgame/product/webpack/userupload/20221108/194530/4.mp3"
,
"https://res.wx.qq.com/wechatgame/product/webpack/userupload/20221108/194536/5.mp3"
,
// 长音频
"https://res.wx.qq.com/wechatgame/product/webpack/userupload/20220901/211827/CallMeTeenTop.mp3"
,
"https://res.wx.qq.com/wechatgame/product/webpack/userupload/20220815/105451/1.mp3"
,
"https://res.wx.qq.com/wechatgame/product/webpack/userupload/20220901/211846/Night-n.mp3"
,
...
...
@@ -25,6 +32,8 @@ public class AudioManager : MonoBehaviour
private
bool
isDestroyed
=
false
;
private
int
createdAudioCount
=
0
;
// 初始化
public
void
Start
()
{
...
...
@@ -36,17 +45,12 @@ public class AudioManager : MonoBehaviour
// 批量下载音频文件
downloadAudio
();
// 先下载后播放第3个音频
playAfterDownload
(
2
);
// 立即播放(但不会缓存到本地)第1个音频
playRightNow
(
0
);
}
private
WXInnerAudioContext
getAudio
()
{
if
(
this
.
isDestroyed
)
{
if
(
this
.
isDestroyed
)
{
return
null
;
}
...
...
@@ -64,27 +68,43 @@ public class AudioManager : MonoBehaviour
private
void
removeAudio
(
WXInnerAudioContext
audio
)
{
audio
.
OffCanplay
();
if
(
audioPlayArray
.
Contains
(
audio
))
{
audio
.
OffCanplay
();
audioPlayArray
.
Remove
(
audio
);
}
if
(!
audioPool
.
Contains
(
audio
))
{
audioPool
.
Enqueue
(
audio
);
}
Debug
.
Log
(
"___________________"
);
Debug
.
Log
(
"已创建InnerAudio"
+
createdAudioCount
+
" 对象池:"
+
audioPool
.
Count
+
" 正在播放:"
+
audioPlayArray
.
Count
);
Debug
.
Log
(
"___________________"
);
}
private
WXInnerAudioContext
addAudio
()
{
if
(
createdAudioCount
>
32
)
{
Debug
.
LogError
(
"最多只支持同时使用32个InnerAudio"
);
}
var
audio
=
WX
.
CreateInnerAudioContext
(
new
InnerAudioContextParam
()
{
needDownload
=
true
});
createdAudioCount
+=
1
;
// 自动播放停止
audio
.
OnEnded
(()
=>
{
Debug
.
Log
(
audio
.
instanceId
+
" OnEnded"
);
removeAudio
(
audio
);
});
// 加载出错
audio
.
OnError
(()
=>
{
Debug
.
Log
(
audio
.
instanceId
+
"audio OnError"
);
audio
.
Stop
();
removeAudio
(
audio
);
});
...
...
@@ -92,9 +112,22 @@ public class AudioManager : MonoBehaviour
// 手动停止
audio
.
OnStop
(()
=>
{
Debug
.
Log
(
audio
.
instanceId
+
"audio OnStop"
);
removeAudio
(
audio
);
});
// 暂停
audio
.
OnPause
(()
=>
{
Debug
.
Log
(
audio
.
instanceId
+
"audio OnPause"
);
});
// 播放成功
audio
.
OnPlay
(()
=>
{
Debug
.
Log
(
audio
.
instanceId
+
"audio OnPlay"
);
});
audioPool
.
Enqueue
(
audio
);
return
audio
;
...
...
@@ -110,7 +143,7 @@ public class AudioManager : MonoBehaviour
// 下载成功
// 下载后播放第2个音频
playAfterDownload
(
1
);
//
playAfterDownload(1);
}
else
{
...
...
@@ -119,32 +152,42 @@ public class AudioManager : MonoBehaviour
});
}
public
void
playAfterDownload
(
int
index
)
public
void
playAfterDownload
(
int
index
,
bool
isEffect
)
{
var
audioIndex
=
getAudio
();
if
(
audioIndex
==
null
)
{
if
(
audioIndex
==
null
)
{
return
;
}
// 如果没有下文修改needDownload为false的函数,理论上创建的所有音频都是true,可以省去这一条
audioIndex
.
needDownload
=
true
;
if
(
audioIndex
.
src
==
audioList
[
index
])
{
audioIndex
.
Play
();
// 如果要设置的src和原音频对象一致,可以直接播放
if
(
audioIndex
.
src
==
audioList
[
index
])
{
audioIndex
.
Play
();
}
else
{
// 对于已经设置了needDownload为true的audio,设置src后就会开始下载对应的音频文件
// 如果该文件已经下载过,并且配置了缓存本地,就不会重复下载
// 如果该文件没有下载过,等同于先调用WX.PreDownloadAudios下载后再播放
audioIndex
.
src
=
audioList
[
index
];
// 在可以播放时播放
audioIndex
.
OnCanplay
(()
=>
{
audioIndex
.
Play
();
});
else
{
// 对于已经设置了needDownload为true的audio,设置src后就会开始下载对应的音频文件
// 如果该文件已经下载过,并且配置了缓存本地,就不会重复下载
// 如果该文件没有下载过,等同于先调用WX.PreDownloadAudios下载后再播放
audioIndex
.
src
=
audioList
[
index
];
if
(
isEffect
)
{
audioIndex
.
Play
();
}
else
{
// 在可以播放时播放
audioIndex
.
OnCanplay
(()
=>
{
audioIndex
.
Play
();
});
}
}
}
...
...
@@ -154,7 +197,8 @@ public class AudioManager : MonoBehaviour
// 但是再次使用该音频时会因为没有下载而需要再次下载,并不推荐这样使用
var
audioPlayRightNow
=
getAudio
();
if
(
audioPlayRightNow
==
null
)
{
if
(
audioPlayRightNow
==
null
)
{
return
;
}
...
...
@@ -162,36 +206,64 @@ public class AudioManager : MonoBehaviour
audioPlayRightNow
.
needDownload
=
false
;
// 如果要设置的src和原音频对象一致,可以直接播放
if
(
audioPlayRightNow
.
src
==
audioList
[
index
])
{
audioPlayRightNow
.
Play
();
if
(
audioPlayRightNow
.
src
==
audioList
[
index
])
{
audioPlayRightNow
.
Play
();
}
else
{
// 如果当前音频已经下载过,并且配置了缓存本地,就算设置needDownload为false也不会重复下载
audioPlayRightNow
.
src
=
audioList
[
index
];
// 在可以播放时播放
audioPlayRightNow
.
OnCanplay
(()
=>
{
audioPlayRightNow
.
Play
();
});
else
{
// 如果当前音频已经下载过,并且配置了缓存本地,就算设置needDownload为false也不会重复下载
audioPlayRightNow
.
src
=
audioList
[
index
];
// 在可以播放时播放
audioPlayRightNow
.
OnCanplay
(()
=>
{
audioPlayRightNow
.
Play
();
});
}
}
public
void
stopAllAudio
()
{
audioPlayArray
.
ForEach
(
audio
=>
{
audioPlayArray
.
ForEach
(
audio
=>
{
audio
.
OffCanplay
();
audio
.
Stop
();
});
}
public
void
play
Random
()
public
void
play
Short
()
{
var
index
=
new
System
.
Random
().
Next
(
0
,
audioList
.
Length
);
var
index
=
new
System
.
Random
().
Next
(
0
,
5
);
Debug
.
Log
(
"Play:"
+
index
);
Debug
.
Log
(
"PlayAudioLength:"
+
audioPlayArray
.
Count
);
playRightNow
(
index
);
playAfterDownload
(
index
,
true
);
}
public
void
playLong
()
{
var
index
=
new
System
.
Random
().
Next
(
5
,
10
);
Debug
.
Log
(
"Play:"
+
index
);
playAfterDownload
(
index
,
false
);
}
// 播放5个短音频
public
void
playShort5
()
{
for
(
var
i
=
0
;
i
<
5
;
i
++)
{
this
.
playShort
();
};
}
// 播放5个长音频
public
void
playLong5
()
{
for
(
var
i
=
0
;
i
<
5
;
i
++)
{
this
.
playLong
();
};
}
private
void
OnDestroy
()
...
...
Demo/API/ProjectSettings/EditorBuildSettings.asset
浏览文件 @
a004d13f
...
...
@@ -11,4 +11,10 @@ EditorBuildSettings:
-
enabled
:
1
path
:
Assets/Scenes/SampleScene.unity
guid
:
9a069299b0b9d4cf68218d815a4c1b38
-
enabled
:
1
path
:
Assets/Scenes/InnerAudio.unity
guid
:
6cc335c875b7242b2b937937d262f304
-
enabled
:
1
path
:
Assets/Scenes/Recorder.unity
guid
:
61476b21b30644ba3b6656f5633e7b3c
m_configObjects
:
{}
Demo/API/UserSettings/EditorUserSettings.asset
浏览文件 @
a004d13f
...
...
@@ -11,6 +11,12 @@ EditorUserSettings:
RecentlyUsedSceneGuid-1
:
value
:
5a5004505c0750020f5e0e7b14775c16404f4a7b742c7e6178284930b3e26760
flags
:
0
RecentlyUsedSceneGuid-2
:
value
:
5500005153575b0a0f5d5c7444775d1145154e7c797e20657b7a4e36b5e2673b
flags
:
0
RecentlyUsedSceneGuid-3
:
value
:
5552575556000a035a5b0e7542770d12441541797b717567297b4b61e4b3646c
flags
:
0
vcSharedLogLevel
:
value
:
0d5e400f0650
flags
:
0
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录