UGUI学习笔记

来源:互联网 发布:剑网三正太脸型数据图 编辑:程序博客网 时间:2024/05/16 18:45

inertia惰性;迟钝

拖动的事件:IBeginDragHandler, IDragHandler, IEndDragHandler           IDropHandler, IPointerEnterHandler, IPointerExitHandler     Scene:Drag And Drop

IPointerClickHandler       Scene:Lighting

IPointerDownHandler, IDragHandler          IPointerDownHandler, IDragHandler        Scene:Draggable Panel

Button的动画过渡可用Animator Controller,不好的是要自己写脚本转换状态

拖拽的效果比较麻烦,需要调用Drag、Drop、Pointer接口

脚本的事件传递还是SendMessage,效率较低

跟NGUI相比,没有Atlas的概念


Auto Layout 
自动布局系统提供方法来将元素放置在嵌套的布局组水平组、 垂直的组或网格等中。 它还允许自动根据包含的内容大小来调整元素的大小。 

•Content Size Fitter •Layout Element •Horizontal Layout Group •Vertical Layout Group •Grid Layout Group  




EventSystem 
EventSystem 是一种将基于输入的事件发送到应用程序中的对象,无论是键盘、 鼠标、 触摸或自定义输入。EventSystem 由发送事件的几个组件共同组成。 Overview 当您将 EventSystem 组件添加到一个游戏对象上,你会发现它并没有多少暴露的功能 时,这是因为 EventSystem 本身被设计为一个 manager and facilitator(管理和主持) EventSystem 模块之间的通信。 EventSystem 的主要作用是,如下所示: • Manage which GameObject is considered selected 
• Manage which InputModule is in use 
• Manage Raycasting (if required) 
• Updating all InputModules as required • 管理被选中游戏对象 • 管理正在使用的 InputModule • 管理 Raycasting(如果需要) • 更新所需的所有 InputModules  
Input Modules 输入的模块是 EventSystem 表现的主要逻辑怎样的,它们用于: •Handling Input 
•Managing event state 
•Sending events to scene objects. • 处理输入 
  
• 管理事件的状态 • 将事件发送到场景物体。 在同一时间只有一个 InputModule 可以在 EventSystem 中处于活动状态,并且他们必 须作为 EventSystem 组件的同一游戏物体上的组件。  
Raycasters Raycasters 用于发送确定 pointer 指针位于什么上方,对于 InputModules 使用 Raycasters 这是常见的,场景配置中用于计算 pointing 设备是 over。 有 3 种情况提供的 Raycasters 默认存在: •GraphicRaycaster - Used for UI elements 
•2DPhysicsRaycaster - Used for 2D physics elements 
•3DPhysicsRaycaster - Used for 3D physics elements • GraphicRaycaster-用于 UI 元素 • 2DPhysicsRaycaster-用于为 2D 物理元素 • 3DPhysicsRaycaster-用于为三维物理元素 

Supported Events 

•IPointerEnterHandler-OnPointerEnter-当 pointer 指针进入该对象时调用 •IPointerExitHandler-OnPointerExit- pointer 指针退出该对象时调用 •IPointerDownHandler-OnPointerDown-当指针在对象上按下时调用 •IPointerUpHandler-OnPointerUp-pointer 指针被释放 (原状态为被按下) 时调用 •IPointerClickHandler-OnPointerClick-pointer 指针在同一对象上按下并释放时调用(单击) •IBeginDragHandler-OnBeginDrag-拖动对象在拖动开始时调用 •IDragHandler-OnDrag-拖动对象,当拖动正在发生进行时调用 •IEndDragHandler-OnEndDrag-拖动对象拖动完成时调用 •IDropHandler-OnDrop-对该对象拖动完成时调用 •IScrollHandler-OnScroll-当鼠标滚轮滚动时调用 •IUpdateSelectedHandler-OnUpdateSelected-在选定的对象上 each tick 中调用 •ISelectHandler-OnSelect-当对象成为所选的对象时调用 •IDeselectHandler-OnDeselect-在被选定的对象成为取消被选择时调用 •IMoveHandler-OnMove-移动事件发生时调用 (左、 右、 上、 下等) •ISubmitHandler-OnSubmit-当按下提交按钮时调用 •ICancelHandler-OnCancel-当按下取消按钮时调用  

