Introduction to Unity Classes

42

description

Introduction to Unity Classes. Unity game engine classes. Two major categories: Runtime see http://unity3d.com/support/documentation/ScriptReference/20_class_hierarchy.html Editor see http://unity3d.com/support/documentation/ScriptReference/20_class_hierarchy.Editor_Classes.html. - PowerPoint PPT Presentation

Transcript of Introduction to Unity Classes

Page 1: Introduction to Unity Classes
Page 2: Introduction to Unity Classes

Two major categories:1. Runtime see

http://unity3d.com/support/documentation/ScriptReference/20_class_hierarchy.html

2. Editor see

http://unity3d.com/support/documentation/ScriptReference/20_class_hierarchy.Editor_Classes.html

Page 3: Introduction to Unity Classes

1. AccelerationEvent2. AnimationCurve3. AnimationEvent4. AnimationState5. Application6. Array7. AudioSettings8. BitStream9. BoneWeight10. Bounds11. ClothSkinningCoefficient12. Collision13. Color14. CombineInstance15. ContactPoint16. ControllerColliderHit17. Debug18. DetailPrototype19. Event20. GL21. GUIContent22. GUILayoutOption23. GUILayoutUtility24. GUILayout25. GUISettings26. GUIStyleState27. GUIStyle28. GUIUtility29. GUI30. GeometryUtility31. Gizmos32. Graphics33. Hashtable34. HostData35. Input36. JointDrive37. JointLimits38. JointMotor39. JointSpring40. Keyframe41. LayerMask42. LightmapData43. LightmapSettings44. LocationInfo45. MasterServer46. MaterialPropertyBlock47. Mathf

1. Matrix4x42. NetworkMessageInfo3. NetworkPlayer4. NetworkViewID5. Network6. Object (has many, many subclasses)7. Particle8. Path9. Physics10. Ping11. Plane12. PlayerPrefsException13. PlayerPrefs14. Profiler15. QualitySettings16. Quaternion17. Random18. Ray19. RaycastHit20. RectOffset21. Rect22. RenderSettings23. Resolution24. Resources25. Screen26. Security27. SoftJointLimit28. SplatPrototype29. StaticBatchingUtility30. String31. SystemInfo32. Time33. Touch34. TreeInstance35. TreePrototype36. Vector237. Vector338. Vector439. WWWForm40. WWW41. WheelFrictionCurve42. WheelHit43. YieldInstruction (some additional subclasses)44. iPhoneInput45. iPhoneKeyboard46. iPhoneSettings47. iPhoneUtils

Page 4: Introduction to Unity Classes

1. AnimationClipCurveData2. AnimationUtility3. AssetDatabase4. AssetImporter5. AssetPostprocessor6. BuildPipeline7. CustomEditor8. DragAndDrop9. DrawGizmo10. EditorApplication11. EditorGUILayout12. EditorGUIUtility13. EditorGUI14. EditorPrefs15. EditorStyles16. EditorUtility17. EditorWindow18. Editor19. FileUtil20. HandleUtility

1. Handles2. Help3. LightmapEditorSettings4. Lightmapping5. MenuCommand6. MenuItem7. MeshUtility8. ModelImporterClipAnimation9. MonoScript10. ObjectNames11. PlayerSettings12. Selection13. SerializedObject14. SerializedProperty15. StaticOcclusionCullingVisualization16. StaticOcclusionCulling17. TextureImporterSettings18. Undo19. UnwrapParam20. Unwrapping

Page 5: Introduction to Unity Classes

class A inherits from class B• Object is the base class for all objects Unity can

reference.1.variables (data members)2.constructors (ctors)3.functions (methods)4.class variables (static)5.class functions (static)6.inherited members

inherited variables inherited functions inherited class variables inherited class functions

Page 6: Introduction to Unity Classes

also:1. Enumerations groups of constants

