多个模型围圈无限旋转动画改进

时间:2022-02-02 01:19:18

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class ModelAnimal : MonoBehaviour
{
//记录鼠标滑动
public Vector2 lastPos;//鼠标上次位置
Vector2 currPos;//鼠标当前位置
public Vector2 offset;//两次位置的偏移值
//实例化
public static ModelAnimal instance;
//需滑动的物体坐标
public List vertices = new List();
//需滑动的物体
public GameObject[] objArry;
private void Awake()
{

    instance = this;

    for (int i = 0; i < objArry.Length; i++)
    {
        vertices.Add(objArry[i].transform.localPosition);
    }
}  
void Update()
{
    DragModel();
}
/// <summary>
/// 拖拽事件
/// </summary>
public void DragModel()
{        
    if (Input.GetMouseButtonDown(0))
    {
        lastPos = Input.mousePosition;
        Debug.Log(lastPos);
    }
    if (Input.GetMouseButtonUp(0))
    {
        currPos = Input.mousePosition;
        offset = currPos - lastPos;
        Debug.Log(currPos);
        Debug.Log("偏移值"+offset);
        DoMatch(offset);
    }
}
/// <summary>
/// 鼠标左滑事件
/// </summary>
public void AnimatorMoveLeft()
{
    Vector3 finalPos = vertices[objArry.Length - 1];
    for (int i = objArry.Length - 1; i >= 0; i--)
    {
        if (i == 0)
        {
            //objArry[i].transform.localPosition = finalPos;
            objArry[i].transform.DOLocalMove(finalPos, 0.2f).OnComplete(()=>
            {
                GameObject temp = objArry[0];
                for (int i = 0; i < objArry.Length; i++)
                {
                    if (i < objArry.Length - 1)
                    {
                        objArry[i] = objArry[i + 1];
                    }
                    else if (i == objArry.Length - 1)
                    {
                        objArry[i] = temp;
                    }
                }
                this.enabled = false;
            });
        }
        else 
        {
            objArry[i].transform.DOLocalMove(vertices[i - 1], 0.2f);
        }
    }
 
}

/// <summary>
/// 鼠标右滑事件
/// </summary>
public void AnimatorMoveRight()
{                      
    Vector3 Lastpos = vertices[0];
    for (int i = 0; i <= objArry.Length - 1; i++)
    {
        if (i < objArry.Length - 1)
        {
            //objArry[i].transform.localPosition = objArry[i + 1].transform.localPosition;
            objArry[i].transform.DOLocalMove(vertices[i + 1], 0.2f);
        }
        else if (i == objArry.Length - 1)
        {
            //objArry[i].transform.localPosition = Lastpos;
            objArry[i].transform.DOLocalMove(Lastpos, 0.2f).OnComplete(() =>
            {
                GameObject temp = objArry[objArry.Length - 1];
                for (int i = objArry.Length - 1; i > 0; i--)
                {
                    objArry[i] = objArry[i - 1];
                }
                objArry[0] = temp;
            });
        }     
    }
    
    instance.GetComponent<ModelAnimal>().enabled = false;
}

/// <summary>
/// 移动方向判断
/// </summary>
/// <param name="_offset"></param>
void DoMatch(Vector2 _offset)
{
    //水平移动
    if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y))
    {
        Debug.Log(offset.x);
        if (offset.x > 100f )
        {
            Debug.Log("右");
            AnimatorMoveRight();
        }
        else if(offset.x < -100f)
        {
            Debug.Log("左");
            AnimatorMoveLeft();
        }
    }
    else//垂直移动
    {
        if (offset.y > 0)
        {
            Debug.Log("上");
        }
        else
        {
            Debug.Log("下");
        }
    }
}

}