Skip to content

Instantly share code, notes, and snippets.

@EdwinChua
Last active September 8, 2017 01:50
Show Gist options
  • Save EdwinChua/d1546acc2a0a45bbff7804dbe22c45d6 to your computer and use it in GitHub Desktop.
Save EdwinChua/d1546acc2a0a45bbff7804dbe22c45d6 to your computer and use it in GitHub Desktop.
Intro to Unity

Unity3d Basics

Aims to provide the reader with a brief guide to coding with Unity.

Unity Editor

Common Terms

  • Editor – The Unity IDE
  • Scene – A collection of objects and scripts that are loaded and active together (like an Activity in Android)
  • GameObject – Any object/item seen in the hierarchy tab
  • Prefab – A GameObject (could have child GameObjects attached) with customized components/scripts attached. Common uses:
    • Instantiate a prefab via script (Instantiate(prefab, parentTransform))
    • Click and drag more copies of the prefab onto the scene, with different parameters
  • Component – Anything that is attached to a GameObject. Components give GameObjects their position, behavior, and other attributes. A script attached to a GameObject is also a component.

Tabs

There are 6 main tabs in the editor most commonly used. They are:

  • Hierarchy
    • Displays currently active scenes in the scene tab. (Recommendation: Unload/Remove scenes not currently in use)
    • Shows GameObjects that are in the scene, and their relative hierarchy (parent-child relationship)
  • Project
    • Shows all files in the Assets folder of the project
    • Unity creates multiple folders for each project, but the programmer only needs to access the Assets folder
  • Inspector
    • Shows the components/scripts that have been added to the currently selected GameObject
    • Each component/script has variables that can be adjusted. The adjustments can be performed here.
    • Note: If a script specifies a default value, and the value is changed in the inspector, the changes will take priority. These values are stored in the corresponding .meta file.
  • Console
    • Shows warnings, errors, and other Debug.Log() or print() messages
  • Scene
    • Shows GameObjects currently in the scene. They can be dragged and dropped, or their positions can be modified in the Inspector
  • Game
    • Shows the game view, via the currently active camera

Scripting in C#

Scripts are attached to GameObjects to give them certain attributes and behaviours.

They can also manipulate other GameObjects, by providing the script with a reference. This is most easily achieved by declaring a public variable, and assigning it in the Inspector via click-&-drag from the Hierarchy tab. In the example below, an InputField is assigned to the userName and pwd fields.

Variables declared without an access modifier are implicitly private, while those declared internal are available throughout the scripts with the same namespace, but are not exposed for modification in the inspector. If no namespace is referenced, the script will be available throughout the project.

Variables declared static are shared across all instances of the script. Only public, non-static variables will be available in the inspector for modification.

pic jere

Commonly Used in Scripting

  • gameObject: Reserved variable name, refers to the GameObject the script is attached to. Same with transform, etc...
  • gameObject.GetComponent<XXXX>(): Gets a component (XXXX) attached to the GameObject. GetComponent<XXXX> can be used with transform, camera. Can be chained, eg. gameObject.GetComponent<XXXX>().XXXX_Method()

Import & Export - Unity Packages (.unitypackages)

Often, a project will have components that can be re-used in other projects, or an entire project is meant to be included in the final application.

In this case, a Unity package (.unitypackage) is a way to easily export and import a project.

Importing

To import a package, select Assets > Import Package > Custom Package.

If you updated a package imported previously, when you perform this step, Unity will automatically detect the files that need to be updated. In either case, you will be allowed to select the files to import.

Exporting

To export a package, select Assets > Export Package. You will be allowed to select the files to export.

Common Problems

Application freezes when you press play (Editor) Occasionally, an infinite loop in the Start() method will cause the application to freeze. More commonly, an exception was thrown at startup. This may cause the application to freeze for some time (up to 5mins). If it does not appear to be “Not Responding” in the task manager, waiting for the application to resume will allow you to identify the exception being thrown via the Console.

Application crashes on start (mobile device)

The application works fine in the Editor, but crashes on start when deployed. This problem is tricky. You will need to connect your device to your PC, and check the Android log output, either via Android Studio or the Android Device Monitor. https://developer.android.com/studio/profile/monitor.html

Application freezes after loading successfully (mobile device)

The application freezes on/immediately after showing the first screen. This is most likely due to an unsuccessful network connection attempt on the main thread, which causes the application to freeze until connection is successful, or the connection attempt times out. Implementing network connections on a separate thread will resolve this issue.

Recommended Intro Tutorial

https://unity3d.com/learn/tutorials/projects/roll-ball-tutorial

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment