C# Cheat Sheet Unity

Posted : admin On 1/2/2022

This list will continue to get updated as I discover more tips and tricks for how to integrate audio into Unity! Anyone is welcome to use these codes to learn Wwise themselves.

You may have gotten through the 101, 201 and 251 courses through Audiokinetic's website, but you still might not know how to actually INTEGRATE these Events, States and RTPC's you set up into Unity. I don't blame you - this information is for some reason, incredibly difficult to find online. But you need to know, in order to get your Wwise integrated into Unity. So let's get started!

Fundamental Integration through C#

In Unity, the most common way of using Wwise is by calling upon the 'AkSoundEngine.____' in your C# Scripts.

This does mean that it will immensely help you if you understand the basics of C# and how scripts work. I'd highly recommend you take your time to learn what void, Start, Update, if, how functions work and reading scripts. While you won't be doing coding most of the time, you will be searching for appropriate places to insert these codes. In many of your games, you'll have to ask your programmer for instructions on which scripts you need to squeeze these in. But, if they're organized programmers and if you're keeping track of where everything is, you won't need to waste their time asking for obvious places to code them.
What you want Wwise to do is now up to you. That ______ can be filled out by you to determine what you want Wwise to do. The most basic ones that you'll use is PostEvent, SetRTPCValue, SetState.

  1. Site d'information de la Commission d'Utilisation Nationale Chiens de Berger et de Garde (cun-cbg); commission de la SOCIETE CENTRALE CANINE (SCC).
  2. The degree Celsius is a unit of temperature on the Celsius scale, a temperature scale originally known as the centigrade scale. The degree Celsius (symbol: °C) can refer to a specific temperature on the Celsius scale or a unit to indicate a difference or range between two temperatures.

2912 views on Imgur: The magic of the Internet. Article by imgur. C Sharp Programming Programming Tutorial Drawing For Beginners Game Dev Cheat Sheets App Development Game Design Cheating Trending Memes. Introduced in 1985, the model line replaced the locally produced C10 (derived from the first-generation 1964 C/K). The model line was marketed under three model series, designated by fuel (A=ethanol/gasoline flex-fuel, C=gasoline, D=diesel).

AkSoundEngine.PostEvent( ' EventName' , gameObject);

For sending out a message to Wwise to trigger an Event, you use PostEvents. This is good for one-time actions.
Useful for: Collision sounds, button click sounds, weapon fire sound, you can even use it to trigger the music to play at the Start of your game.

AkSoundEngine.SetState ('State Group name', 'State Name');

For sending out a message to Wwise to set a State. This is great for letting Wwise know what state you'd like to be in.
Useful for: Changing your adaptive music with States. Telling Wwise what Level you are, whether your game is Paused or not, whether you're underwater or not.

AkSoundEngine.SetRTPC( 'RTPC Name in Wwise', variableNameInUnity);

For setting RTPC (Real Time Parameter Control) of any value that you'd like to be reflected by a value in Unity, you can use this.
Useful for: Any numerical value, like Health, How many kills you've gotten, your high score, your car's speed, setting the volume of your SFX or Music channel in the options menu.

In the image below, you can see that there are several lines of AkSoundEngine scripts I put into my 'State_InGame' script in Unity.

1. AkSoundEngine. is calling Wwise.

2. SetState( ) is telling Wwise to prepare to do an action: set State. But now you have to tell Wwise which state group and to what state you'd like to target.

3. ('Group', 'State'); In each quotation marks, you can put the name of your Group and the name of your State.

Now, your script is ready. What should we do with it? Let's take that script, and attach it to anything you want. I like to give my scripts that set states into a GameStateSetter game object, so that I can always look for it quickly. Since the void Start () function will trigger at the very first frame when this gameobject loads into the scene, Unity will tell Wwise to set your state and play the corresponding music.

That's really about as hard as it gets. Congratulations! You now understand the fundamentals of actual integration.

General Tips & Tricks

