Главная | Обратная связь | Поможем написать вашу работу!
МегаЛекции

Canvas Setup. Toggle Component. Hide / Disable on Deselect (click away). Check if the Pointer is over a UI Element. Useful Source Code




UI

Canvas Setup

Render Mode:

Screen Space Overlay: The Canvas is overlayed on top of all camera projections

Screen Space Camera: The Canvas exists within the projection space of a specified Camera

■ Allows for 3D objects to be rendered among 2D UI objects

■ (Suggestion; use an Orthographic Camera to render this, see “Cameras” section for more information)

World Space: The Canvas exists in the Scene

Toggle Component

● Toggle | Unity UI | 1. 0. 0

● To read the current state of a UI Toggle Component, refer to its “ isOn ” Property

Hide / Disable on Deselect (click away)

using UnityEngine. EventSystems;

public class CLASSNAME: MonoBehaviour, IDeselectHandler, IPointerEnterHandler, IPointerExitHandler

{

 

  private bool mouseIsOver = false;

 

  private void OnEnable() {

       EventSystem. current. SetSelectedGameObject(gameObject);

   }

 

   public void OnDeselect(BaseEventData eventData) {

  //Close the Window on Deselect only if a click occurred outside this panel

       if (! mouseIsOver)

           CloseSelf();

   }

 

   public void OnPointerEnter(PointerEventData eventData) {

       mouseIsOver = true;

       EventSystem. current. SetSelectedGameObject(gameObject);

   }

   

   public void OnPointerExit(PointerEventData eventData) {

       mouseIsOver = false;

       EventSystem. current. SetSelectedGameObject(gameObject);

   }

}

=================== CloseOnDeselect Component ===================

using UnityEngine. EventSystems;

using UnityEngine;

 

public class CloseOnDeselect: MonoBehaviour, IDeselectHandler, IPointerEnterHandler, IPointerExitHandler

{

  private bool mouseIsOver = false;

 

  public delegate void DeselectEvent ();

public event DeselectEvent OnDeselected = delegate { };

   

private void OnEnable() {

 EventSystem. current. SetSelectedGameObject(gameObject);

}

   

public void OnDeselect(BaseEventData eventData) {

//Close the Window on Deselect only if a click occurred outside this panel

 if (! mouseIsOver)

       OnDeselected. Invoke();

}

 

public void OnPointerEnter(PointerEventData eventData) {

 mouseIsOver = true;

 EventSystem. current. SetSelectedGameObject(gameObject);

}

 

public void OnPointerExit(PointerEventData eventData) {

 mouseIsOver = false;

 EventSystem. current. SetSelectedGameObject(gameObject);

}

}

Check if the Pointer is over a UI Element

Tags: Click, Mouse, UI, Ignore Click

● Use this method to check if the pointer is currently over a UI GameObject

if (EventSystem. current. IsPointerOverGameObject()) {

 

}

Useful Source Code

PatrolPath:

Creates a path defined by 2 Vector2s which can be edited in the Scene view with a handle on the start and end vectors:

https: //drive. google. com/drive/folders/1Us6Ayko8F-AxUL5QmMAoNc7KAMxXlOxp

Taken from Unity sample Project:

https: //learn. unity. com/project/2d-platformer-template

 

Unity Extensions

TextMeshPro

Rich Text Tags:

http: //digitalnativestudios. com/textmeshpro/docs/rich-text/

TMP Input Field

● To get the “value” from a TMP Input field, get a reference to its “text” Component

● To parse this into an integer, use the Parse method as in the example below;
public TMP_InputField iterations;
int _iterations = int. Parse(iterations. text);

Set Container Size to match text contents

● The following snippet will resize a Container RectTransform to match the height of the text within an element which will enable a ScrollRect or Vertical Layout Group to function properly

Tags: Fixed width, change height, autosize, scrollrect

public TextMeshProUGUI windowText;

public RectTransform windowContent;

windowText. text = “string”;

windowText. rectTransform. sizeDelta = new Vector2(windowText. rectTransform. sizeDelta. x, windowText. preferredHeight);

windowContent. sizeDelta = new Vector2(windowContent. sizeDelta. x, windowText. preferredHeight);

 

Cinemachine

3D Camera Setup:

 

2D Camera Setup:

Create a Cinemachine Camera:

○ Cinemachine > Create 2D Camera

Set Follow: On the newly created “CM vcam 1” the “Follow” field can be set to the GameObject whose position the Camera is supposed to track.

Lens Adjustment: Changing the “Orthographic Size” of the Camera will adjust how large the camera’s field of view is.

Dutch: To control the camera’s Rotation, the “dutch” value can be set

Set Limits:

■ Enable “ Game Window Guides ” to adjust the blue “soft follow zone” and red “hard follow zone”.

Blue “Soft Follow”: Within the Blue zone, the camera will use dampening to move to match the position of a tracked GameObject

Red “Hard Follow”: Within the Red zone, the camera will match the position of a tracked GameObject without dampening.

Adding a Confiner:

○ The Camera’s position can be confined to prevent it from moving outside the boundary of the intended view-port.

Create the Volume: First, create a Composite Collider 2D or Polygon Collider 2D which will serve as the limits of the Camera

IsTrigger: In either case, this Collider will overlap the entire playable level, meaning it will trigger collisions with any Rigidbody that touches it. To fix this, set the Collider’s “Is Trigger” boolean to true.

Add the Extension: At the bottom of the Cinemachine Virtual Camera is an option to add an Extension, select “CinemachineConfiner”

■ Drag in a reference to the created collider

Switching between Virtual Cameras: Different Virtual Cameras can have completely unique behaviour (different Confiners, different follow dampening, etc. ) and can actively be switched out during play.

Поделиться:





Воспользуйтесь поиском по сайту:



©2015 - 2024 megalektsii.ru Все авторские права принадлежат авторам лекционных материалов. Обратная связь с нами...