Monster shadow

VIDEO

Helper functions I can't live without in Unity

Description:
Today I share with you some of my beloved helper functions. I use these in every Unity project and I'm sure they'll help you out, too!
ADD A COMMENT

Tarodev
Camera.main is not necessary expensive anymore, but I still use this helper method as it's easy access.
K Ken
I was told that in 2019 unity has auto cache the main camera for you. Do you know if this is true?
Graig
Hey dude... Good video but at 1:36 you never unblur the helper version of the coroutine.... I cant see how you call the waitforseconds....
Jorhoto
Hi, for the camera thing you can also do in a single line: public static Camera Camera => _camera ??= Camera.main; *Unity 2020.2 or higher. Thanks for your great videos and tips ;)
Tarodev
@Thomas Ingram I really dislike the operator override for null... I really confuses new devs (at least on my discord).
Thomas Ingram
You should avoid using modern null checking operators with Unity objects because Unity Objects evaluate to null without actually being null, and that behaviour is only overridden for equality. Null conditional (?.) and the null coalescing (??, ??=) operators could not be overridden by Unity. They will only work correctly when references are explicitly null, so if you changed scenes (destroying the old main camera) this would fail.
Tarodev
That's beautiful.
Jugibur
I'm new to c# and Unity and try to understand what's going on in the last method "DeleteChildren()" 1.) If I understand correctly, "this" extends the transform component with a "DeleteChildren" method. 2.) I'm confused why and since when the transform component have children (why not use gameObject instead?) 3.) This method isn't called recursive, so how can it also delete sub children? (you mentioned a complete level clean up with giving the parent object) Unfortunately I'm not a native english speaker and perhaps I misunderstand this concept...
Tarodev
@Jugibur Yup! I see what you're saying though... It makes sense that a function which destroys gameObjects should take in a gameObject. Totally up to you if you'd prefer it that way and just do gameObject.transform for the loop :)
Jugibur
@Tarodev Thanks for your explainations (and all the good stuff!) PS: Meanwhile I have read that every component has a link to it's gameObject, so I understand that construction.
Tarodev
1. The 'this' keyword is used in extention methods to allow the method to be chained directly from the object. For example transform.MyNewFunction. We have added a function to the transform type. 2. We take in transform as that's what we use to iterate children. We can't do it directly from the game object. But it's personal preference if you'd like to take in a game object. 3. I'm sure after I say this you'll think it's super obvious, but if you destroy a game object it also destroys all its children, so no need for recursion.
meowsqueak
Just a note for Unity 2020.2 and newer with regards to the first tip (Camera Reference): "The new approach stores a dedicated list of objects with the MainCamera tag, and does not use a secondary array of potential matches. Instead, the list is queried directly, and as soon as a match is found, it is returned. All objects that are considered are objects with the MainCamera tag, so the chance of success is much higher. In contrived test cases containing 50,000 objects, we saw speed increase by 21,000x to 51,000x." https://blog.unity.com/technology/new-performance-improvements-in-unity-2020-2 That said, it's arguably still faster to cache it yourself if you're sure you only have one camera, as you show here.
Tarodev
This is great info. I wonder if they do any internal caching for GetComponent...
Baris Dincer
Wow! That's definitely a video I'll add to my bookmarks :)
Tarodev
Glad you found something useful ❤️
Atef Sassi
awesome ! thank you for sharing
Tarodev
Hope some of them help you out :)
srisrid multi purpose channel
make a series about the basics of c# and unity.
srisrid multi purpose channel
@Tarodev ohhhh good :)
Tarodev
You read my mind ;)
srisrid multi purpose channel
good vid subscribes. I am a beginner in unity by rhe way.
Tarodev
You're at the beginning of a wonderful journey 😊 good luck!
SEE ALL COMMENTS


Transcript:

