改写Unity DropDown 支持多次点击同一选项均回调
生活随笔
收集整理的這篇文章主要介紹了
改写Unity DropDown 支持多次点击同一选项均回调
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[很久前的一個Note,不知道現在的Unity Dropdown是否已經支持該特性]
Unity UGUI是開源的:?https://bitbucket.org/Unity-Technologies/ui
可以下載到UI的代碼閱讀并改寫
下面的DropdownEx類在Dropdown基礎上,增加一個m_AlwaysCallback 變量,勾選后每次點擊都會觸發回調
using System; using System.Collections; using System.Collections.Generic; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.UI.CoroutineTween; using UnityEngine.UI; using UnityEngine;public class DropdownEx : Dropdown {public bool m_AlwaysCallback = false;public void Show(){base.Show();Transform toggleRoot = transform.FindChild("Dropdown List/Viewport/Content");Toggle[] toggleList = toggleRoot.GetComponentsInChildren<Toggle>(false);for(int i = 0; i < toggleList.Length; i++){Toggle temp = toggleList[i];temp.onValueChanged.RemoveAllListeners();temp.isOn = false;temp.onValueChanged.AddListener(x => OnSelectItemEx(temp));}}public override void OnPointerClick(PointerEventData eventData){Show();}public void OnSelectItemEx(Toggle toggle){if (!toggle.isOn){toggle.isOn = true;return;}int selectedIndex = -1;Transform tr = toggle.transform;Transform parent = tr.parent;for (int i = 0; i < parent.childCount; i++){if (parent.GetChild(i) == tr){// Subtract one to account for template child.selectedIndex = i - 1;break;}}if (selectedIndex < 0)return;if (value == selectedIndex && m_AlwaysCallback)onValueChanged.Invoke(value);elsevalue = selectedIndex;Hide();}}?
補充下Editor腳本
using UnityEngine.UI; using UnityEditor; using UnityEditor.UI;[CustomEditor(typeof(DropdownEx), true)] [CanEditMultipleObjects] public class DropdownExEditor : DropdownEditor {SerializedProperty m_AlwaysCallback;protected override void OnEnable(){base.OnEnable();m_AlwaysCallback = serializedObject.FindProperty("m_AlwaysCallback");}public override void OnInspectorGUI(){base.OnInspectorGUI();EditorGUILayout.PropertyField(m_AlwaysCallback);serializedObject.ApplyModifiedProperties();} }?
轉載于:https://www.cnblogs.com/wmalloc/p/7234319.html
總結
以上是生活随笔為你收集整理的改写Unity DropDown 支持多次点击同一选项均回调的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库的三大范式以及五大约束
- 下一篇: js便利json 数组的方法