2. Attributes special (i.e., non C# language) command in scripts to add

items to development, tells compiler/interpreter how code should be processed, etc.

notation:• static function Quit ( ) : void static void Quit ( )

• static function LoadLevel ( index : int ) : void static void LoadLevel ( int index )

Page 7: Introduction to Unity Classes
Page 8: Introduction to Unity Classes

access to application run-time data

This class contains static methods for looking up information about and controlling the run-time data.

Page 9: Introduction to Unity Classes

static function Quit ( ) : void• Quits the player application. Quit is ignored in

the editor or the web player. static function LoadLevel ( index : int ) :

void• Loads the level.

This function loads level by its index. You can see the indices of all levels using the File->Build Settings... menu in Unity. Before you can load a level you have to add it to the list of levels used in the game. Use File->Build Settings... in Unity and add the levels you need to the level list there.

Page 10: Introduction to Unity Classes

A representation of audio sources in 3D. An AudioSource is attached to a GameObject for playing

back sounds in a 3D environment. Mono sounds play back 3D. In order to play 3D sounds you also need to have a AudioListener. The audio listener is normally attached to the camera you want to use. Stereo sounds are always played back without distance based attenuation.

You can play a single audio clip using Play, Pause and Stop. You can also adjust its volume while playing using volume property, or seek using time. Multiple sounds can be played on one AudioSource using PlayOneShot. You can play a clip at a static position in 3D space using PlayClipAtPoint.

Page 11: Introduction to Unity Classes

A Camera is a device through which the player views the world.

A screen space point is defined in pixels. The bottom-left of the screen is (0,0); the right-top is (pixelWidth,pixelHeight). The z position is in world units from the camera.

A viewport space point is normalized and relative to the camera. The bottom-left of the camera is (0,0); the top-right is (1,1). The z position is in world units from the camera.

A world space point is defined in global coordinates (eg. Transform.position).

Page 12: Introduction to Unity Classes

Representation of RGBA colors.

This structure is used throughout Unity to pass colors around. Each color component is a floating point value with a range from 0 to 1.

Components (r,g,b) define a color in RGB color space. Alpha component (a) defines transparency - alpha of one is completely opaque, alpha of zero is completely transparent.

Page 13: Introduction to Unity Classes

Class variables• red - Solid red. RGBA is (1, 0, 0, 1).• green - Solid green. RGBA is (0, 1, 0, 1).• blue - Solid blue. RGBA is (0, 0, 1, 1).• white - Solid white. RGBA is (1, 1, 1, 1).• black - Solid black. RGBA is (0, 0, 0, 1).• yellow - Yellow. RGBA is weird (1, 235/255, 4/255, 1), but the

color is nice to look at!• cyan - Cyan. RGBA is (0, 1, 1, 1).• magenta - Magenta. RGBA is (1, 0, 1, 1).• gray - Gray. RGBA is (0.5, 0.5, 0.5, 1).• grey - English spelling for gray. RGBA is the same (0.5, 0.5, 0.5,

1).• clear - Completely transparent. RGBA is (0, 0, 0, 0).

Page 14: Introduction to Unity Classes

static function Color ( r : float, g : float, b : float, a : float ) : Color• Constructs a new Color with given r,g,b,a

components.

Page 15: Introduction to Unity Classes

Base class for all entities in Unity scenes. Variables

• transform - The Transform attached to this GameObject. (null if none)• rigidbody - The Rigidbody attached to this GameObject. (null if none)• camera - The Camera attached to this GameObject. (null if none)• light - The Light attached to this GameObject. (null if none)• renderer - The Renderer attached to this GameObject. (null if none)• audio - The AudioSource attached to this GameObject. (null if none)• collider - The Collider attached to this GameObject. (null if none)• particleEmitter - The ParticleEmitter attached to this GameObject. (null

none)• layer - The layer the game object is in. A layer is in the range [0...32].• tag - The tag of this game object.

Page 16: Introduction to Unity Classes

Class functions• FindWithTag - Returns one active GameObject

tagged tag. Returns null if no GameObject was found.

• FindGameObjectsWithTag - Returns a list of active GameObjects tagged tag. Returns null if no GameObject was found.

• Find - Finds a game object by name and returns it.

Page 17: Introduction to Unity Classes

Constructor(s)

• static function GameObject ( name : string ) : GameObject Creates a new game object, named name.

Page 18: Introduction to Unity Classes

Functions• GetComponent - Returns the component of Type type if

the game object has one attached, null if it doesn't.• GetComponentInChildren - Returns the component of

Type type in the GameObject or any of its children using depth first search.

• GetComponents - Returns all components of Type type in the GameObject.

• GetComponentsInChildren - Returns all components of Type type in the GameObject or any of its children.

• AddComponent - Adds a component class named className to the game object.

Page 19: Introduction to Unity Classes

Gizmos are used to give visual debugging or setup aids in the scene view.

All gizmo drawing has to be done in either OnDrawGizmos or OnDrawGizmosSelected functions of the script.

OnDrawGizmos is called every frame. All gizmos rendered within OnDrawGizmos are pickable.

OnDrawGizmosSelected is called only if the object the script is attached to is selected.

Page 20: Introduction to Unity Classes

Class variable(s)• color - Sets the color for the gizmos that will be drawn next.

Class functions• DrawLine - Draws a line starting at from towards to.• DrawWireSphere - Draws a wireframe sphere with center

and radius.• DrawSphere - Draws a solid sphere with center and radius.• DrawWireCube - Draw a wireframe box with center and size.• DrawCube - Draw a solid box with center and size.• DrawIcon - Draw an icon at world position in the scene view.• DrawGUITexture - Draw a texture in screen coordinates.

Useful for GUI backgrounds.

Page 21: Introduction to Unity Classes

Script interface for light components.

Use this to control all aspects of Unity's lights. The properties are an exact match for the values shown in the Inspector.

Usually lights are just created in the editor but sometimes you want to create a light from a script.

Page 22: Introduction to Unity Classes

Variables• type - The type of the light (can be LightType.Spot,

LightType.Directional, LightType.Point).• color - The color of the light.• intensity - The Intensity of a light is multiplied with the Light

color.• shadows - How this light casts shadows.• shadowStrength - Strength of light's shadows• range - The range of the light.• spotAngle - The light's spotlight angle in degrees.• cookie - The cookie texture projected by the light.• flare - The flare asset to use for this light.• renderMode - How to render the light.• cullingMask - This is used to lights parts of the scene selectively.

Example of an enum (enumeration).

Page 23: Introduction to Unity Classes

The line renderer is used to draw free-floating lines in 3D space.

This class is a script interface for a line renderer component.

Page 24: Introduction to Unity Classes

A collection of common math functions. Class variables

• PI - The infamous 3.14159265358979... value.• Infinity - A representation of positive infinity.• NegativeInfinity - A representation of negative

infinity.• Deg2Rad - Degrees-to-radians conversion

constant.• Rad2Deg - Radians-to-degrees conversion

constant.• Epsilon - A tiny floating point value.

Page 25: Introduction to Unity Classes

A collection of common math functions.

Class functions• familiar functions such as Sin, Cos, Tan,

Asin, Acos, Atan, Atan2, Sqrt, Abs, Min, Max, Pow, Exp, Log, Log10, Ceil, Floor, Round, etc.

Page 26: Introduction to Unity Classes

MonoBehaviour is the base class every script derives from.

Using Javascript every script automatically derives from MonoBehaviour.

When using C# (or Boo) you have to explicitly derive from MonoBehaviour.

MonoBehaviour inherits from Behaviour which inherits from Component which inherits from Object.

Page 27: Introduction to Unity Classes

Methods typically overridden:• Update - called every frame, if the

MonoBehaviour is enabled• FixedUpdate - This function is called every

fixed framerate frame, if the MonoBehaviour is enabled.

• Awake - called when the script instance is being loaded

• Start - called just before any of the Update methods are called the first time

• Reset - Reset to default values.

Page 28: Introduction to Unity Classes

Methods typically overridden in response to some event:• OnMouseEnter - called when the mouse entered the GUIElement

or Collider• OnMouseOver - called every frame while the mouse is over the

GUIElement or Collider• OnMouseExit - called when the mouse is not any longer over the

GUIElement or Collider• OnMouseDown - called when the user has pressed the mouse

button while over the GUIElement or Collider• OnMouseUp - called when the user has released the mouse button• OnMouseDrag - called when the user has clicked on a GUIElement

or Collider and is still holding down the mouse

Page 29: Introduction to Unity Classes

Methods typically overridden in response to some event:• OnTriggerEnter - called when the Collider other enters the trigger• OnTriggerExit - called when the Collider other has stopped touching the

trigger• OnTriggerStay - called once per frame for every Collider other that is

touching the trigger

• OnCollisionEnter - called when this collider/rigidbody has begun touching another rigidbody/collider

• OnCollisionExit - called when this collider/rigidbody has stopped touching another rigidbody/collider

• OnCollisionStay - called once per frame for every collider/rigidbody that is touching rigidbody/collider

• OnControllerColliderHit - called when the controller hits a collider while performing a Move

• OnParticleCollision - called when a particle hits a collider

Page 30: Introduction to Unity Classes

Methods typically overridden in response to some event:• OnBecameVisible - called when the renderer became visible by

any camera

• OnBecameInvisible - called when the renderer is no longer visible by any camera

• OnLevelWasLoaded - called after a new level was loaded

• OnEnable - called when the object becomes enabled and active

• OnDisable - called when the behaviour becomes disabled () or inactive

Page 31: Introduction to Unity Classes

Methods typically overridden in response to some event:• OnGUI - called for rendering and handling GUI events

• OnDrawGizmosSelected - Implement this if you want to draw gizmos only if the object is selected.

• OnDrawGizmos - Implement this if you want to draw gizmos that are also pickable and always drawn.

Page 32: Introduction to Unity Classes

Base class for all objects Unity can reference.

Any public variable you make that derives from Object gets shown in the inspector as a drop target, allowing you to set the value from the GUI.

Page 33: Introduction to Unity Classes

AnimationClip AssetBundle AudioClip Component Flare Font GameObject Material Mesh PhysicMaterial ScriptableObject Shader TerrainData TextAsset Texture

Page 34: Introduction to Unity Classes

Physics material describes how to handle colliding objects.• friction, bounciness

Page 35: Introduction to Unity Classes

Class for generating random data. Class variables

• seed - the seed for the random number generator• value - a random number between 0.0 [inclusive] and 1.0

[inclusive]• insideUnitSphere - a random point inside a sphere with radius 1• insideUnitCircle - a random point inside a circle with radius 1• onUnitSphere - a random point on the surface of a sphere with

radius 1• rotation - a random rotation

Class functions• static function Range ( min : float, max : float ) : float

Returns a random float number between and min [inclusive] and max [inclusive].

Page 36: Introduction to Unity Classes

Access to display information.

Screen class can be used to get the list of supported resolutions, switch the current resolution, hide or show the system mouse pointer.

Page 37: Introduction to Unity Classes

Class variables• resolutions

All fullscreen resolutions supported by the monitor (Read Only).

• currentResolution The current screen resolution (Read Only).

• showCursor Should the cursor be visible?

• lockCursor Should the cursor be locked?

• width The current width of the screen window in pixels (Read Only).

• height The current height of the screen window in pixels (Read Only).

• fullScreen Is the game running fullscreen?

Class functions• SetResolution

Switches the screen resolution.

Page 38: Introduction to Unity Classes

Access system information.• OS, amount of memory, # of processors,

etc.

Page 39: Introduction to Unity Classes

The interface to get time information from Unity.

Class variable(s)• time - The time this frame has started (Read

Only). This is the time in seconds since the start of the game.

Page 40: Introduction to Unity Classes

Position (translation), rotation, and scale of an object.• Every object in a scene has a Transform.

It's used to store and manipulate the position, rotation and scale of the object. Every Transform can have a parent, which allows you to apply position, rotation and scale hierarchically. This is the hierarchy seen in the Hierarchy pane. They also support enumerators (iterators?) so you can loop through children using C# foreach.

Page 41: Introduction to Unity Classes

Variables:• position - The position of the transform in world

space.• eulerAngles - The rotation as Euler angles in degrees.• rotation - The rotation of the transform in world

space stored as a Quaternion.• right - The red axis of the transform in world space.• up - The green axis of the transform in world space.• forward - The blue axis of the transform in world

space.• localScale - The scale of the transform relative to the

parent.

Page 42: Introduction to Unity Classes

Functions• Translate - Moves the transform in the direction and

distance of translation.• Rotate - Applies a rotation of eulerAngles.z degrees around

the z axis, eulerAngles.x degrees around the x axis, and eulerAngles.y degrees around the y axis (in that order).

• RotateAround - Rotates the transform about axis passing through point in world coordinates by angle degrees.

• LookAt - Rotates the transform so the forward vector points at target's current position.

• TransformPoint - Transforms position from local space to world space.