Tips & Tricks
I constantly run across a variety of tips and tricks, whether from reddit, people on Patreon I follow, reading books, or just various stuff I discover myself. I started this […]
What's a game dev that doesn't make the gaming world a better place?
I constantly run across a variety of tips and tricks, whether from reddit, people on Patreon I follow, reading books, or just various stuff I discover myself. I started this […]
I constantly run across a variety of tips and tricks, whether from reddit, people on Patreon I follow, reading books, or just various stuff I discover myself. I started this page to track those tips and tricks and locate them all in a single place for me, hopefully you will get some use from them as well.
When a group of tips and tricks grows big enough I will relocate them to a separate page to make it easier to focus on them.
back to table of contents
_var.Text = $"{num.ToString(_format)} text goes here.";
_var.Text = $"{num:N0} text goes here.";
back to table of contents
Beginning with C# v7 you can use pattern matching in switch cases. If you use Unity 2018.3 or later (or an earlier version that allows .NET v4.0) you can do something like the code below.
void OnCollisionEnter(Collision other)
{
// Detect and handle collisions
switch (other.gameObject.name)
{
case string a when a.Contains("Wall"):
bouncePos = transform.position;
bounce = true;
break;
case string a when a.Contains("Door"):
bouncePos = transform.position;
bounce = true;
break;
case string a when a.Contains("Stairs"):
distanceToMove = 0.0f; // Disable moving before changing levels
StartCoroutine(ChangeLevel(other.gameObject.GetComponent<Stairs>().NewRelativePosition));
distanceToMove = 4.0f; // Enable moving after changing levels
break;
case null:
new System.ArgumentNullException(nameof(other));
break;
}
}
back to table of contents
back to table of contents
default
operator in C#; example [SerializeField] private Transform t = default;
back to table of contents
I had a case where I wanted to use the System.Numerics namespace, however, it is not part of the normal Unity NET. I added a ‘mcs.rsp’ file (which is a text file) to the Assets folder and then added the follow line to it. (Current as of late 2018, the folder may change over time.)
-r:"C:/Program Files/Unity/Editor/Data/MonoBleedingEdge/lib/mono/4.5/System.Numerics.dll"
Then I went back to Unity and clicked the Assets menu, then Refresh to get Unity to recompile the code and include the reference.
⚠️NOTE: This caused a conflict with various Vector classes. To work around this I created a using alias directive, i.e., using Num = System.Numerics.
⚠️NOTE: The file name depends on the .NET version targeted, ‘mcs’ for .NET v3.5 (deprecated) and ‘csc’ for .NET v4. See the bottom of the Platform Dependent Compilation page. To see the ‘mcs’ help navigate to a folder similar to “C:\Program Files\Unity\Hub\Editor\2018.3.0f2\Editor\Data\MonoBleedingEdge\lib\mono\4.5” (adjusted for version), open a command window, and type ‘mcs -help’. I assume csc and mcs have the same options.
back to table of contents
Create a folder like ‘_Game’, in the ‘Assets’ folder, so it sits at the top of the folder structure. All the downloaded assets can set in the main ‘Assets’ folder and I can always find my game assets. Don’t move assets that are generated, e.g., materials generated from a model.
Below I listed out the current folder structure I use. I only create a folder when I need it so as to not clutter my folders. The folder structure is adapted from a variety of sources.
Current as of 24 April 2019.
back to table of contents
Reset all empty GameObjects to the following values: transform (0, 0, 0); rotation (0, 0, 0); scale (1, 1, 1). For empty GameObjects that hold scripts use a @ prefix, e.g., @Cheats. When you instantiate an object during runtime put it in ‘_Dynamic’.
Create empty GameObjects for each of these sections. My current practice is to put as much as I can as a child object so that I collapse entire sections and focus on just what I need to see. I got this structure from The Knights of Unity.
Current as of 24 April 2019.
back to table of contents
back to table of contents