Unity3d Coding Tips and Tricks
Let's talk about @unity3d, and the execution order of its event functions—such as 𝚂𝚝𝚊𝚛𝚝 and 𝚄𝚙𝚍𝚊𝚝𝚎.
— Alan Zucconi (@AlanZucconi) March 8, 2021
Trust me when I say that ...IT'S WAY MORE COMPLICATED THAN YOU THINK. 😬#gamedev #unitytips #madewithunity
🧵👇 pic.twitter.com/jTcX0lp15R
With the latest @unity 2020 LTS you can use some of the new features in C# 8.0! One of my favorites is a switch expression which greatly reduces the syntax required. It's not a full replacement for switch statements but works well for linking data together. #UnityTips pic.twitter.com/tAB3mxfxvN
— Dan Miller (@DanMillerDev) May 11, 2021
If you want to destroy an object in @unity3d, but want to wait a few seconds, you can insert a delay as a second optional parameter.#TipTuesday #TopTips #unitytips pic.twitter.com/mTY646t72F
— Gordon Arber (@GordonArber) June 15, 2021
Found a nice property attribute that labels array elements by enum names. This is going to be very useful, no more need to make get methods or dicts as I typically would when I want to map data to enum. Added it to my utils repo: https://t.co/7noj6Ly9l0 #unitytips #madewithunity pic.twitter.com/uN7GRnO7uy
— 😷 Ivan 🌲🏎️💨🌲 (@Nothke) June 9, 2021
Did you know Debug.LogException() in try..catch will navigate right to the proper line which caused the exception (while other Debug.Log* methods will navigate to themselves)?#unitytips #unity3d pic.twitter.com/01Mys6nDXf
— Dmitriy Yukhanov (@dmitriy_focus) June 15, 2021
#UnityTips Of course it is very good to know how to get from degree to radians ( rad = deg * PI / 180 )
— Binary Impact (@BinaryImpactG) October 27, 2020
But you could also use one of the constants from the Mathf library ( Mathf.Deg2Rad )#gamedev #indiedev pic.twitter.com/27zdWocZVH
#UnityTips You can use extension methods to add functionality specific to a particular enum type.
— Binary Impact (@BinaryImpactG) October 13, 2020
In our example we use an enum to define neighbors on a square tiled board. And we add quality of life methods to the enumeration.#csharp #indiedev #gamedev pic.twitter.com/yUq6s6Vms7
#UnityTips If you use the pattern [SerializeField] private int _myVar; and stumble upon the warning 0649 here are three ways to deal with it 👇#gamedev #indiedev pic.twitter.com/NIOGKfTLIw
— Binary Impact (@BinaryImpactG) November 10, 2020
#UnityTips Today two functions to return random points on a circle & a sphere, given a radius.
— Binary Impact (@BinaryImpactG) October 20, 2020
We wanted to highlight two very useful methods in Unitys Random class.#coding #indiedev #gamedev pic.twitter.com/Lk60ctBDDh
I recently discovered the nameof() expression in c#!
— adamgryu (@adamgryu) November 14, 2020
It's handy for creating custom inspectors, since they often require referencing a field by its string name.
With nameof(), if you refactor that name, you don't need to also update a hardcoded string!#gamedev #unitytips pic.twitter.com/44a9zUNrhP
There are many tools to work with asynchronous code, such as:
— Radek Paszkowski (@RadekPaszkowski) April 14, 2020
- Coroutines
- Callbacks
- Async/Await
- Reactive programming
They have their pros and cons, but all of them are useful!#Unity #unity3D #unitytips #programming #gamedev #indiedev #rx #unirx pic.twitter.com/PZNy6h7Pfn
In @unity3d, You can use [AddComponentMenu()] to place a script in a specific place in the "Component" menu.#unitytips https://t.co/KAI9M4wHCm pic.twitter.com/7Tw66AQ1yH— JfranMora (@JfranMora) July 2, 2019
A little code shorthand for #unitytips!— Hannah Rose (@hannaesthetic) July 2, 2019
If you wanna check if a thing you've saved exists, and if so run a method on it, you can get rid of the whole "if is not null run the code", and replace it with a nice question mark.
See attached image 👇 pic.twitter.com/AtHjiXZL6E
#UnityTip A simple helpbox can work wonders on understanding your tools. It is a good practice to add them to your custom inspectors.#gamedev #indiedev #custominspector #csharp pic.twitter.com/ksIeth7qur— Binary Impact (@BinaryImpactG) December 10, 2019
#unitytip Make your scripts easy to use even for non-programmer. Show what is missing with a red color and only unlock the rest of the script once the critical obj is dragged in. Then you can change the field to green to indicate the user made something right.#gamedev #indiedev pic.twitter.com/WpMpYrW7iU— Binary Impact (@BinaryImpactG) December 17, 2019
UnityTip: Rename variables without losing their references in the inspector using FormerlySerializedAsAttribute.— Mahesh Athirala (@MaheshAthirala) July 16, 2019
Doc:https://t.co/avgtj255T8#indiedev #gamedev #UnityTips pic.twitter.com/18bSvzSabN
📏Lerp vs Slerp⭕— Freya Holmér (@FreyaHolmer) September 23, 2019
• Lerp interpolates in a straight line
• useful for interpolating positions
• Slerp rotates around (0,0)
• useful for interpolating directions#unitytips pic.twitter.com/KSQQWCb5f4
If you're using normal C# event handlers in your #unity3D project, don't forget to unsubscribe them (often, but not always, in either the OnDisable or OnDestroy methods).— keyboardP (@keyboardP) January 4, 2020
If the object is destroyed and you haven't unsubscribed, the game could be unstable#unitytips #gamedev
If you need precision when writing things like positions and rotations in @unity3d to a file (or in general) make sure to use the optional parameters in ToString() here I'm using ToString("F4") to have 4 points of precision on my position floats #UnityTips pic.twitter.com/781SDaeabq— Dan Miller (@DanMillerDev) November 19, 2019
Friendly reminder! If you use raycast functions, check the API first! There are several functions that do almost the same functionality but have a very different impact on performance / GC, make sure you use the most optimized for your needs! #unitytipshttps://t.co/01IRAWqKYN pic.twitter.com/D00CKdxkHp— JfranMora (@JfranMora) December 16, 2019
You can use the OnBecameVisible / Invisible functions to detect when objects show up or disappear from screen!— André Cardoso (@andre_mc) December 17, 2019
I use this to quite a lot to detect when new targets show up on screen! #unitytips #unity3d pic.twitter.com/hZh1lfpfmK
Since Unity 2019.2 you can use TryGetComponent to check if an object has a component, it will not allocate GC in the editor if the object doesn't have one.— JfranMora (@JfranMora) November 26, 2019
Handy for Collisions, Triggers, etc.https://t.co/n5CkOj8IrS#UnityTips pic.twitter.com/E4EwM7rPNZ
Camera.main is an expensive lookup, try avoid using it in a loop.— keyboardP (@keyboardP) January 7, 2020
Instead, cache the variable in the Start() method and reuse (or add camera to component directly in the editor if possible which is then accessible via 'camera' variable)#gamedev #unity3d #unitytips #indiedev pic.twitter.com/DQLgXrgEpS
[Unity, C#] How to access to internals/privates in other assemblies WITHOUT reflection: https://t.co/O6CN0S3B1e #unity3d #unitytips #CSharp pic.twitter.com/wvtc78Efj2— mob-sakai (@mob_sakai) January 6, 2020
Unity3D doesn't have a shortcut to save the scene and the project at the same time so I wrote a quick script that allows CTRL + Alt + S to save both, scene and project.— keyboardP (@keyboardP) January 3, 2020
The script creates a new menu item under "File" called "Save Scene and Project"#unity3d #unitytips #gamedev pic.twitter.com/dimkvltX0r
If you need translations/localisaton of common game-related, here is a HUGE crowdsourced list of words as a starting point by @PolyglotGamedev (check Master tab on the sheet) https://t.co/1bIARegFno#gamedev #unity3d #unitytips #gamedesign— keyboardP (@keyboardP) January 2, 2020
Use audiosource.PlayOneShot() instead https://t.co/82fJtNy34D() if you want to play different sounds with the same audiosource without cutting each other off.— JfranMora (@JfranMora) November 5, 2019
You can specify volume intensity.
It's very useful for sound effects!#unitytipshttps://t.co/LHealzFBg5
a thread on Inverse Lerp - a super useful, yet often overlooked function!✨— Freya Holmér (@FreyaHolmer) October 15, 2019
Lerp( a, b, t ) = value
InvLerp( a, b, value ) = t
• lerp returns a blend between a and b, based on a fraction t
• inverse lerp returns a fraction t, based on a value between a and b#unitytips pic.twitter.com/P0s4MHCqSO
Visual Studio 16.3 and Visual Studio for Mac 8.3 give you diagnostics and code-fixes specifically for Unity projects! Let me know what best-practices you'd like to see the editor suggest. #unitytips #gamedev pic.twitter.com/z6tTWmI090— John Miller (@jmillerdev) September 30, 2019
— Mohammad Faizan Khan (@voiceoffaizan) October 8, 2019
While working on your Unity game in Visual Studio try:— Kornel (@Kornel___) September 30, 2019
Shift+Ctrl+V to bring out a Clipboard of thing you copied inside VS
Quite handy 😀#unitytips pic.twitter.com/acGoYnu6s3
I recently learned about the "=>" syntax for getters in C# and I am never looking. back. #unitytips pic.twitter.com/oJ641Pht0W— Altered Item Vollmer (@AsherVo) October 3, 2019
You can use Debug.Break() to pause the Editor via script.— Febucci (@febucci) October 1, 2019
> More tips here: https://t.co/XBVspMFUiX #gamedev #indiedev #unitytips #unity3d pic.twitter.com/YgVAD3AKQV
Its Tuesday, #UnityTips day, and time to improve unity workflow and share some shortcuts!— Mohammad Faizan Khan (@voiceoffaizan) June 25, 2019
Here are the amazing 35 tips by @febucci.https://t.co/mqK8yT0qTA#GameDev #unity3d
the null-coalescing operator (??) is a super neat way to quickly get a value or return a fallback in case it's null✨— Freya Holmér 🔜 IndieCade Europe (@FreyaHolmer) October 5, 2019
(note however that it's unsafe to use it with objects deriving from UnityEngine.Object ⚠ info in thread)#unitytips pic.twitter.com/ikTcL6X8vd
There is an easy way to debug frame-by-frame situations. Debug.Break() pauses the application and with Ctrl+Alt+P / Alt+Cmd+P you can step through. Very handy to take a closer look at collisions and other physics stuff#unitytips #gamedev #unity #unity3d pic.twitter.com/4xD0Dkquyo— Spires of Mana (@SpiresOfMana) December 12, 2017
It was only now that I realized drag and drop reordering of components is finally here. #unitytips pic.twitter.com/IHdnpXbk8v— Stephan Hövelbrinks (@talecrafter) September 22, 2017
Reduce C# compile time by moving code into DLLs or Plugin scope (compiles separately)— Unity Tips & Tricks (@tips4unity) May 7, 2017
Use attribute "DisallowMultipleComponent" to prevents to have 2 MonoBehaviours of same type on a GameObject#UnityTips #GameDev #IndieDev pic.twitter.com/SnsDWoe3Rp— Nicoplv (@nicoplv) September 26, 2017
If you want a private variable to show up in the Inspector use the [SerializeField] attribute. #gamedev #igdev #unity3d #unitytips #indiedev— Abhinav Demkeys (@abhinav_Demkeys) September 14, 2017
Hide a public variable in inspector "[HideInInspector] public bool myBool;"— Unity Tips & Tricks (@tips4unity) September 10, 2017
Debug.Log("click me", myGameObject); | Clicking on the console log highlights myGameObject— Unity Tips & Tricks (@tips4unity) September 14, 2017
Set the menu order of a MenuItem by using the priority parameter (99) [MenuItem("Scenes/myScene", false, 99)]— Unity Tips & Tricks (@tips4unity) September 23, 2017
#unitytips If you need to know when @unity3d added a new method (for #defines), go to the script docs & click "Other Versions" to find out pic.twitter.com/mrhBPFwWWJ— Mark Hogan (@markeahogan) August 8, 2017
If I forgot to add your favourite tips and trick then, please comment.#unitytips You can add custom items to context menu, even for built-in classes, by using [MenuItem("CONTEXT/...")]#madewithunity #gamedev pic.twitter.com/r6wxcQwt4b— Yannick Gero (@YannickGero) September 19, 2017
See Also:
Social Plugin