Game Development Basics CS526: Mobile Games
MonoBehaviour
Base class to derive from to implement game logic
Copyright By PowCoder代写 加微信 powcoder
Attached to a Game Object as a component
Expose public fields as parameters configurable in Unity Inspector
https:docs.unity3d.comScriptReferenc eMonoBehaviour.html
Awake and Start
Awake: This function is always called before any Start functions and also just after a prefab is instantiated. If a GameObject is inactive during start up Awake is not called until it is made active.
Start: Start is called before the first frame update only if the script instance is enabled.
How to best use Awake and Start
Rule of Thumb:
Use Awake for any set up that does not involve another class
Use Start for any set up that is dependent on any other classes to be ready
This ensures that your classes are ready and available before some other
class tries to interact with it as part of its setup.
You could modify Script Execution Order, but we recommend you start with the proper
architecture
OnEnable OnDisable
OnEnable: only called if the Object is active: This function is called just after the object is enabled. This happens when a MonoBehaviour instance is created, such as when a level is loaded or a GameObject with the script component is instantiated.
When is OnEnable or OnDisable useful?
Often times, you will enable or disable a script or a gameobject when you dont want it to be running.
For example:
EnableDisable a User Interface menu only when it is in use
Graphic doesnt need to render the menu.
Scripts dont need to fetch the latest values from the system.
How to enabledisable?
You can use:
Disable a GameObject https:docs.unity3d.comScriptRef erenceGameObject.SetActive.html
Disable a Monobehaviour script or components
https:docs.unity3d.comScriptRef erenceBehaviourenabled.html
Fixed Update vs Update
Fixed Update happens at a consistent interval Edit Settings Time Fixed Timestep
Update happens based on a frame.
Generally, as fast as your CPUGPU allow
https:docs.unity3d.comScriptReferenceApplicationtargetFrameRate.html
Frame Per Second FPS
30 FPS: Mobile Games and most games
60 FPS: Action Games
90 FPS: VR Games for HTC Vive, somewhere between 72120 FPS for
Why not higher? Restricted by CPUGPU
https:www.techsmith.comblogframeratebeginnersguide::textThe se20two20are20pretty20straightforward,files20and20longer20e xport20times.
LateUpdate
LateUpdate, as the name suggests, happen after Update
This is useful to order script execution. For example a follow camera
should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
https:docs.unity3d.comScriptReferenceMonoBehaviour.LateUpdate.html
real time: the amount of time that has elapsed in the real world
game time: how much time has elapsed in the games world
delta time: time elapsed between consecutive frames
Used to provide frameindependence
Avoid constant updates of variables
Instead of this.hp 5;
Do this.hp 5 Time.deltaTime;
If your game runs at 100 frames per second, hp will change by 500 units
If your game runs at 10 frames per second, hp will change by 50 units
Using deltaTime, hp will change by 5 units regardless of frame rate.
Coroutines
Use coroutines for timers to suspend execution until some condition
Options for yielding:
WaitForSeconds
WaitForFixedUpdate
yield a coroutine to chain
coroutines wait until another completes
Game Entities
Each object that we can interact with
Representation: Models Audio
State: scripts describing current state in the world
Behavior: how it behaves interacts with things in the world
Update every frame
Communication is usually using events
Skeletal meshes
Maps Normal, Specular, Environment
Represented in Scene Graph
Represents a hierarchy of objects in
Usually holds transformations
position, rotation, scale and geometry
Prefab system: allows configuring game objects as reusable templates
Can also instantiate prefabs from game logic
Good way to separate which files developers are working on
Game State
Controllers can hold the current state of the game
State machines: Identify and use States and Behaviors
e.g. Pac man: Ghosts States
name 1 if you can name all the ghosts off the top of your head
state eatable or not
changing state
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com