Panes, panels and windows

Revisado con versión: 4.6

-

Dificultad: Intermedio

In this session we will be looking at Panes, Panels and Windows. How to make them, resize them, open them, close them and drag them around the screen. Tutor - Adam Buckner

Panes, panels and windows

Intermedio User Interface (UI)

Download the project images (including Character Icons and Demo UI Images) HERE.

DragPanel

Code snippet

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;

public class DragPanel : MonoBehaviour, IPointerDownHandler, IDragHandler {

    private Vector2 pointerOffset;
    private RectTransform canvasRectTransform;
    private RectTransform panelRectTransform;

    void Awake () {
        Canvas canvas = GetComponentInParent <Canvas>();
        if (canvas != null) {
            canvasRectTransform = canvas.transform as RectTransform;
            panelRectTransform = transform.parent as RectTransform;
        }
    }

    public void OnPointerDown (PointerEventData data) {
        panelRectTransform.SetAsLastSibling ();
        RectTransformUtility.ScreenPointToLocalPointInRectangle (panelRectTransform, data.position, data.pressEventCamera, out pointerOffset);
    }

    public void OnDrag (PointerEventData data) {
        if (panelRectTransform == null)
            return;

        Vector2 pointerPostion = ClampToWindow (data);

        Vector2 localPointerPosition;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle (
            canvasRectTransform, pointerPostion, data.pressEventCamera, out localPointerPosition
        )) {
            panelRectTransform.localPosition = localPointerPosition - pointerOffset;
        }
    }

    Vector2 ClampToWindow (PointerEventData data) {
        Vector2 rawPointerPosition = data.position;

        Vector3[] canvasCorners = new Vector3[4];
        canvasRectTransform.GetWorldCorners (canvasCorners);
        
        float clampedX = Mathf.Clamp (rawPointerPosition.x, canvasCorners[0].x, canvasCorners[2].x);
        float clampedY = Mathf.Clamp (rawPointerPosition.y, canvasCorners[0].y, canvasCorners[2].y);

        Vector2 newPointerPosition = new Vector2 (clampedX, clampedY);
        return newPointerPosition;
    }
}

TogglePanelButton

Code snippet

using UnityEngine;
using System.Collections;

public class TogglePanelButton : MonoBehaviour {

    public void TogglePanel (GameObject panel) {
        panel.SetActive (!panel.activeSelf);
    }
}

ResizePanel

Code snippet

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ResizePanel : MonoBehaviour, IPointerDownHandler, IDragHandler {
    
    public Vector2 minSize;
    public Vector2 maxSize;
    
    private RectTransform rectTransform;
    private Vector2 currentPointerPosition;
    private Vector2 previousPointerPosition;
    
    void Awake () {
        rectTransform = transform.parent.GetComponent<RectTransform>();
    }
    
    public void OnPointerDown (PointerEventData data) {
        rectTransform.SetAsLastSibling();
        RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out previousPointerPosition);
    }
    
    public void OnDrag (PointerEventData data) {
        if (rectTransform == null)
            return;
        
        Vector2 sizeDelta = rectTransform.sizeDelta;
        
        RectTransformUtility.ScreenPointToLocalPointInRectangle (rectTransform, data.position, data.pressEventCamera, out currentPointerPosition);
        Vector2 resizeValue = currentPointerPosition - previousPointerPosition;
        
        sizeDelta += new Vector2 (resizeValue.x, -resizeValue.y);
        sizeDelta = new Vector2 (
            Mathf.Clamp (sizeDelta.x, minSize.x, maxSize.x),
            Mathf.Clamp (sizeDelta.y, minSize.y, maxSize.y)
            );
        
        rectTransform.sizeDelta = sizeDelta;
        
        previousPointerPosition = currentPointerPosition;
    }
}

Focus Panel

Code snippet

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class FocusPanel : MonoBehaviour, IPointerDownHandler {

    private RectTransform panel;

    void Awake () {
        panel = GetComponent <RectTransform> ();  
    }

    public void OnPointerDown (PointerEventData data) {
        panel.SetAsLastSibling ();
    }

}

Tutoriales relacionados