Skip to content

Commit 3bf061d

Browse files
committedApr 13, 2023
add slides for audio mixer and imgui
1 parent 5a49f7f commit 3bf061d

13 files changed

+414
-97
lines changed
 

‎math/5-math-classes-slides.html

+15-10
Large diffs are not rendered by default.

‎math/5-math-classes.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Math. Unity Math classes
33
marp: true
44
paginate: true
5+
math: mathjax
56
---
67
<!-- headingDivider: 3 -->
78
<!-- class: invert -->
@@ -43,9 +44,11 @@ paginate: true
4344
### `Random.Range`
4445

4546
* [Script Reference: Random.Range](https://docs.unity3d.com/ScriptReference/Random.Range.html)
46-
* **Note:** For ints, the maximum value is ***exclusive***, for float it's ***inclusive***!
47-
* `Random.Range(0,10)` returns random integer values 0,1,...,9f
48-
* `Random.Range(0f,10f)` returns random float values 0f,...,10f
47+
* **Note:** For integers, the maximum value is ***exclusive*** (it's not included!)
48+
* For floats it's ***inclusive*** (it IS included!)
49+
* For example:
50+
* `Random.Range(0,10)` returns random integer values $0,\dots,9$ (not $10$!)
51+
* `Random.Range(0f,10f)` returns random float values $0,\dots,10$
4952

5053
## `Quaternion` class
5154

‎temp/really-temp/important-unity-classes.md

-19
This file was deleted.

‎tooltips/vscode-setup.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ paginate: true
8383
```
8484
8585
## Extra: Other extensions
86-
8786
<!-- _backgroundColor: #5d275d -->
87+
8888
* *Marp for VS Code*
8989
* If you want to read these slides inside VS Code
9090
* After installation, open this .md file from the course repository

‎unity-cookbook/UI-slides.html

+65-35
Large diffs are not rendered by default.

‎unity-cookbook/UI.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,34 @@ Try out setting RectTransform anchor points and see how the UI changes when you
338338

339339
* [Unity UI manual: Designing UI for multiple resolutions](https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/HOWTO-UIMultiResolution.html)
340340
* [Unity UI manual: Creating UI elements from scripting](https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/HOWTO-UICreateFromScripting.html)
341+
342+
## Extra: IMGUI - Immediate Mode Graphical User Interface
343+
<!-- _backgroundColor: #5d275d -->
344+
345+
* Code-driven system to create UI elements quickly
346+
* [Manual: GUI scripting guide](https://docs.unity3d.com/Manual/GUIScriptingGuide.html)
347+
* [ScriptReference: GUI](https://docs.unity3d.com/ScriptReference/GUI.html)
348+
* Example: Button
349+
```c#
350+
if (GUI.Button(new Rect(100, 30, 150, 30), "Do a thing!"))
351+
{
352+
// When button is pressed, do a thing here
353+
}
354+
```
355+
* Example: Input Field
356+
```c#
357+
value = GUI.TextField(new Rect(300, 30, 200, 20), value, 25);
358+
```
359+
360+
---
361+
362+
![width:700px](imgs/imgui.png)
363+
* This is the kind of elements you can create with IMGUI.
364+
365+
## Extra: UI toolkit
366+
<!-- _backgroundColor: #5d275d -->
367+
341368
* [UI Toolkit](https://docs.unity3d.com/Manual/UIElements.html)
342369
* Collection of features, resources, and tools for UI
343-
* Create UIs with style sheets
370+
* Create UIs with style sheets
371+

‎unity-cookbook/audio-slides.html

+223-22
Large diffs are not rendered by default.

‎unity-cookbook/audio.md

+75-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
title: Unity Cookbook. Audio playback
33
marp: true
44
paginate: true
5+
style: |
6+
.columns {
7+
display: grid;
8+
grid-template-columns: repeat(2, minmax(0, 1fr));
9+
gap: 1rem;
10+
}
511
---
612
<!-- headingDivider: 3 -->
713
<!-- class: invert -->
@@ -19,14 +25,21 @@ paginate: true
1925
## AudioSource
2026

2127
* You can have multiple of these
22-
* [Script Reference: AudioSource class](https://docs.unity3d.com/ScriptReference/AudioSource.html)
2328
* [Manual: AudioSource component](https://docs.unity3d.com/Manual/class-AudioSource.html)
2429
* Insert to every GameObject that should be able to make a sound
2530
* E.g., if a Coin GameObject should have a *\*bling\** sound effect when collected
26-
* The *bling.wav* sound effect is drag-and-dropped into the component
27-
* most important functions to control the component: `.Play()`, `.Pause()`, `.Stop()`
31+
* Drag and drop a *bling.wav* sound effect file into the component
32+
* Supported audio file formats are listed here: [Manual: Audio Files](https://docs.unity3d.com/Manual/AudioFiles.html)
33+
34+
### Controlling audio with code
35+
36+
* [Script Reference: AudioSource class](https://docs.unity3d.com/ScriptReference/AudioSource.html)
37+
* The most important functions to control the component are:
38+
* `.Play()`
39+
* `.Pause()`
40+
* `.Stop()`
2841
```c#
29-
public AudioSource audio;
42+
[SerializeField] AudioSource audio;
3043
if (PlayerGrabbedCoin)
3144
audio.Play();
3245
if (DoSomethingElse)
@@ -72,6 +85,62 @@ Then, add sounds to your game. Try at least these two kinds of sound effects:
7285
1) a one-time sound effect that can be triggered with code
7386
2) looping sound that plays all the time
7487

