Unity UGUI Selectable部分源码浅析

2024-01-30 09:44

本文主要是介绍Unity UGUI Selectable部分源码浅析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

private readonly List<CanvasGroup> m_CanvasGroupCache = new List<CanvasGroup>();protected override void OnCanvasGroupChanged(){//判断父节点中是否允许交互var groupAllowInteraction = true;Transform t = transform;while (t != null){t.GetComponents(m_CanvasGroupCache);bool shouldBreak = false;for (var i = 0; i < m_CanvasGroupCache.Count; i++){//如果父节点中的canvasgroup不允许交互,那么断开循环,设置falseif (!m_CanvasGroupCache[i].interactable){groupAllowInteraction = false;shouldBreak = true;}// 如果此节点设置了“忽略父设置”,那么也直接断开if (m_CanvasGroupCache[i].ignoreParentGroups)shouldBreak = true;}if (shouldBreak)break;t = t.parent;}if (groupAllowInteraction != m_GroupsAllowInteraction){m_GroupsAllowInteraction = groupAllowInteraction;OnSetProperty();}}

在canvasGroup属性改变时调用OnCanvasGroupChanged

public virtual bool IsInteractable(){return m_GroupsAllowInteraction && m_Interactable;}

物体的可交互属性需要一起判断m_GroupsAllowInteraction 和 m_Interactable,即自身或父物体上的canvasGroup是否允许交互,以及自身是否允许交互

protected override void OnEnable(){base.OnEnable();if (s_IsDirty)RemoveInvalidSelectables();  //如果标记为脏,就从可选择物体数组中移除m_WillRemove = false;if (s_SelectableCount == s_Selectables.Length)   //如果可选择物体数组满了,则扩容两倍{Selectable[] temp = new Selectable[s_Selectables.Length * 2];Array.Copy(s_Selectables, temp, s_Selectables.Length);s_Selectables = temp;}s_Selectables[s_SelectableCount++] = this;isPointerDown = false;DoStateTransition(currentSelectionState, true);  //设置初始状态}
protected override void OnTransformParentChanged(){base.OnTransformParentChanged();// 如果父物体有改变,需要判断自身和父物体的canvasGroup属性是否有改变OnCanvasGroupChanged();}
private void OnSetProperty(){
#if UNITY_EDITORif (!Application.isPlaying)DoStateTransition(currentSelectionState, true);else
#endifDoStateTransition(currentSelectionState, false);}

设置状态属性,如果是Editor模式,如果正在运行,那么立即改变,没有过渡,如果非Editor模式,那么有过渡

        protected override void OnDisable(){m_WillRemove = true;s_IsDirty = true;InstantClearState();  //重置各种状态base.OnDisable();}

取消激活时,将自身标记为脏

private static void RemoveInvalidSelectables(){for (int i = s_SelectableCount - 1; i >= 0; --i){// Swap last element in array with element to be removedif (s_Selectables[i] == null || s_Selectables[i].m_WillRemove)s_Selectables[i] = s_Selectables[--s_SelectableCount];}s_IsDirty = false;}

将元素从可选择物体数组列表中移除,同时标记为脏

protected SelectionState currentSelectionState{get{if (!IsInteractable())return SelectionState.Disabled;if (isPointerDown)return SelectionState.Pressed;if (hasSelection)return SelectionState.Selected;if (isPointerInside)return SelectionState.Highlighted;return SelectionState.Normal;}}

获取当前的选择状态

protected virtual void InstantClearState(){string triggerName = m_AnimationTriggers.normalTrigger;isPointerInside = false;isPointerDown = false;hasSelection = false;switch (m_Transition){case Transition.ColorTint:StartColorTween(Color.white, true);break;case Transition.SpriteSwap:DoSpriteSwap(null);break;case Transition.Animation:TriggerAnimation(triggerName);break;}}

即时清除状态,将Selectable的状态还原

protected virtual void DoStateTransition(SelectionState state, bool instant){if (!gameObject.activeInHierarchy)return;Color tintColor;Sprite transitionSprite;string triggerName;switch (state){case SelectionState.Normal:tintColor = m_Colors.normalColor;transitionSprite = null;triggerName = m_AnimationTriggers.normalTrigger;break;case SelectionState.Highlighted:tintColor = m_Colors.highlightedColor;transitionSprite = m_SpriteState.highlightedSprite;triggerName = m_AnimationTriggers.highlightedTrigger;break;case SelectionState.Pressed:tintColor = m_Colors.pressedColor;transitionSprite = m_SpriteState.pressedSprite;triggerName = m_AnimationTriggers.pressedTrigger;break;case SelectionState.Selected:tintColor = m_Colors.selectedColor;transitionSprite = m_SpriteState.selectedSprite;triggerName = m_AnimationTriggers.selectedTrigger;break;case SelectionState.Disabled:tintColor = m_Colors.disabledColor;transitionSprite = m_SpriteState.disabledSprite;triggerName = m_AnimationTriggers.disabledTrigger;break;default:tintColor = Color.black;transitionSprite = null;triggerName = string.Empty;break;}switch (m_Transition){case Transition.ColorTint:StartColorTween(tintColor * m_Colors.colorMultiplier, instant);break;case Transition.SpriteSwap:DoSpriteSwap(transitionSprite);break;case Transition.Animation:TriggerAnimation(triggerName);break;}}

状态切换方法

public Selectable FindSelectable(Vector3 dir){dir = dir.normalized;Vector3 localDir = Quaternion.Inverse(transform.rotation) * dir;Vector3 pos = transform.TransformPoint(GetPointOnRectEdge(transform as RectTransform, localDir));float maxScore = Mathf.NegativeInfinity;Selectable bestPick = null;if (s_IsDirty)RemoveInvalidSelectables();for (int i = 0; i < s_SelectableCount; ++i){Selectable sel = s_Selectables[i];if (sel == this)continue;if (!sel.IsInteractable() || sel.navigation.mode == Navigation.Mode.None)continue;#if UNITY_EDITOR除了运行时使用,FindSelectable被自定义编辑器用来在不同的可选项之间绘制箭头。对于场景视图摄像机,只考虑同一阶段的可选对象。if (Camera.current != null && !UnityEditor.SceneManagement.StageUtility.IsGameObjectRenderedByCamera(sel.gameObject, Camera.current))continue;
#endifvar selRect = sel.transform as RectTransform;Vector3 selCenter = selRect != null ? (Vector3)selRect.rect.center : Vector3.zero;Vector3 myVector = sel.transform.TransformPoint(selCenter) - pos;//值就是沿着这个方向的距离float dot = Vector3.Dot(dir, myVector);//跳过方向错误或距离为0的元素这也确保了下面的评分公式不会有除数为零的误差if (dot <= 0)continue;// This scoring function has two priorities:// - Score higher for positions that are closer.// - Score higher for positions that are located in the right direction.// This scoring function combines both of these criteria.// It can be seen as this://   Dot (dir, myVector.normalized) / myVector.magnitude// The first part equals 1 if the direction of myVector is the same as dir, and 0 if it's orthogonal.// The second part scores lower the greater the distance is by dividing by the distance.// The formula below is equivalent but more optimized.//// If a given score is chosen, the positions that evaluate to that score will form a circle// that touches pos and whose center is located along dir. A way to visualize the resulting functionality is this:// From the position pos, blow up a circular balloon so it grows in the direction of dir.// The first Selectable whose center the circular balloon touches is the one that's chosen.float score = dot / myVector.sqrMagnitude;if (score > maxScore){maxScore = score;bestPick = sel;}}return bestPick;}

此方法用来寻找临近的可选择对象

public virtual void OnMove(AxisEventData eventData){switch (eventData.moveDir){case MoveDirection.Right:Navigate(eventData, FindSelectableOnRight());break;case MoveDirection.Up:Navigate(eventData, FindSelectableOnUp());break;case MoveDirection.Left:Navigate(eventData, FindSelectableOnLeft());break;case MoveDirection.Down:Navigate(eventData, FindSelectableOnDown());break;}}

当焦点移动到另一个可选择对象时,调用此方法,确定选择的对象

protected bool IsHighlighted(){if (!IsActive() || !IsInteractable())return false;return isPointerInside && !isPointerDown && !hasSelection;}

判断是否高亮,即鼠标移上去的时候,需判断三个状态,鼠标在UI范围内,没有点下,没有选择

protected bool IsPressed(){if (!IsActive() || !IsInteractable())return false;return isPointerDown;}

是否按下

private void EvaluateAndTransitionToSelectionState(){if (!IsActive() || !IsInteractable())return;DoStateTransition(currentSelectionState, false);}

执行状态改变

public virtual void OnPointerDown(PointerEventData eventData){if (eventData.button != PointerEventData.InputButton.Left)  //如果不是鼠标左键,returnreturn;//如果可以交互,导航模式不为空,且EventSystem存在,那么设置EventSystem的当前选择物体if (IsInteractable() && navigation.mode != Navigation.Mode.None && EventSystem.current != null)EventSystem.current.SetSelectedGameObject(gameObject, eventData);isPointerDown = true;EvaluateAndTransitionToSelectionState();}

鼠标按下回调

public virtual void OnPointerUp(PointerEventData eventData){if (eventData.button != PointerEventData.InputButton.Left)return;isPointerDown = false;EvaluateAndTransitionToSelectionState();}

鼠标抬起回调

public virtual void OnPointerEnter(PointerEventData eventData){isPointerInside = true;EvaluateAndTransitionToSelectionState();}

鼠标进入回调

public virtual void OnPointerExit(PointerEventData eventData){isPointerInside = false;EvaluateAndTransitionToSelectionState();}

鼠标离开回调

public virtual void OnSelect(BaseEventData eventData){hasSelection = true;EvaluateAndTransitionToSelectionState();}

选择回调

public virtual void OnDeselect(BaseEventData eventData){hasSelection = false;EvaluateAndTransitionToSelectionState();}

取消选择回调

public virtual void Select(){if (EventSystem.current == null || EventSystem.current.alreadySelecting)return;EventSystem.current.SetSelectedGameObject(gameObject);}

直接确定EventSystem的选择物体

这篇关于Unity UGUI Selectable部分源码浅析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/659838

相关文章

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

浅析CSS 中z - index属性的作用及在什么情况下会失效

《浅析CSS中z-index属性的作用及在什么情况下会失效》z-index属性用于控制元素的堆叠顺序,值越大,元素越显示在上层,它需要元素具有定位属性(如relative、absolute、fi... 目录1. z-index 属性的作用2. z-index 失效的情况2.1 元素没有定位属性2.2 元素处

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory

浅析Python中的绝对导入与相对导入

《浅析Python中的绝对导入与相对导入》这篇文章主要为大家详细介绍了Python中的绝对导入与相对导入的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1 Imports快速介绍2 import语句的语法2.1 基本使用2.2 导入声明的样式3 绝对import和相对i

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

浅析Rust多线程中如何安全的使用变量

《浅析Rust多线程中如何安全的使用变量》这篇文章主要为大家详细介绍了Rust如何在线程的闭包中安全的使用变量,包括共享变量和修改变量,文中的示例代码讲解详细,有需要的小伙伴可以参考下... 目录1. 向线程传递变量2. 多线程共享变量引用3. 多线程中修改变量4. 总结在Rust语言中,一个既引人入胜又可

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti