Skip to content

Commit 735726f

Browse files
author
uhu
committed
feat: 完善音频
1 parent 9b60b15 commit 735726f

File tree

2 files changed

+280
-23
lines changed

2 files changed

+280
-23
lines changed

Demo/API/Assets/Scenes/Audio.unity

Lines changed: 224 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ RectTransform:
284284
m_Children:
285285
- {fileID: 1611328529}
286286
m_Father: {fileID: 2105647631}
287-
m_RootOrder: 3
287+
m_RootOrder: 4
288288
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
289289
m_AnchorMin: {x: 0.5, y: 0.5}
290290
m_AnchorMax: {x: 0.5, y: 0.5}
@@ -465,6 +465,220 @@ CanvasRenderer:
465465
m_PrefabAsset: {fileID: 0}
466466
m_GameObject: {fileID: 945804509}
467467
m_CullTransparentMesh: 0
468+
--- !u!1 &952095108
469+
GameObject:
470+
m_ObjectHideFlags: 0
471+
m_CorrespondingSourceObject: {fileID: 0}
472+
m_PrefabInstance: {fileID: 0}
473+
m_PrefabAsset: {fileID: 0}
474+
serializedVersion: 6
475+
m_Component:
476+
- component: {fileID: 952095109}
477+
- component: {fileID: 952095111}
478+
- component: {fileID: 952095110}
479+
m_Layer: 5
480+
m_Name: Text
481+
m_TagString: Untagged
482+
m_Icon: {fileID: 0}
483+
m_NavMeshLayer: 0
484+
m_StaticEditorFlags: 0
485+
m_IsActive: 1
486+
--- !u!224 &952095109
487+
RectTransform:
488+
m_ObjectHideFlags: 0
489+
m_CorrespondingSourceObject: {fileID: 0}
490+
m_PrefabInstance: {fileID: 0}
491+
m_PrefabAsset: {fileID: 0}
492+
m_GameObject: {fileID: 952095108}
493+
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
494+
m_LocalPosition: {x: 0, y: 0, z: 0}
495+
m_LocalScale: {x: 1, y: 1, z: 1}
496+
m_ConstrainProportionsScale: 0
497+
m_Children: []
498+
m_Father: {fileID: 976685858}
499+
m_RootOrder: 0
500+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
501+
m_AnchorMin: {x: 0, y: 0}
502+
m_AnchorMax: {x: 1, y: 1}
503+
m_AnchoredPosition: {x: 0, y: 0}
504+
m_SizeDelta: {x: 0, y: 0}
505+
m_Pivot: {x: 0.5, y: 0.5}
506+
--- !u!114 &952095110
507+
MonoBehaviour:
508+
m_ObjectHideFlags: 0
509+
m_CorrespondingSourceObject: {fileID: 0}
510+
m_PrefabInstance: {fileID: 0}
511+
m_PrefabAsset: {fileID: 0}
512+
m_GameObject: {fileID: 952095108}
513+
m_Enabled: 1
514+
m_EditorHideFlags: 0
515+
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
516+
m_Name:
517+
m_EditorClassIdentifier:
518+
m_Material: {fileID: 0}
519+
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
520+
m_RaycastTarget: 1
521+
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
522+
m_Maskable: 1
523+
m_OnCullStateChanged:
524+
m_PersistentCalls:
525+
m_Calls: []
526+
m_FontData:
527+
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
528+
m_FontSize: 32
529+
m_FontStyle: 0
530+
m_BestFit: 0
531+
m_MinSize: 3
532+
m_MaxSize: 40
533+
m_Alignment: 4
534+
m_AlignByGeometry: 0
535+
m_RichText: 1
536+
m_HorizontalOverflow: 0
537+
m_VerticalOverflow: 0
538+
m_LineSpacing: 1
539+
m_Text: Play CDN
540+
--- !u!222 &952095111
541+
CanvasRenderer:
542+
m_ObjectHideFlags: 0
543+
m_CorrespondingSourceObject: {fileID: 0}
544+
m_PrefabInstance: {fileID: 0}
545+
m_PrefabAsset: {fileID: 0}
546+
m_GameObject: {fileID: 952095108}
547+
m_CullTransparentMesh: 0
548+
--- !u!1 &976685857
549+
GameObject:
550+
m_ObjectHideFlags: 0
551+
m_CorrespondingSourceObject: {fileID: 0}
552+
m_PrefabInstance: {fileID: 0}
553+
m_PrefabAsset: {fileID: 0}
554+
serializedVersion: 6
555+
m_Component:
556+
- component: {fileID: 976685858}
557+
- component: {fileID: 976685861}
558+
- component: {fileID: 976685860}
559+
- component: {fileID: 976685859}
560+
m_Layer: 5
561+
m_Name: Play CDN
562+
m_TagString: Untagged
563+
m_Icon: {fileID: 0}
564+
m_NavMeshLayer: 0
565+
m_StaticEditorFlags: 0
566+
m_IsActive: 1
567+
--- !u!224 &976685858
568+
RectTransform:
569+
m_ObjectHideFlags: 0
570+
m_CorrespondingSourceObject: {fileID: 0}
571+
m_PrefabInstance: {fileID: 0}
572+
m_PrefabAsset: {fileID: 0}
573+
m_GameObject: {fileID: 976685857}
574+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
575+
m_LocalPosition: {x: 0, y: 0, z: 0}
576+
m_LocalScale: {x: 1, y: 1, z: 1}
577+
m_ConstrainProportionsScale: 0
578+
m_Children:
579+
- {fileID: 952095109}
580+
m_Father: {fileID: 2105647631}
581+
m_RootOrder: 0
582+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
583+
m_AnchorMin: {x: 0.5, y: 0.5}
584+
m_AnchorMax: {x: 0.5, y: 0.5}
585+
m_AnchoredPosition: {x: 0, y: 180}
586+
m_SizeDelta: {x: 320, y: 60}
587+
m_Pivot: {x: 0.5, y: 0.5}
588+
--- !u!114 &976685859
589+
MonoBehaviour:
590+
m_ObjectHideFlags: 0
591+
m_CorrespondingSourceObject: {fileID: 0}
592+
m_PrefabInstance: {fileID: 0}
593+
m_PrefabAsset: {fileID: 0}
594+
m_GameObject: {fileID: 976685857}
595+
m_Enabled: 1
596+
m_EditorHideFlags: 0
597+
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
598+
m_Name:
599+
m_EditorClassIdentifier:
600+
m_Navigation:
601+
m_Mode: 3
602+
m_WrapAround: 0
603+
m_SelectOnUp: {fileID: 0}
604+
m_SelectOnDown: {fileID: 0}
605+
m_SelectOnLeft: {fileID: 0}
606+
m_SelectOnRight: {fileID: 0}
607+
m_Transition: 1
608+
m_Colors:
609+
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
610+
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
611+
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
612+
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
613+
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
614+
m_ColorMultiplier: 1
615+
m_FadeDuration: 0.1
616+
m_SpriteState:
617+
m_HighlightedSprite: {fileID: 0}
618+
m_PressedSprite: {fileID: 0}
619+
m_SelectedSprite: {fileID: 0}
620+
m_DisabledSprite: {fileID: 0}
621+
m_AnimationTriggers:
622+
m_NormalTrigger: Normal
623+
m_HighlightedTrigger: Highlighted
624+
m_PressedTrigger: Pressed
625+
m_SelectedTrigger: Selected
626+
m_DisabledTrigger: Disabled
627+
m_Interactable: 1
628+
m_TargetGraphic: {fileID: 976685860}
629+
m_OnClick:
630+
m_PersistentCalls:
631+
m_Calls:
632+
- m_Target: {fileID: 2105647632}
633+
m_TargetAssemblyTypeName: AudioManager, Assembly-CSharp
634+
m_MethodName: PlayCDN
635+
m_Mode: 1
636+
m_Arguments:
637+
m_ObjectArgument: {fileID: 0}
638+
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
639+
m_IntArgument: 0
640+
m_FloatArgument: 0
641+
m_StringArgument:
642+
m_BoolArgument: 0
643+
m_CallState: 2
644+
--- !u!114 &976685860
645+
MonoBehaviour:
646+
m_ObjectHideFlags: 0
647+
m_CorrespondingSourceObject: {fileID: 0}
648+
m_PrefabInstance: {fileID: 0}
649+
m_PrefabAsset: {fileID: 0}
650+
m_GameObject: {fileID: 976685857}
651+
m_Enabled: 1
652+
m_EditorHideFlags: 0
653+
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
654+
m_Name:
655+
m_EditorClassIdentifier:
656+
m_Material: {fileID: 0}
657+
m_Color: {r: 1, g: 1, b: 1, a: 1}
658+
m_RaycastTarget: 1
659+
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
660+
m_Maskable: 1
661+
m_OnCullStateChanged:
662+
m_PersistentCalls:
663+
m_Calls: []
664+
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
665+
m_Type: 1
666+
m_PreserveAspect: 0
667+
m_FillCenter: 1
668+
m_FillMethod: 4
669+
m_FillAmount: 1
670+
m_FillClockwise: 1
671+
m_FillOrigin: 0
672+
m_UseSpriteMesh: 0
673+
m_PixelsPerUnitMultiplier: 1
674+
--- !u!222 &976685861
675+
CanvasRenderer:
676+
m_ObjectHideFlags: 0
677+
m_CorrespondingSourceObject: {fileID: 0}
678+
m_PrefabInstance: {fileID: 0}
679+
m_PrefabAsset: {fileID: 0}
680+
m_GameObject: {fileID: 976685857}
681+
m_CullTransparentMesh: 0
468682
--- !u!1 &1026316876
469683
GameObject:
470684
m_ObjectHideFlags: 0
@@ -662,7 +876,7 @@ RectTransform:
662876
m_Children:
663877
- {fileID: 1026316877}
664878
m_Father: {fileID: 2105647631}
665-
m_RootOrder: 4
879+
m_RootOrder: 5
666880
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
667881
m_AnchorMin: {x: 0.5, y: 0.5}
668882
m_AnchorMax: {x: 0.5, y: 0.5}
@@ -970,7 +1184,7 @@ RectTransform:
9701184
m_Children:
9711185
- {fileID: 1260414952}
9721186
m_Father: {fileID: 2105647631}
973-
m_RootOrder: 2
1187+
m_RootOrder: 3
9741188
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
9751189
m_AnchorMin: {x: 0.5, y: 0.5}
9761190
m_AnchorMax: {x: 0.5, y: 0.5}
@@ -1184,7 +1398,7 @@ RectTransform:
11841398
m_Children:
11851399
- {fileID: 2074957559}
11861400
m_Father: {fileID: 2105647631}
1187-
m_RootOrder: 5
1401+
m_RootOrder: 6
11881402
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
11891403
m_AnchorMin: {x: 0, y: 1}
11901404
m_AnchorMax: {x: 0, y: 1}
@@ -1466,7 +1680,7 @@ RectTransform:
14661680
m_Children:
14671681
- {fileID: 945804510}
14681682
m_Father: {fileID: 2105647631}
1469-
m_RootOrder: 0
1683+
m_RootOrder: 1
14701684
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
14711685
m_AnchorMin: {x: 0.5, y: 0.5}
14721686
m_AnchorMax: {x: 0.5, y: 0.5}
@@ -1600,7 +1814,7 @@ RectTransform:
16001814
m_Children:
16011815
- {fileID: 1360654995}
16021816
m_Father: {fileID: 2105647631}
1603-
m_RootOrder: 1
1817+
m_RootOrder: 2
16041818
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
16051819
m_AnchorMin: {x: 0.5, y: 0.5}
16061820
m_AnchorMax: {x: 0.5, y: 0.5}
@@ -1877,6 +2091,7 @@ RectTransform:
18772091
m_LocalScale: {x: 0, y: 0, z: 0}
18782092
m_ConstrainProportionsScale: 0
18792093
m_Children:
2094+
- {fileID: 976685858}
18802095
- {fileID: 1970663474}
18812096
- {fileID: 2039791428}
18822097
- {fileID: 1302124276}
@@ -1903,9 +2118,9 @@ MonoBehaviour:
19032118
m_Script: {fileID: 11500000, guid: 425996276dd1f4577a5c2b9ca9030287, type: 3}
19042119
m_Name:
19052120
m_EditorClassIdentifier:
1906-
AudioSource: {fileID: 587693937}
1907-
AudioClipLong: {fileID: 8300000, guid: 58afc26fffc594f129f62de04b9c7d5d, type: 3}
1908-
AudioClipShort: {fileID: 8300000, guid: ac9b7516255724a1b8e79bd67e717b6c, type: 3}
2121+
audioSource: {fileID: 587693937}
2122+
audioClipLong: {fileID: 8300000, guid: 58afc26fffc594f129f62de04b9c7d5d, type: 3}
2123+
audioClipShort: {fileID: 8300000, guid: ac9b7516255724a1b8e79bd67e717b6c, type: 3}
19092124
--- !u!114 &2105647633
19102125
MonoBehaviour:
19112126
m_ObjectHideFlags: 0
Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,84 @@
1+
using System;
12
using System.Collections;
23
using System.Collections.Generic;
34

