Unity 算盘制作过程
作者:互联网
using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; public class Operation: MonoBehaviour { public GameObject allBead; public List<float> startFloat; private List<Vector3> v3; private float moveRange = 100;//珠子移动范围 private Transform ParentTF; private List<float> stopY_V3=new List<float>();//点击珠子的时候,就确定好,珠子停止Y的位置 private List<Transform> moveBead=new List<Transform>();//哪些珠子需要移动 private bool isUp=true; private float startY;//用来判断是上、下拨珠子; private Vector2 V2Delta; private float start_mouse_y; private void Start() { for(int i = 0; i < allBead.transform.childCount; i++) { Transform tf = allBead.transform.GetChild(i).transform; for (int j=0;j< tf.childCount; j++) { GameObject obj = tf.GetChild(j).gameObject; EventTriggerListener.Get(obj).onDown = onDown; EventTriggerListener.Get(obj).onDrag = onDrag; EventTriggerListener.Get(obj).onDragOut = onDragOut; // EventTriggerListener.Get(obj).onClick = onClick; if (i==0) startFloat.Add(obj.transform.localPosition.y); } } } void onDown(GameObject sender) { startY = sender.transform.localPosition.y; start_mouse_y = Input.mousePosition.y; } void onDrag(GameObject sender, Vector2 delta) { float differMouseY = Input.mousePosition.y - start_mouse_y;//整数为上拨珠子,负数为下拨珠子 ParentTF = sender.transform.parent;//此珠子的父级 //0-1是上面两个珠子的操作 for (int k = 0; k < 2; k++) { if (sender.name == ParentTF.GetChild(k).name) { V2Delta = delta; if (differMouseY > 0) { UpBead(0, k,"up2");//往上拨珠子 } else if (differMouseY < 0) { DownBead(k, 1, "up2");//往下拨珠子 print("往下操作"); } } } //-------------------------------------------------------------------------------------------------------------- //2-6是下面五个珠子的操作 for (int i = 2; i < ParentTF.childCount; i++) { if (sender.name == ParentTF.GetChild(i).name) { V2Delta = delta; if (differMouseY > 0) { UpBead(2, i,"down5");//往上拨珠子 } else if (differMouseY < 0) { DownBead(i, 6, "down5");//往下拨珠子 } } } } //往上拨珠子 void UpBead(int startNum,int stopNum ,string str) { for (int j = startNum; j <= stopNum; j++) { Vector3 pos = ParentTF.GetChild(j).transform.localPosition; pos.y += V2Delta.y; if (str == "up2") { if (pos.y < startFloat[j]) { ParentTF.GetChild(j).transform.localPosition = pos; addList(j, stopNum); } } else if (str == "down5") { if (pos.y < startFloat[j] + moveRange) { ParentTF.GetChild(j).transform.localPosition = pos; addList(j, stopNum); } } } } //往下拨珠子 void DownBead(int startNum, int stopNum, string str) { for (int j = startNum; j <= stopNum; j++) { Vector3 pos = ParentTF.GetChild(j).transform.localPosition; pos.y += V2Delta.y; if (str == "up2") { if (pos.y > startFloat[j] - moveRange) { ParentTF.GetChild(j).transform.localPosition = pos; addList(j, stopNum); } } else if (str == "down5") { if (pos.y > startFloat[j]) { ParentTF.GetChild(j).transform.localPosition = pos; addList(j, stopNum); } } } } void addList(int num,int stopNum) { if (isUp) { stopY_V3.Add(startFloat[num] + moveRange); moveBead.Add(ParentTF.GetChild(num)); if (num == stopNum) isUp = false; } } void onDragOut(GameObject sender) { float differnece = sender.transform.localPosition.y - startY; for (int i=0;i< moveBead.Count; i++) { if (differnece > 0) { if (differnece >= 20) { //“Bead1”和“Bead2”是算盘上面两个珠子 if (sender.name == "Bead1" || sender.name == "Bead2") { moveBead[i].DOLocalMoveY(stopY_V3[i] - moveRange, 0.1f); print("2上拨停上"); } else { moveBead[i].DOLocalMoveY(stopY_V3[i], 0.1f); print("5上拨停上"); } } else { if (sender.name == "Bead1" || sender.name == "Bead2") { moveBead[i].DOLocalMoveY(stopY_V3[i] - moveRange*2, 0.1f); print("2上拨停下"); } else { moveBead[i].DOLocalMoveY(stopY_V3[i] - moveRange, 0.1f); print("5上拨停下"); } } }else if (differnece < 0) { if (Mathf.Abs(differnece) >= 20) {//所有的珠子往下拨 //“Bead1”和“Bead2”是算盘上面两个珠子 if (sender.name== "Bead1"|| sender.name == "Bead2") { moveBead[i].DOLocalMoveY(stopY_V3[i] - moveRange*2, 0.1f);//本来这个地方 应该-moveRange一个,但是上面+moveRange一个,所有要-2个moveRange } else { moveBead[i].DOLocalMoveY(stopY_V3[i] - moveRange, 0.1f); } } else { if (sender.name == "Bead1" || sender.name == "Bead2") { moveBead[i].DOLocalMoveY(stopY_V3[i] - moveRange, 0.1f); } else { moveBead[i].DOLocalMoveY(stopY_V3[i], 0.1f); } } } } isUp = true; moveBead.Clear(); stopY_V3.Clear(); } }
标签:stopY,sender,moveBead,Unity,珠子,moveRange,算盘,制作,name 来源: https://www.cnblogs.com/zqiang0803/p/16276692.html