0. Connect to your Unity and use Wwise in real time. Man, did this blow my mind when I first understood what 'Connect' and 'Remote' really meant. You can use this to mix audio in your game in real time as you're playtesting the game. Furthermore, it allows you to monitor how many 'voices' are active in your game and how much processing power your audio is using. It's so useful.

1. Names of Events, State Groups, RTPC's are all in ' '. Names of your in-game variables are not. For example, below, you can see that my 'MusicVolume' is in quotations. This script sets the 'MusicVolume' to musicVolume. This is how you do Volume settings, by the way.

2. Calling Game events. You have to put 'gameObject' as the target for posting your event. This means that whatever Event you just posted, it'll associate itself with the gameObject that this script is attached to. Without this later part, your PostEvent script will not function.

3. The order of your actions matter. In this case, Sprinkler sound should play first, then Setting Valve, then finally Set State to On. If I put Set State to On first and then Sprinkler, Sprinkler plays at 0.

4. Ways to integrate Ambient sounds into your game. Notice that Wwise Picker tab that appeared in your Unity project. Most of the times, you won't have to deal with it. However, there are sometimes like setting Ambiences or Reverb zones when you do want to drag objects from this bar directly onto your objects. If it's not an ambient sound that will be emitted from an object, you don't have to place the Event into an object to get it to play. This way, sounds won't just burst out at the beginning of your game. (Since you have to load it by Start, Awake or one of the Collision options).

