Unity Coroutines vs C# Async. Programming. Cameras. General. Viewport Rect (Split-Screen Setup). Creating a Layered GUI Camera
Unity Coroutines vs C# Async ● Async: ○ Best Practices for Async vs Coroutines: Best practices: Async vs. coroutines - Unite Copenhagen ○ Best for handling User Interaction, Network Calls ● Coroutines: ○ Unity - IEnumerator's yield return null ■ The top answer by user “Everts” gives a very good explanation of what a Coroutine is ○ Does not support Return values ○ Best for “Fire and forget” scenarios; ■ Closing a Window ■ Animating a background detail ○ Coroutine Method: A coroutine must be declared as an “IEnumerator” IEnumerator MethodName() { SomeFunction(); yield return null; AnotherFunction(); } ○ Yield return null: This will cause the method to wait for a single frame before executing AnotherFunction(); ● Yield return WaitForSeconds: ○ If you intend to wait more than a single frame using “yield return null” you can use WaitForSeconds instead. ● Garbage Collection Tip: ○ The typical way of using “Yield return WaitForSeconds” involves creating a “new” WaitForSeconds in the return line as seen below; ○ The full example below results in less Garbage Collection Allocation; by declaring a “shortWait” and returning that in the Coroutine instead ● Coroutine Example Use Snippet: The following snippet creates a series of GameObjects based on a prefab, and assigns each a sprite based on the Sprites List public GameObject letterPrefab; public List< Sprite> letters = new List< Sprite> (); public float letterDelay = 0. 05f;
WaitForSeconds shortWait = new WaitForSeconds(letterDelay);
void Start() { StartCoroutine(SpawnLetters()); }
IEnumerator SpawnLetters() { foreach (var letter in letters) { GameObject newLetter = Instantiate(letterPrefab, transform, false); newLetter. GetComponent< Image> (). sprite = letter; yield return shortWait; } } ● Coroutine Task Manager: ○ https: //forum. unity. com/threads/a-more-flexible-coroutine-interface. 94220/? _ga=2. 153570993. 846436558. 1592243927-174792342. 1591205497 ● Using Async: Programming ● Check Implementation: To see if a class inherits from a given class, you can use a cast or GetComponent: ● Convert Degree into Vector2; Convert a Float into a x / y Vector2 ○ Other helper Functions; Equivalent of " Degree to Vector2" in Unity
Cameras General ● Reference: https: //docs. unity3d. com/Manual/class-Camera. html ● Clear Flags: What the Camera is “cleared with” each frame ○ Skybox: By default, the camera is cleared with the “Skybox” ○ Solid Color: A flat color, preferable for 2D games ○ Depth Only: All information about where an object exists in 3D space is discarded (in a first person shooter, if you have your weapon aimed at a wall and it’s barrel would be clipping into the Wall, this setting would allow the weapon to be drawn without regard to the wall geometry) ○ Don’t Clear: You will handle the responsibility of clearing the camera ● Culling Mask: Which Layers the Camera can see. ○ By default, each Camera can see “Everything” ● Depth: If multiple Cameras exist in the Scene, they are rendered from lowest Depth to highest ○ If 2 Cameras are in the Scene; ■ Camera A with a Depth of 10 ■ Camera B with a Depth of -10 ■ Camera A will be drawn to the scene, then Camera B will be drawn over top of it. ○ To ensure multiple overlapping Camera views are visible, use the Culling Mask and Clear Flags features. ● No need for multiple Audio Listeners: Each Camera will, by default have an Audio Listener Component added to it which should be removed. Viewport Rect (Split-Screen Setup) ● Reference: Split Screen in Unity - YouTube ● Viewport Rect: Controls the X / Y position of the camera’s view within the overall viewport of the game ○ X / Y: Anchored from the bottom-left, sets the viewport position from (0-1) ○ W / H: As a percentage, sets how much of the width / height the viewport takes up (0-1) ● Split Screen Setup: ○ Top / Bottom: Set the [W/H] to [1, 0. 5] for both Cameras (so that they each only occupy half the height of the overall view) ■ Set the Viewport of Camera A to; [X/Y] [0, 0. 5] to push it half-way up the view ■ Leave the Viewport of Camera B to; [X/Y] [0, 0] Creating a Layered GUI Camera ● If you need an Orthographic Camera to render 3D objects on a GUI layer while a regular Perspective Camera renders the rest of the Scene; ● Reference: Camera Layering in Unity - YouTube ● Perspective Camera Setup: ○ Culling Mask: Untick the “UI” Layer (and any other Layers this camera should not see) ● GUI Camera Setup: ○ Clear Flags: Depth Only ○ Culling Mask: Set to Nothing but “UI” (and any other layers this should see) ○ Projection: Orthographic ○ Depth: 10 (any Depth above the Perspective Camera to ensure it renders above it) ○ Remove Audio Listener Component: If there is a 2nd one which you don’t want ● Canvas Setup: ○ Set Layer: To “UI” ○ Render Mode: Screen Space - Camera ○ Render Camera: Specify reference to GUI Camera ○ Optional; set the GUI Camera as a Child of the Canvas ■ This way, if the Canvas is disabled, so is the GUI Camera
Воспользуйтесь поиском по сайту: ©2015 - 2024 megalektsii.ru Все авторские права принадлежат авторам лекционных материалов. Обратная связь с нами...
|