75-
## Playing sound when something gets destroyed
88+
## Note: Playing sound when something gets destroyed
89+
90+
* If you add an audio source to a GameObject that gets destroyed when the sound should be played, the sound stops abruptly
91+
* Instead, add the audio source to some GameObject that you know won't be destroyed
92+
* E.g, for playing a sound when collecting a coin, add a CoinPlayer GameObject with an Audiosource component, and call its `.Play()` method when collecting the coin.
93+
94+
## Fading between volume levels
95+
96+
There are many ways to fade in/out audio. Here, we present an example that needs very minimal code.
97+
98+
* Create a new audio mixer asset: *Create > Audio Mixer*.
99+
* Name it "FadeMixer" or something like that.
100+
![](imgs/audiomixerasset.png)
101+
* Click *Open*
102+
103+
---
104+
105+
![width:500px](imgs/audiomixer.png)
106+
107+
* In the Mixer view:
108+
* Name the existing Snapshot to *VolumeOn*
109+
* Create a new snapshot *VolumeOff*
110+
* Set its volume in the *Master* mixer group's slider to -80dB.
111+
* Right click *VolumeOff* and select *Set as start Snapshot*.
112+
113+
---
114+
115+
* In project view, expand the mixer asset to see the *Master* mixer group and the two snapshots *VolumeOn* and *VolumeOff*.
116+
* Drag the *Master* group to the audio source component that you want to fade.
117+
* Then, in the script where you want to control the fade, create a new variable:
118+
```c#
119+
using UnityEngine.Audio;
120+
...
121+
[SerializeField] AudioMixerSnapshot VolumeOn;
122+
```
123+
* Drag the *VolumeOn* snapshot to the inspector to the corresponding variable.
124+
* Then, we can fade from the default *VolumeOff* snapshot to the *VolumeOn* snapshot by calling the method
125+
```c#
126+
VolumeOn.TransitionTo(3); // fade takes 3 seconds
127+
```
128+
129+
---
130+
131+
<div class="columns" markdown="1">
132+
<div markdown="1">
133+
134+
![](imgs/audiomixerasset-expanded.png)
135+
136+
Here's what the audio mixer asset looks like when expanded.
137+
138+
</div>
139+
<div markdown="1">
140+
141+
![](imgs/audiomixer-inspectorvalues.png)
142+
143+
Here, the *Master* audio mixer group is dragged to Audio Source, and the *VolumeOn* snapshot to SceneManager.
76144

77-
* xx
145+
</div>
146+
</div>
Loading

‎unity-cookbook/imgs/audiomixer.png

27 KB
Loading
7.15 KB
Loading
11 KB
Loading

‎unity-cookbook/imgs/imgui.png

14.8 KB
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.