一个Panel的拖动 

using UnityEngine;using UnityEngine.UI;using UnityEngine.EventSystems;using System.Collections;public class DragPanel : MonoBehaviour, IPointerDownHandler, IDragHandler {private Vector2 originalLocalPointerPosition;private Vector3 originalPanelLocalPosition;private RectTransform panelRectTransform;private RectTransform parentRectTransform;void Awake () {panelRectTransform = transform.parent as RectTransform;parentRectTransform = panelRectTransform.parent as RectTransform;}public void OnPointerDown (PointerEventData data) {originalPanelLocalPosition = panelRectTransform.localPosition;RectTransformUtility.ScreenPointToLocalPointInRectangle (parentRectTransform, data.position, data.pressEventCamera, out originalLocalPointerPosition);}public void OnDrag (PointerEventData data) {if (panelRectTransform == null || parentRectTransform == null)return;Vector2 localPointerPosition;if (RectTransformUtility.ScreenPointToLocalPointInRectangle (parentRectTransform, data.position, data.pressEventCamera, out localPointerPosition)) {Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;panelRectTransform.localPosition = originalPanelLocalPosition + offsetToOriginal;}ClampToWindow ();}// Clamp panel to area of parentvoid ClampToWindow () {Vector3 pos = panelRectTransform.localPosition;Vector3 minPosition = parentRectTransform.rect.min - panelRectTransform.rect.min;Vector3 maxPosition = parentRectTransform.rect.max - panelRectTransform.rect.max;pos.x = Mathf.Clamp (panelRectTransform.localPosition.x, minPosition.x, maxPosition.x);pos.y = Mathf.Clamp (panelRectTransform.localPosition.y, minPosition.y, maxPosition.y);panelRectTransform.localPosition = pos;}}
一个setTextureOffset的效果
using UnityEngine;using UnityEngine.UI;[RequireComponent(typeof(Image))]public class ScrollDetailTexture : MonoBehaviour{public bool uniqueMaterial = false;public Vector2 scrollPerSecond = Vector2.zero;Matrix4x4 m_Matrix;Material mCopy;Material mOriginal;Image mSprite;Material m_Mat;void OnEnable (){mSprite = GetComponent<Image>();mOriginal = mSprite.material;if (uniqueMaterial && mSprite.material != null){mCopy = new Material(mOriginal);mCopy.name = "Copy of " + mOriginal.name;mCopy.hideFlags = HideFlags.DontSave;mSprite.material = mCopy;}}void OnDisable (){if (mCopy != null){mSprite.material = mOriginal;if (Application.isEditor)UnityEngine.Object.DestroyImmediate(mCopy);elseUnityEngine.Object.Destroy(mCopy);mCopy = null;}mOriginal = null;}void Update (){Material mat = (mCopy != null) ? mCopy : mOriginal;if (mat != null){Texture tex = mat.GetTexture("_DetailTex");if (tex != null){mat.SetTextureOffset("_DetailTex", scrollPerSecond * Time.time);// TODO: It would be better to add support for MaterialBlocks on UIRenderer,// because currently only one Update() function's matrix can be active at a time.// With material block properties, the batching would be correctly broken up instead,// and would work with multiple widgets using this detail shader.}}}}

3D Menu

using UnityEngine;using UnityEngine.UI;using UnityEngine.EventSystems;using System.Collections;using System.Collections.Generic;public class PanelManager : MonoBehaviour {public Animator initiallyOpen;private int m_OpenParameterId;private Animator m_Open;private GameObject m_PreviouslySelected;const string k_OpenTransitionName = "Open";const string k_ClosedStateName = "Closed";public void OnEnable(){m_OpenParameterId = Animator.StringToHash (k_OpenTransitionName);if (initiallyOpen == null)return;OpenPanel(initiallyOpen);}public void OpenPanel (Animator anim){if (m_Open == anim)return;anim.gameObject.SetActive(true);var newPreviouslySelected = EventSystem.current.currentSelectedGameObject;anim.transform.SetAsLastSibling();CloseCurrent();m_PreviouslySelected = newPreviouslySelected;m_Open = anim;m_Open.SetBool(m_OpenParameterId, true);GameObject go = FindFirstEnabledSelectable(anim.gameObject);SetSelected(go);}static GameObject FindFirstEnabledSelectable (GameObject gameObject){GameObject go = null;var selectables = gameObject.GetComponentsInChildren<Selectable> (true);foreach (var selectable in selectables) {if (selectable.IsActive () && selectable.IsInteractable ()) {go = selectable.gameObject;break;}}return go;}public void CloseCurrent(){if (m_Open == null)return;m_Open.SetBool(m_OpenParameterId, false);SetSelected(m_PreviouslySelected);StartCoroutine(DisablePanelDeleyed(m_Open));m_Open = null;}IEnumerator DisablePanelDeleyed(Animator anim){bool closedStateReached = false;bool wantToClose = true;while (!closedStateReached && wantToClose){if (!anim.IsInTransition(0))closedStateReached = anim.GetCurrentAnimatorStateInfo(0).IsName(k_ClosedStateName);wantToClose = !anim.GetBool(m_OpenParameterId);yield return new WaitForEndOfFrame();}if (wantToClose)anim.gameObject.SetActive(false);}private void SetSelected(GameObject go){EventSystem.current.SetSelectedGameObject(go);var standaloneInputModule = EventSystem.current.currentInputModule as StandaloneInputModule;if (standaloneInputModule != null && standaloneInputModule.inputMode == StandaloneInputModule.InputMode.Buttons)return;EventSystem.current.SetSelectedGameObject(null);}}

最后一个函数,一直寻找从当前gameObject到他的各个父节点直到取得所要的Component为止

using UnityEngine;using UnityEngine.EventSystems;using UnityEngine.UI;[RequireComponent(typeof(Image))]public class DragMe : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler{public bool dragOnSurfaces = true;private GameObject m_DraggingIcon;private RectTransform m_DraggingPlane;public void OnBeginDrag(PointerEventData eventData){var canvas = FindInParents<Canvas>(gameObject);if (canvas == null)return;// We have clicked something that can be dragged.// What we want to do is create an icon for this.m_DraggingIcon = new GameObject("icon");m_DraggingIcon.transform.SetParent (canvas.transform, false);m_DraggingIcon.transform.SetAsLastSibling();var image = m_DraggingIcon.AddComponent<Image>();// The icon will be under the cursor.// We want it to be ignored by the event system.CanvasGroup group = m_DraggingIcon.AddComponent<CanvasGroup>();group.blocksRaycasts = false;image.sprite = GetComponent<Image>().sprite;image.SetNativeSize();if (dragOnSurfaces)m_DraggingPlane = transform as RectTransform;elsem_DraggingPlane = canvas.transform as RectTransform;SetDraggedPosition(eventData);}public void OnDrag(PointerEventData data){if (m_DraggingIcon != null)SetDraggedPosition(data);}private void SetDraggedPosition(PointerEventData data){if (dragOnSurfaces && data.pointerEnter != null && data.pointerEnter.transform as RectTransform != null)m_DraggingPlane = data.pointerEnter.transform as RectTransform;var rt = m_DraggingIcon.GetComponent<RectTransform>();Vector3 globalMousePos;if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlane, data.position, data.pressEventCamera, out globalMousePos)){rt.position = globalMousePos;rt.rotation = m_DraggingPlane.rotation;}}public void OnEndDrag(PointerEventData eventData){if (m_DraggingIcon != null)Destroy(m_DraggingIcon);}static public T FindInParents<T>(GameObject go) where T : Component{if (go == null) return null;var comp = go.GetComponent<T>();if (comp != null)return comp;Transform t = go.transform.parent;while (t != null && comp == null){comp = t.gameObject.GetComponent<T>();t = t.parent;}return comp;}}


1 0
原创粉丝点击