Specify an area, drag the player's mouse or finger to the area, the model will be offset and used to select characters and props.
Define some properties for the model
using ; using ; using UnityEngine; public class UIModelUtil : MonoBehaviour { public Animator animator; public int id; public int index; }
Model control
using ; using ; using UnityEngine; public class UIModelControl : MonoBehaviour { public Transform modelsParent; public Transform centerPos; public float interval; public bool loop; List<UIModelUtil> models; bool isPressing; public UIDrag dragComp; Vector3 mousePos; private void Awake() { if(models == null) { int i = 0; models = new List<UIModelUtil>(); foreach(UIModelUtil util in <UIModelUtil>()) { (util); // = i; Vector3 pos = ; = i * interval; = pos; i++; } } } private void Start() { JumpToSelect(); } private void Update() { //Accept drag events if (isPressing) { float x = GetInputDeltaX(); int dir = 0; if (x > 0) dir = 1; else if (x < 0) dir = -1; //Resolution correction if (dir == 0) return; x = (x) / () * 800f; if (x > 800f) x = 800f; //Offset float currectX = (0, interval, x / 800f) * dir; Vector3 pos = ; += currectX; Transform right = GetRight().transform; Transform left = GetLeft().transform; //Set borders when not looping if ( > 2 || !loop || == 1) { if ( + interval / 10 < -) = -( + interval / 10); else if ( - interval / 10 > -) = -( - interval / 10); // = pos; } // When there are only two cycles else if ( == 2 && loop) { Transform selected = GetSelect().transform; //The current one is on the right and drag to the right if (selected == right && dir < 0) { Vector3 leftPos = ; = + interval; = leftPos; } //The current one is on the left and drag to the left else if (selected == left && dir > 0) { Vector3 rightPos = ; = - interval; = rightPos; } } = pos; AfterSelect(); } } void AfterSelect() { foreach(UIModelUtil util in models) { float dis = GetXDis(util); //Set display if (dis > interval) (false); else { //The closer you get to the middle, the more forward you get (true); float t = (dis) / interval; float y = (, , t); Vector3 pos = ; = y; = pos; } } //Location correction during loop if (loop && > 2) { Transform right = GetRight().transform; Transform left = GetLeft().transform; Transform selected = GetSelect().transform; if (selected == right) { Vector3 pos = ; += interval; = pos; } else if (selected == left) { Vector3 pos = ; -= interval; = pos; } } //Set the UI selection status (GetSelect().id, GetSelect().index); } //Select by id UIModelUtil GetById(int id) { if (models == null) return null; UIModelUtil target = null; foreach (UIModelUtil util in models) { if ( == id) return util; } return target; } //Get the currently selected UIModelUtil GetSelect() { if (models == null) return null; float min = 9999; UIModelUtil target = null; foreach(UIModelUtil util in models) { float dis = ( GetXDis(util)); if(dis < min) { target = util; min = dis; } } return target; } //The one on the rightmost of all models UIModelUtil GetRight() { if (models == null) return null; float max = -9999; UIModelUtil target = null; foreach(UIModelUtil util in models) { float dis = ; if(dis > max) { target = util; max = dis; } } return target; } //The one on the leftmost of all models UIModelUtil GetLeft() { if (models == null) return null; float min = 9999; UIModelUtil target = null; foreach(UIModelUtil util in models) { float dis = ; if(dis < min) { target = util; min = dis; } } return target; } // Press the UI control to trigger public void OnPress() { if (isPressing) return; isPressing = true; if () mousePos = ; else mousePos = (0).position; if (backing != null) StopCoroutine(backing); } //Universal control release trigger public void OnRelease() { backing = StartCoroutine(ToSelect()); isPressing = false; } Coroutine backing; //Offset after release IEnumerator ToSelect() { UIModelUtil selected = GetSelect(); float dis = GetXDis(selected); float time = (0, 1f, (dis) / interval); float timer = 0; Vector3 from = ; Vector3 to = from; = -; while(timer < time) { timer += ; float t = timer / time; Vector3 pos = (from, to, t); = pos; AfterSelect(); yield return null; } backing = null; } //Get finger offset float GetInputDeltaX() { Vector3 pos; if () pos = ; else pos = (0).position; Vector3 delta = pos - mousePos; //(pos +"/"+mousePos +"/"+ ); mousePos = pos; return ; } // Calculate the X-axis distance of the offset center position float GetXDis(UIModelUtil util) { return - ; } // Jump to the selected id public void JumpToSelect() { int id = ; Vector3 pos = ; UIModelUtil selected = GetById(id); = -; = pos; AfterSelect(); } }
The UI accepts click events:
using ; using ; using UnityEngine; using ; public class UIDrag : MonoBehaviour,IPointerDownHandler, IPointerUpHandler { public UIModelControl control; virtual public void OnPointerDown(PointerEventData data) { (); } virtual public void OnPointerUp(PointerEventData data) { (); } virtual public void OnSelected(int id, int index) { } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.