45
using UnityEngine;
5-
6-
using WeChatWASM;
6+
using UnityEngine.Audio;
7+
using UnityEngine.Networking;
78

89
public class AudioManager : MonoBehaviour
910
{
10-
public AudioSource AudioSource;
11-
public AudioClip AudioClipLong;
12-
public AudioClip AudioClipShort;
11+
public AudioSource audioSource;
12+
public AudioClip audioClipLong;
13+
public AudioClip audioClipShort;
14+
private AudioClip audioClipCDN;
15+
16+
17+
void Start()
18+
{
19+
StartCoroutine(LoadCDNAudio());
20+
}
21+
22+
IEnumerator LoadCDNAudio()
23+
{
24+
string uriString = "https://res.wx.qq.com/wechatgame/product/webpack/userupload/20220901/211827/CallMeTeenTop.mp3";
25+
Uri uri = new Uri(uriString);
26+
UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(uri, AudioType.MPEG);
27+
yield return request.SendWebRequest();
28+
if (request.result == UnityWebRequest.Result.Success)
29+
{
30+
audioClipCDN = DownloadHandlerAudioClip.GetContent(request);
31+
// yield return new WaitUntil(() => audioClipCDN.loadState == AudioDataLoadState.Loaded);
32+
// Debug.Log("audioClipCDN loaded, clip length: " + audioClipCDN.length);
33+
}
34+
else
35+
{
36+
Debug.Log("request error: " + request.error);
37+
}
38+
}
39+
40+
public void PlayCDN()
41+
{
42+
if (audioClipCDN != null)
43+
{
44+
audioSource.clip = audioClipCDN;
45+
audioSource.loop = false;
46+
audioSource.Play();
47+
}
48+
}
1349

1450
public void PlayLong()
1551
{
16-
AudioSource.clip = AudioClipLong;
17-
AudioSource.loop = true;
18-
AudioSource.Play();
52+
if (audioClipLong != null)
53+
{
54+
audioSource.clip = audioClipLong;
55+
audioSource.loop = true;
56+
audioSource.Play();
57+
}
1958
}
2059

2160
public void PlayShort()
2261
{
23-
AudioSource.clip = AudioClipShort;
24-
AudioSource.loop = false;
25-
AudioSource.Play();
62+
if (audioClipShort != null)
63+
{
64+
audioSource.clip = audioClipShort;
65+
audioSource.loop = false;
66+
audioSource.Play();
67+
}
2668
}
2769

2870
public void Pause()
2971
{
30-
AudioSource.Pause();
72+
audioSource.Pause();
3173
}
3274

3375
public void Resume()
3476
{
35-
AudioSource.UnPause();
77+
audioSource.UnPause();
3678
}
3779

3880
public void Stop()
3981
{
40-
AudioSource.Stop();
82+
audioSource.Stop();
4183
}
4284
}

0 commit comments

Comments
 (0)