This was a confusion that I had because when I was first learning to use PostEvent, I watched a woman integrate sounds onto her cubes as her ball was rolling into them to pick them up. (If you've watched this video). She has it set so that these sounds will play when these objects are being Destroyed. Unless you want your sounds to play when your objects are being destroyed, you do NOT want to integrate your sounds this way. Attaching it on a script that says 'AkSoundEngine.PostEvent('Pickup', gameObject); and then having these functions activate is better practice.

5. When working with buttons, if you want a previously playing music to stop, you must add a Stop action to the Event, and make sure that its Scope is set to Global and not GameObject.

6. Attach an AkGameObj script onto a prefab or a gameObject in order to store information like Events, RTPC, and position!

7. Adding a brief pause function so that it only detects the first moment of an action- For example: You have a bird that accelerates when you click and hold right click. The moment you press, you want a blast of air sound to go off, but don't want it to be played every frame while you're holding right click.

This is great because you can call upon the void stop(); later on to pause your current script, which will resume after that amount of specified time.

8. Adding 'Typing' sound effects to only Keyboard inputs. Call any KeyDown, and exclude Mouse 1 and Mouse 2. If you want a 'Submit' sound, add it to the script that actually disables/destroys this script. Adding it here won't be fast enough.

In the C programming language, operations can be performed on a bit level using bitwise operators.

Bitwise operations are contrasted by byte-level operations which characterize the bitwise operators' logical counterparts, the AND, OR and NOT operators. Instead of performing on individual bits, byte-level operators perform on strings of eight bits (known as bytes) at a time. The reason for this is that a byte is normally the smallest unit of addressable memory (i.e. data with a unique memory address).

This applies to bitwise operators as well, which means that even though they operate on only one bit at a time they cannot accept anything smaller than a byte as their input.

All of these operators are also available in C++, and many C-family languages.

Bitwise operators[edit]

C provides six operators for bit manipulation.[1]

SymbolOperator
&bitwise AND
bitwise inclusive OR
^bitwise XOR (exclusive OR)
<<left shift
>>right shift
~bitwise NOT (one's complement) (unary)

Bitwise AND &[edit]

bit abit ba & b (a AND b)
000
010
100
111

The bitwise AND operator is a single ampersand: &. It is just a representation of AND which does its work on the bits of the operands rather than the truth value of the operands. Bitwise binary AND does the logical AND (as shown in the table above) of the bits in each position of a number in its binary form.

For instance, working with a byte (the char type):

The most significant bit of the first number is 1 and that of the second number is also 1 so the most significant bit of the result is 1; in the second most significant bit, the bit of second number is zero, so we have the result as 0. [2]

Bitwise OR [edit]

bit abit ba b (a OR b)
000
011
101
111

Similar to bitwise AND, bitwise OR only operates at the bit level. Its result is a 1 if either of the bits is 1 and zero only when both bits are 0. Its symbol is which can be called a pipe.

Bitwise XOR ^[edit]

bit abit ba ^ b (a XOR b)
000
011
101
110

The bitwise XOR (exclusive or) performs a logical XOR function, which is equivalent to adding two bits and discarding the carry. The result is zero only when we have two zeroes or two ones.[3] XOR can be used to toggle the bits between 1 and 0. Thus i = i ^ 1 when used in a loop toggles its values between 1 and 0.[4]

Bitwise NOT ~ / ones' complement (unary)[edit]

C# Cheat Sheet Unity

The bitwise complement operator, the tilde, ~, flips every bit. A useful way to remember this is that the tilde is sometimes called a twiddle, and the bitwise complement twiddles every bit: if you have a 1, it's a 0, and if you have a 0, it's a 1.

This turns out to be a great way of finding the largest possible value for an unsigned number:

1unsigned intmax = ~0;

0, of course, is all 0s: 00000000 00000000. Once we twiddle 0, we get all 1s: 11111111 11111111. Since max is an unsigned int, we don't have to worry about sign bits or twos complement. We know that all 1s is the largest possible number.

Note that ~ and ! cannot be used interchangeably. When you take the logical NOT of a non-zero number, you get 0 (FALSE). However, when you twiddle a non-zero number, the only time you'll get 0 is when every bit is turned on. (This non-equivalence principle holds true for bitwise AND too, unless you know that you are using strictly the numbers 1 and 0. For bitwise OR, to be certain that it would be equivalent, you'd need to make sure that the underlying representation of 0 is all zeros to use it interchangeably. But don't do that! It'll make your code harder to understand.)

Now that we have a way of flipping bits, we can start thinking about how to turn off a single bit. We know that we want to leave other bits unaffected, but that if we have a 1 in the given position, we want it to be a 0. Take some time to think about how to do this before reading further.

We need to come up with a sequence of operations that leaves 1s and 0s in the non-target position unaffected; before, we used a bitwise OR, but we can also use a bitwise AND. 1 AND 1 is 1, and 0 AND 1 is 0. Now, to turn off a bit, we just need to AND it with 0: 1 AND 0 is 0. So if we want to indicate that car 2 is no longer in use, we want to take the bitwise AND of XXXXX1XX with 11111011.

How can we get that number? This is where the ability to take the complement of a number comes in handy: we already know how to turn a single bit on. If we turn one bit on and take the complement of the number, we get every bit on except that bit:

1~(1<<position)

Now that we have this, we can just take the bitwise AND of this with the current field of cars, and the only bit we'll change is the one of the car_num we're interested in.

1

2

3

4

voidset_unused(intcar_num)

{

in_use = in_use & ~(1<<car_num);

}

You might be thinking to yourself, but this is kind of clunky. We actually need to know whether a car is in use or not (if the bit is on or off) before we can know which function to call. While this isn't necessarily a bad thing, it means that we do need to know a little bit about what's going on. There is an easier way, but first we need the last bitwise operator: exclusive-or.

Shift operators[edit]

There are two bitwise shift operators. They are

  • Right shift (>>)
  • Left shift (<<)

Right shift >>[edit]

The symbol of right shift operator is >>. For its operation, it requires two operands. It shifts each bit in its left operand to the right.The number following the operator decides the number of places the bits are shifted (i.e. the right operand).Thus by doing ch >> 3 all the bits will be shifted to the right by three places and so on.

However, do note that a shift operand value which is either a negative number or is greater than or equal to the total number of bits in this value results in undefined behavior. For example, when shifting a 32 bit unsigned integer, a shift amount of 32 or higher would be undefined.

Example:

If the variable ch contains the bit pattern 11100101, then ch >> 1 will produce the result 01110010, and ch >> 2 will produce 00111001.

Here blank spaces are generated simultaneously on the left when the bits are shifted to the right. When performed on an unsigned type or a non-negative value in an signed type, the operation performed is a logical shift, causing the blanks to be filled by 0s (zeros). When performed on a negative value in a signed type, the result is technically implementation-defined (compiler dependent),[5] however most compilers will perform an arithmetic shift, causing the blank to be filled with the set sign bit of the left operand.

Right shift can be used to divide a bit pattern by 2 as shown:

Right shift operator usage[edit]

Typical usage of a right shift operator in C can be seen from the following code.

Example:

The output of the above program will be

Left shift <<[edit]

The symbol of left shift operator is <<. It shifts each bit in its left-hand operand to the left by the number of positions indicated by the right-hand operand. It works opposite to that of right shift operator. Thus by doing ch << 1 in the above example we have 11001010.Blank spaces generated are filled up by zeroes as above.

However, do note that a shift operand value which is either a negative number or is greater than or equal to the total number of bits in this value results in undefined behavior. This is defined in the standard at ISO 9899:2011 6.5.7 Bit-wise shift operators.For example, when shifting a 32 bit unsigned integer, a shift amount of 32 or higher would be undefined.

Left shift can be used to multiply an integer by powers of 2 as in

Example: a simple addition program[edit]

The following program adds two operands using AND, XOR and left shift (<<).

Bitwise assignment operators[edit]

C provides a compound assignment operator for each binary arithmetic and bitwise operation (i.e. each operation which accepts two operands). Each of the compound bitwise assignment operators perform the appropriate binary operation and store the result in the left operand.[6]

The bitwise assignment operators are as follows:

SymbolOperator
&=bitwise AND assignment
=bitwise inclusive OR assignment
^=bitwise exclusive OR assignment
<<=left shift assignment
>>=right shift assignment

Logical equivalents[edit]

Four of the bitwise operators have equivalent logical operators. They are equivalent in that they have the same truth tables. However, logical operators treat each operand as having only one value, either true or false, rather than treating each bit of an operand as an independent value. Logical operators consider zero false and any nonzero value true. Another difference is that logical operators perform short-circuit evaluation.

The table below matches equivalent operators and shows a and b as operands of the operators.

C to f
BitwiseLogical
a & ba && b
a ba b
a ^ ba != b
~a!a

!= has the same truth table as ^ but unlike the true logical operators, by itself != is not strictly speaking a logical operator. This is because a logical operator must treat any nonzero value the same. To be used as a logical operator != requires that operands be normalized first. A logical not applied to both operands won’t change the truth table that results but will ensure all nonzero values are converted to the same value before comparison. This works because ! on a zero always results in a one and ! on any nonzero value always results in a zero.

C-diff Bacteria Infection

Example:

The output of the above program will be

See also[edit]

Vie

References[edit]

  1. ^Kernighan; Dennis M. Ritchie (March 1988). The C Programming Language (2nd ed.). Englewood Cliffs, NJ: Prentice Hall. ISBN0-13-110362-8. Archived from the original on 2019-07-06. Retrieved 2019-09-07.CS1 maint: discouraged parameter (link) Regarded by many to be the authoritative reference on C.
  2. ^ ab'Tutorials - Bitwise Operators and Bit Manipulations in C and C++'. cprogramming.com.
  3. ^'Exclusive-OR Gate Tutorial'. Basic Electronics Tutorials.
  4. ^'C++ Notes: Bitwise Operators'. fredosaurus.com.
  5. ^'ISO/IEC 9899:2011 - Information technology -- Programming languages -- C'. www.iso.org.
  6. ^'C/C++ Compound assignment operators'. XL C/C++ V8.0 for AIX. IBM. Retrieved 11 November 2013.CS1 maint: discouraged parameter (link)

External links[edit]

Unity C# Cheat Sheet 2020

Retrieved from 'https://en.wikipedia.org/w/index.php?title=Bitwise_operations_in_C&oldid=1007657832'