Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Mr BigSock
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gard Aleksander Furre
Mr BigSock
Commits
b550d4fd
Commit
b550d4fd
authored
2 years ago
by
Robin Halseth Sandvik
Browse files
Options
Downloads
Patches
Plain Diff
Added AudioService for manaing sound in code.
parent
9bcc2e44
No related branches found
Branches containing commit
No related tags found
1 merge request
!60
Upgrades people.
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
MrBigsock/Assets/Code/Services/AudioService.cs
+69
-0
69 additions, 0 deletions
MrBigsock/Assets/Code/Services/AudioService.cs
MrBigsock/Assets/Code/Services/AudioService.cs.meta
+11
-0
11 additions, 0 deletions
MrBigsock/Assets/Code/Services/AudioService.cs.meta
with
80 additions
and
0 deletions
MrBigsock/Assets/Code/Services/AudioService.cs
0 → 100644
+
69
−
0
View file @
b550d4fd
using
System.Collections
;
using
System
;
using
System.Linq
;
using
System.Collections.Generic
;
using
System.Reflection
;
using
UnityEngine
;
using
UnityEngine.InputSystem
;
using
UnityEditor
;
using
BigSock.Item
;
namespace
BigSock.Service
{
/*
Service for handling audio.
*/
public
partial
class
AudioService
{
/*
The instance to use.
*/
public
static
readonly
AudioService
SINGLETON
=
new
AudioService
();
/*
Get a audio of a name.
*/
public
AudioClip
Get
(
string
name
)
{
if
(
_items
.
TryGetValue
(
_sanitize
(
name
),
out
var
res
))
return
res
;
return
null
;
}
}
public
partial
class
AudioService
{
private
Dictionary
<
string
,
AudioClip
>
_items
=
new
Dictionary
<
string
,
AudioClip
>();
private
System
.
Random
_rnd
=
new
System
.
Random
();
private
AudioService
()
{
_loadItems
();
}
/*
Load the items into the dictionary.
Based on: https://stackoverflow.com/a/67670629
*/
private
void
_loadItems
()
{
string
[]
guids
=
AssetDatabase
.
FindAssets
(
"t:AudioClip"
,
new
string
[]
{
"Assets/sound"
}
);
var
dict
=
new
Dictionary
<
string
,
AudioClip
>();
foreach
(
var
guid
in
guids
)
{
var
path
=
AssetDatabase
.
GUIDToAssetPath
(
guid
);
var
name
=
_sanitize
(
path
.
Replace
(
".wav"
,
""
).
Replace
(
".mp3"
,
""
).
Replace
(
"Assets/sound/"
,
""
));
AudioClip
go
=
AssetDatabase
.
LoadAssetAtPath
<
AudioClip
>(
path
);
//Debug.Log($"[SpriteService._loadItems()] {name}");
dict
[
name
]
=
go
;
}
_items
=
dict
;
}
private
string
_sanitize
(
string
name
)
=>
name
.
Replace
(
" "
,
""
).
Replace
(
"_"
,
""
).
ToLower
();
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
MrBigsock/Assets/Code/Services/AudioService.cs.meta
0 → 100644
+
11
−
0
View file @
b550d4fd
fileFormatVersion: 2
guid: bace12991ada10e438cef9039856ed10
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment