1.先看效果图,按照自己需要的顺序拆解组装模型
2.将需要移动的模型按照移动的顺序加入到 ObiController 数组中
3.将需要移动到的位置赋值给需要移动的模型上,并对应好位置关系
4.对应脚本,仅供参考,欢迎交流分享.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
//挂载到需要移动位置的模型上
public class ObiTransform : MonoBehaviour
{
//目标位置
public GameObject targetPoint;
private Vector3 targetPosi;
private Vector3 targetRote;
private Vector3 oldPosi;
private void Start()
{
targetPosi = targetPoint.transform.localPosition;
targetRote = targetPoint.transform.eulerAngles;
oldPosi = transform.localPosition;
}
public void Open()
{
transform.DOLocalMove(targetPosi, 0.5f);
}
public void Close()
{
transform.DOLocalMove(oldPosi, 0.5f);
}
}
using UnityEngine;
public class ObiController : MonoBehaviour
{
[SerializeField] private GameObject[] target;
private int index = -1;
/// <summary>
/// 合并
/// </summary>
public void MergeObject()
{
if (play)
{
play = false;
}
else
{
play = true;
}
if (j == 0)
{
j = 1;
}
if (j == 10)
{
j = 20;
}
}
/// <summary>
/// 拆分
/// </summary>
public void SplitObject()
{
if (play)
{
play = false;
Debug.Log(play);
}
else
{
play = true;
Debug.Log(play);
}
if (j == 0)
{
j = 1;
}
if (j == 10)
{
j = 20;
}
}
float j = 0;
float ii = 0.5f;
public static bool play;
private void Update()
{
ii -= Time.deltaTime;
if (ii <= 0 && play)
{
if (j == 1)
{
if (index < target.Length - 1)
{
index++;
}
else
{
j = 10;
play = false;
}
if (index > -1 && index < target.Length)
{
Debug.Log(index);
target[index].GetComponent<ObiTransform>().Open();
}
}
if (j == 20)
{
if (index > -1 && index < target.Length)
{
target[index].GetComponent<ObiTransform>().Close();
}
if (index > -1)
{
index--;
}
else
{
j = 0;
play = false;
}
}
ii = 0.35f;
}
}
}