Тёмный

Unity/C# Tip - #03: String To Hash 

Lasanha Dev
Подписаться 107
Просмотров 961
50% 1

In this video, I'm going to share with you guys a tip for optimizing your code in Unity.
We're going to convert strings To hashes using Unity Animator. StringToHash() method. This allows us to convert our animator state name, which is a string, to an int, which is our hash code.
By converting strings to hash, we can optimize our code when changing animation states because computers are faster at comparing numbers than strings, and each state has its own hash ID based on the state name.
If you have any questions or suggestions, just drop a comment down below.

Опубликовано:

 

16 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 2   
@ograpes3458
@ograpes3458 5 месяцев назад
Thx for the tips, what's the difference between readonly and const ? I know const but never used readonly
@LasanhaDev
@LasanhaDev 5 месяцев назад
Const: You can use const to declare a value at compile time that will never change during runtime. I usually use const to declare strings and remove magic numbers. In this video of the Ghosts 'n Goblins series, I use const to declare some fields inside the LadderController: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-y81ySawtecE.html And also in this video of the event bus 2024 solution, I use const to declare the event names. Since the names won't change at runtime, I can create them at compilation time: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-FhuKp-d9MUQ.html Readonly: You can use this to initialize the variable in the object constructor or field declaration. Since the value will not change, the only thing that you will be able to do with that field is read the value. Because MonoBehaviour doesn't have constructors, you can't declare a readonly field and try to assign it during Awake or Start. So I mostly use to declare Data Structures, and also with EventBus where I need to pass some parameters I usually declare the record fields as readonly and assign them in the constructor. In this example, the animator variable is assigned in the declaration, so I can only read the generated hash value. On the Object Pooling video I use a const field to use as Default pool size and readonly for the _poolsDictionary: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-uOiXN3kuM2o.html I reformulated a little bit the object pool class, after using for sometime I found some problems and areas to improve, so I'll make another video like the event bus but for the object pool 2024 solution. There are some other cases, which the docs explains well: Readonly: learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/readonly Const: learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/const