Game Development Basics CS-526: 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.com/ScriptReferenc e/MonoBehaviour.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 don¡¯t want it to be running.
¡ñ For example:
Enable/Disable a User Interface menu only when it is in use
¡ð Graphic doesn¡¯t need to render the menu.
¡ð Scripts don¡¯t need to fetch the latest values from the system.
How to enable/disable?
¡ñ You can use:
¡ð Disable a GameObject https://docs.unity3d.com/ScriptRef erence/GameObject.SetActive.html
¡ð Disable a Monobehaviour script or components
¡ð https://docs.unity3d.com/ScriptRef erence/Behaviour-enabled.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 CPU/GPU allow
¡ð https://docs.unity3d.com/ScriptReference/Application-targetFrameRate.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 (72~120 FPS) for
¡ñ Why not higher? Restricted by CPU/GPU
¡ñ https://www.techsmith.com/blog/frame-rate-beginners-guide/#:~:text=The se%20two%20are%20pretty%20straightforward,files%20and%20longer%20e xport%20times.
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.com/ScriptReference/MonoBehaviour.LateUpdate.html
¡ñ real time: the amount of time that has elapsed in the real world
¡ñ game time: how much time has elapsed in the game¡¯s world
¡ñ delta time: time elapsed between consecutive frames
¡ð Used to provide frame-independence
¡ð 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
¡ñ Contains
¡ð 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
¡ñ Graphics
¡ð Skeletal meshes
¡ð Textures
¡ö Maps (Normal, Specular, Environment)
¡ð Particles
¡ñ 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 re-usable 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)
¡ö direction
¡ð Behaviors
¡ö changing state
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com