Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement the drag-and-drop Model selection function in UnityUI

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how UnityUI to achieve drag-and-drop model selection function, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Specify an area, which is dragged by the player's mouse or finger. The model will be offset and used to select characters and props.

Define some properties for the model

Using System.Collections;using System.Collections.Generic;using UnityEngine;public class UIModelUtil: MonoBehaviour {public Animator animator; public int id; public int index;}

Model control

Using System.Collections;using System.Collections.Generic;using UnityEngine;public class UIModelControl: MonoBehaviour {public Transform modelsParent; public Transform centerPos; public float interval; public bool loop; List models; bool isPressing; public UIDrag dragComp; Vector3 mousePos; private void Awake () {if (models = = null) {int I = 0; models = new List (); foreach (UIModelUtil util in modelsParent.GetComponentsInChildren ()) {models.Add (util); / / util.index = I; Vector3 pos = Vector3.zero; pos.x = I * interval; util.transform.localPosition = pos; iTunes + } private void Start () {JumpToSelect ();} private void Update () {/ / accept the drag event if (isPressing) {float x = GetInputDeltaX (); int dir = 0; if (x > 0) dir = 1; else if (x

< 0) dir = -1; //分辨率修正 if (dir == 0) return; x = Mathf.Abs(x) / (Screen.width) * 800f; if (x >

800f) x = 800f; / / offset float currectX = Mathf.Lerp (0, interval, x / 800f) * dir; Vector3 pos = modelsParent.position; pos.x + = currectX; Transform right = GetRight (). Transform; Transform left = GetLeft (). Transform; / / sets the border if (models.Count > 2 | | loop | models.Count = = 1) {if (right.localPosition.x + interval / 10) when not looping

< -pos.x) pos.x = -(right.localPosition.x + interval / 10); else if (left.localPosition.x - interval / 10 >

-pos.x) pos.x =-(left.localPosition.x-interval / 10); / / modelsParent.position = pos;} / / when there are only two loops else if (models.Count = = 2 & & loop) {Transform selected = GetSelect (). Transform; / / currently the one on the right and drag if to the right (selected = = right & & dir

< 0) { Vector3 leftPos = left.localPosition; leftPos.x = right.localPosition.x + interval; left.localPosition = leftPos; } //当前是左边那个且向左拖拽 else if (selected == left && dir >

0) {Vector3 rightPos = right.localPosition; rightPos.x = left.localPosition.x-interval; right.localPosition = rightPos;}} modelsParent.position = pos; AfterSelect ();}} void AfterSelect () {foreach (UIModelUtil util in models) {float dis = GetXDis (util); / / set to display if (dis > interval) util.gameObject.SetActive (false); else {/ / closer to the middle util.gameObject.SetActive (true); float t = Mathf.Abs (dis) / interval Float y = Mathf.Lerp (centerPos.position.z, modelsParent.position.z, t); Vector3 pos = util.transform.position; pos.z = y; util.transform.position = pos;}} / / cycle time position correction if (loop & & models.Count > 2) {Transform right = GetRight (). Transform; Transform left = GetLeft (). Transform; Transform selected = GetSelect (). Transform; if (selected = right) {Vector3 pos = right.position; pos.x + = interval; left.position = pos } else if (selected = = left) {Vector3 pos = left.position; pos.x-= interval; right.position = pos;}} / / set UI selection status dragComp.OnSelected (GetSelect (). Id, GetSelect () .index);} / / Select UIModelUtil GetById (int id) {if (models = = null) return null; UIModelUtil target = null; foreach (UIModelUtil util in models) {if (util.id = = id) return util;} return target via id } / / get the currently selected UIModelUtil GetSelect () {if (models = = null) return null; float min = 9999; UIModelUtil target = null; foreach (UIModelUtil util in models) {float dis = Mathf.Abs (GetXDis (util)); if (dis)

< min) { target = util; min = dis; } } return target; } //所有模型最右边的那个 UIModelUtil GetRight() { if (models == null) return null; float max = -9999; UIModelUtil target = null; foreach(UIModelUtil util in models) { float dis = util.transform.localPosition.x; if(dis >

Max) {target = util; max = dis;}} return target;} / / the leftmost UIModelUtil GetLeft () {if (models = = null) return null; float min = 9999; UIModelUtil target = null; foreach (UIModelUtil util in models) {float dis = util.transform.localPosition.x; if (dis < min) {target = util; min = dis;} return target;} / / UI control press to trigger public void OnPress () {if (isPressing) return; isPressing = true If (Application.isEditor) mousePos = Input.mousePosition; else mousePos = Input.GetTouch (0) .position; if (backing! = null) StopCoroutine (backing);} / / UI control release trigger public void OnRelease () {backing = StartCoroutine (ToSelect ()); isPressing = false;} Coroutine backing; / / offset IEnumerator ToSelect () {UIModelUtil selected = GetSelect (); float dis = GetXDis (selected); float time = Mathf.Lerp (0,1f, Mathf.Abs (dis) / interval); float timer = 0 Vector3 from = modelsParent.localPosition; Vector3 to = from; to.x =-selected.transform.localPosition.x; while (timer < time) {timer + = Time.deltaTime; float t = timer / time; Vector3 pos = Vector3.Lerp (from, to, t); modelsParent.localPosition = pos; AfterSelect (); yield return null;} backing = null;} / / get finger offset float GetInputDeltaX () {Vector3 pos; if (Application.isEditor) pos = Input.mousePosition; else pos = Input.GetTouch (0) .position; Vector3 delta = pos-mousePos / / Debug.Log (pos + "/" + mousePos + "/" + delta.x); mousePos = pos; return delta.x;} / / calculate the X-axis distance of the offset center float GetXDis (UIModelUtil util) {return util.transform.position.x-centerPos.position.x;} / / Jump to the selected id public void JumpToSelect () {int id = CharacterManager.characterId; Vector3 pos = modelsParent.localPosition; UIModelUtil selected = GetById (id); pos.x =-selected.transform.localPosition.x ModelsParent.localPosition = pos; AfterSelect ();}}

UI accept Click event:

Using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.EventSystems;public class UIDrag: MonoBehaviour,IPointerDownHandler, IPointerUpHandler {public UIModelControl control; virtual public void OnPointerDown (PointerEventData data) {control.OnPress ();} virtual public void OnPointerUp (PointerEventData data) {control.OnRelease ();} virtual public void OnSelected (int id, int index) {}}

These are all the contents of the article "how UnityUI implements drag-and-drop model selection". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report