so today i'll be showing you some functions that i use across
all of my projects basically i'll create a class called a static class called
helpers um i know other people use different
names like extensions and stuff but that doesn't necessarily make sense as
they're not extension methods some of them are
but these methods are super helpful and i
i copy and paste them it's like the first thing i do when i start a new
project so here we go so this first one is also the
simplest uh if you weren't aware already calling
camera.main is quite expensive so what unity is
basically doing in the background is it's iterating over all the objects in
the scene and it's trying to find the object with the tag
main camera and then it's returning that so you should always store a reference
to your camera so that you're only calling camera main
once at this at scene load or at script load or whatever it is i was
finding that i was having to do this in so many different scripts
so i figured i'd just chuck it in in helpers
and now it's always just available this next one
just allows me to cache my weight for seconds for my co-routines
so every time you create a new wait for second you're
allocating more garbage which the garbage collector then has to
come and clean up so let me just show you
in this top example i'm just looping 100 times and then each time i'm
creating a new weight per second so that is 100 allocations that the
garbage cleaner has to clean up in this one i'm just replacing it with
my helper method which will take in
the time and seconds that you'd like to wait it will look through our weight
dictionary and try to find a value with that
with that same key and if it finds it it will return it and then you can use it
otherwise it will create it and add it to the dictionary and then return that
so this is a great way to reduce your garbage collection in your co-returns
so this next one i use in every single project
and i cannot believe it is not a default for unity
so what this does is it will just detect if your mouse cursor or your your finger
press on a phone is over any kind of ui element
so for example if you've got a game on the phone where every single time you
press on the phone it will jump the character will jump you don't
want the character to jump if you're pressing the pause button or if you're
pressing like an item on on the bottom bar or something
like that so this would just return a boolean um to
let you know so the way it works is first we create our
pointer data which is just taking the the mouse position
and then we're sending it we're sending a raycast
and we're returning the results and basically if it returns anything then we
know that it's over over a ui element so in this scene i've
just got the some ui text and i've also got this
which is also an image on the canvas my little sad ice cream so
you'll see here over ui and even when we go over the text
it's over the ui all over here is not so i'll show you how i implemented
that it's just a simple text equals is are
you over the ui yep no so yeah it's a
very handy method to have and i use it in every single project
so this next one is awesome and it answers a question that i see posted all
the time on the unity forum and that is how to spawn a particle effect on the
canvas or to put a 3d object on the canvas
so basically what this does is you give it a rec transform any any canvas
element and it will put in the direct
it will put in the wrecked position it will input the camera so right now i'm
using the the one from the top of this script and
then it will out the result which will be a vector3
in world coordinates so i've got a script here
called cube which i put on a cube as you'll see in a minute and basically i'm
just setting its transform position to the output of
this function and i'll show you how i'll show you it
in action so here's my cube here's my
canvas element and as you can see on the cube i've got the canvas element
selected now i can just move the canvas element around and it will
follow it in world coordinates so as an example you could have some text
up here in this on the top right of the screen and every
time it changes you could spawn some fireworks or something and this
function would tell you exactly where to spawn it in world space so it lines up
exactly behind the points or in front of the
points or however you want to do it and finally this last one is one that i
use all the time but it's super simple and it just saves me from
doing it constantly throughout throughout lots of other scripts so
basically you just give it a transform and it will
delete um all the all the child objects so for example i use this constantly in
uh user interfaces so like if if they click
like this sort of character i can delete all the previous characters and then
show them these characters or items or what have you or even like
units at the end of a level or something if you want to destroy all the units and
the and the world objects in the game you can just give it the the parent
transform and this will just destroy them without
you having to just uh forage in every single time
all right how was that did i teach you anything uh if you haven't already
subscribed do so now or i'm going to leave this cute little ice cream
spinning for the rest of eternity i'll just leave
my computer on forever and ever and ever and he'll just
be in eternal hell if you have any helper functions that you think
would be handy put them in the comments i'd love to see them and i'll see you
next time