代码中需要使用DoTween插件
using DG.Tweening;
using System.Collections.Generic;
using UnityEngine;
public class SplitTest : MonoBehaviour
{
public Transform m_ParObj;//根节点
public Vector3 center = Vector3.zero;//根节点的中心位置
private void Start()
{
Transform[] children = gameObject.GetComponentsInChildren<Transform>();
foreach (Transform child in children)
{
center += child.position;
}
center /= children.Length;
print("Center" + center);
Renderer[] renders = m_ParObj.GetComponentsInChildren<Renderer>();//获取所有子对象
for (int i = 0; i < renders.Length; i++)
{
Vector3 tempV3 = SplitObjTest(center, renders[i].bounds.center);
renders[i].transform.DOMove(tempV3, 1f, false);
}
}
public Vector3 SplitObjTest(Vector3 conter, Vector3 _TargetObj)
{
Vector3 tempV3;
tempV3.x = (_TargetObj.x - conter.x) * 1.05f;
tempV3.y = (_TargetObj.y - conter.y) * 1.05f;
tempV3.z = (_TargetObj.z - conter.z) * 1.05f;
return tempV3;
}
}