46 Tips & Tricks for 2D mobile Performance in Unity.
If this is your first jump into the world of Unity, my first tip (this one’s a freebie) is to stop trying to use it like other languages and environments. You will be using GameObjects, you will be adding multiple script components, and you will have to think differently. When I first started, my approach was to largely ignore prefabs (or use them like Flash’s display list) and get a copy of Box2D running. Painful as it is to deviate, get ready to put in some work! Without further ado, let’s get started: Physics: -Use the built-in physics.It might seem like a waste of cycles to have a fully 3D physics engine running the show for a 2D game, but bear in mind that the Nvidia PhysX engine will be running in Unity’s native core. -Try to use a 1/1 scale. -Get your object Mass right. -Mesh colliders can be slow compared to primitive box/sphere colliders. -You can simulate more complex shapes by combining primitive colliders. -Objects with a collider but no RigidBody are considered static.
Mike’s Unity Scripting Tutorial
Unity keyboard control for move & turn Now let’s learn scripting by looking at basic scripts for controlling a cube we’ve made. Here’s a sample that uses the user’s choice of fwd/bkwd keys for movement and their left/right keys for turning. Beware: this copies and pastes well from SAFARI but has trouble with line endings when copied from Firefox (Firefox versions 3 through 16 ignore the unix “LF” linefeeds that I use in this unicode web page).Beware: integer math kills rotations.Beware: editing a script might not change copies of the script that are already attached to game objects, so I’ve been pressing "reset" on the attached script in the inspector. Here is a move_force script that allows your player to accelerate and twist. With this script, friction is more of an issue, so I suggest adding a jump script (below) so that you can jump a little and turn in mid-air! This move/turn script works like pacman: looks and goes in the direction chosen, rather than having to turn before you move.
Vector3.Lerp
Vector3.Lerp static function Lerp(from: Vector3, to: Vector3, t: float): Vector3; Description Linearly interpolates between two vectors. Interpolates between from and to by the fraction t.
Editing Unity’s Editor - Size Five Games
Following last week’s post about how we’re handling trendy-looking 2D in Unity3D, I had a few requests for a bit more info on what we’re doing Editor-side. Here’s Tim again, explaining what on Earth he’s been up to: We use Unity Editor extensions for lots of things, The Transform2D Inspector is an example of one we use to customise the editor to work better in a 2D game. It overrides Unity’s default Transform Inspector for object that have a Toolkit2D Sprite component attached. It simply cuts away all the information we don’t need for a 2D game; for example, we never have to rotate something on the x or y axis… it just doesn’t make sense in 2D, so we hide that data away and only show the z rotation, which the inspector surfaces as ‘Rotation’. The Enable Snapping Checkbox turns on our snapping tool. We also use Editor extentions for data entry. Unity defaults to showing the whole list in a tree view, which is confusing to say the least.
Unity | Home
GUI.TextField
static function TextField (position : Rect , text : String , maxLength : int , style : GUIStyle ) : String Parameters Returns String - The edited string. Description Make a single-line text field where the user can edit a string. JavaScript JavaScript C# Boo var stringToEdit : String = "Hello World" ; function OnGUI () { stringToEdit = GUI.TextField ( Rect (10, 10, 200, 20), stringToEdit, 25); } using UnityEngine; using System.Collections; public class example : MonoBehaviour { public string stringToEdit = "Hello World" ; void OnGUI () { stringToEdit = GUI.TextField ( new Rect (10, 10, 200, 20), stringToEdit, 25); } } import UnityEngine import System.Collections class example( MonoBehaviour ): public stringToEdit as string = 'Hello World'
Changing Text Size Relative To Screen?
Here's one I wrote that changes mostly in realtime in the editor so you can place before running. Just toggle Maximize on Play to get the actual change if you aren't seeing it. 1. Create a C# script called ScaleFont 2. Paste in this code 3.
Font
Switch to Scripting Fonts can be created or imported for use in either the GUI Text or the Text Mesh Components. Importing Font files To add a font to your project you need to place the font file in your Assets folder. Unity will then automatically import it. Supported Font formats are TrueType Fonts (.ttf files) and OpenType Fonts (.otf files). To change the Size of the font, highlight it in the Project View and you have a number of options in the Import Settings in the Inspector. Import Settings specific to dynamic fonts After you import the font, you can expand the font in Project View to see that it has auto-generated some assets. Dynamic fonts When you set the Characters drop-down in the Import Settings to Dynamic, Unity will not pre-generate a texture with all font characters. Note that some target platforms (WebGL, some consoles) do not have OS default fonts Unity can access for rendering text. Custom fonts To create a custom font select ‘Create->custom font’ from the project window.
Related:
Related: