Unity工具
大家好,我是心疼你的一切,不定时更新Unity开发技巧,觉得有用记得一键三连哦。
提示:以下是本篇文章正文内容,下面案例可供参考
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SplitController : MonoBehaviour
{
private Transform m_ParObj;//中心点
private List<GameObject> m_Child;//所有子对象
private List<Vector3> m_InitPoint = new List<Vector3>();//初始位置
private int m_IsSplitState = 0;
private float m_TimeTotal = 0;//总时间
private float m_Timecurrent = 0;//当前时间
private int m_TimeDisRe = 0;//时间间隔 记录
private int IndexCount = 0;//计数
public int m_TimeDistance = 1;//时间间隔
void Start()
{
m_ParObj = GetComponent<Transform>();
m_Child = GetChild(m_ParObj);
for (int i = 0; i < m_Child.Count; i++)
{
m_InitPoint.Add(m_Child[i].transform.position);
}
m_TimeTotal = m_Child.Count * m_TimeDistance;
}
//获取所有子对象
public List<GameObject> GetChild(Transform obj)
{
List<GameObject> tempArrayobj = new List<GameObject>();
foreach (Transform child in obj)
{
tempArrayobj.Add(child.gameObject);
}
return tempArrayobj;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.M))
{
m_Timecurrent = 0;
m_TimeDisRe = 0;
IndexCount = 0;
m_IsSplitState = 1;
}
if (Input.GetKeyDown(KeyCode.N))
{
m_Timecurrent = 0;
m_TimeDisRe = 0;
IndexCount = 0;
m_IsSplitState = 2;
}
if (m_IsSplitState == 1)
{
//拆分
SplitObject();
}
else if (m_IsSplitState == 2)
{
//合并
MergeObject();
}
}
private void SplitObject()
{
m_Timecurrent += Time.deltaTime;
if (m_Timecurrent <= m_TimeTotal && IndexCount < m_Child.Count)
{
if (Mathf.FloorToInt(m_Timecurrent) == m_TimeDisRe)
{
m_TimeDisRe += m_TimeDistance;
Vector3 tempV3 = SplitObjTest(m_ParObj.transform, m_Child[IndexCount].transform);
m_Child[IndexCount].transform.DOMove(tempV3, 1f, false);
IndexCount++;
}
}
}
private void MergeObject()
{
m_Timecurrent += Time.deltaTime;
if (m_Timecurrent <= m_TimeTotal && IndexCount < m_Child.Count)
{
if (Mathf.FloorToInt(m_Timecurrent) == m_TimeDisRe)
{
m_TimeDisRe += m_TimeDistance;
m_Child[IndexCount].transform.DOMove(m_InitPoint[IndexCount], 3f, false);
IndexCount++;
}
}
}
public Vector3 SplitObjTest(Transform m_ParObj, Transform _TargetObj)
{
Vector3 tempV3;
tempV3.x = (_TargetObj.position.x - m_ParObj.position.x) * 2;
tempV3.y = (_TargetObj.position.y - m_ParObj.position.y) * 2;
tempV3.z = (_TargetObj.position.z - m_ParObj.position.z) * 2;
return tempV3;
}
}
Unity模型拆分(一个一个的拆分)
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SplitSymmetry : MonoBehaviour
{
private Transform m_ParObj;//中心点
private List<GameObject> m_Child;//所有子对象
private List<Vector3> m_InitPoint = new List<Vector3>();//初始位置
private void Start()
{
m_ParObj = transform;
m_Child = GetChild(m_ParObj);//获取所有子对象
for (int i = 0; i < m_Child.Count; i++)
{
m_InitPoint.Add(m_Child[i].transform.position);
}
}
//获取所有子对象
public List<GameObject> GetChild(Transform obj)
{
List<GameObject> tempArrayobj = new List<GameObject>();
foreach (Transform child in obj)
{
tempArrayobj.Add(child.gameObject);
}
return tempArrayobj;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.M))
{
//拆分
SplitObject();
}
if (Input.GetKeyDown(KeyCode.N))
{
//合并
MergeObject();
}
}
private void SplitObject()
{
for (int i = 0; i < m_Child.Count; i++)
{
Vector3 tempV3 = SplitObjTest(m_ParObj, m_Child[i].transform);
m_Child[i].transform.DOMove(tempV3, 3f, false);
}
}
private void MergeObject()
{
for (int i = 0; i < m_InitPoint.Count; i++)
{
m_Child[i].transform.DOMove(m_InitPoint[i], 3f, false);
}
}
public Vector3 SplitObjTest(Transform m_ParObj, Transform _TargetObj)
{
Vector3 tempV3;
tempV3.x = (_TargetObj.position.x - m_ParObj.position.x) * 2;
tempV3.y = (_TargetObj.position.y - m_ParObj.position.y) * 2;
tempV3.z = (_TargetObj.position.z - m_ParObj.position.z) * 2;
return tempV3;
}
}
Unity模型拆分(一键拆分)
隐藏的模型就是刚刚拼的模型
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SplitAnomaly : MonoBehaviour
{
[Header ("移动之前的物体父节点")]
public Transform m_ChildPointParent;//要移动的子对象的父物体
private List<GameObject> m_Child;//所有子对象
private List<Vector3> m_InitPoint = new List<Vector3>();//初始位置
[Header("移动之后的物体父节点")]
public Transform m_TargetPointParent;//目标点对象的父物体
private List<GameObject> m_TargetChild;//目标点所有子对象
private List<Vector3> m_TargetPoint = new List<Vector3>();//要移动的位置
private void Start()
{
m_Child = GetChild(transform);//获取所有子对象
for (int i = 0; i < m_Child.Count; i++)
{
m_InitPoint.Add(m_Child[i].transform.position);
}
m_TargetChild = GetChild(m_TargetPointParent);//获取所有目标点子对象
for (int i = 0; i < m_TargetChild.Count; i++)
{
m_TargetPoint.Add(m_TargetChild[i].transform.position);
}
}
//获取所有子对象
public List<GameObject> GetChild(Transform obj)
{
List<GameObject> tempArrayobj = new List<GameObject>();
foreach (Transform child in obj)
{
tempArrayobj.Add(child.gameObject);
}
return tempArrayobj;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.M))
{
//拆分
SplitObject();
}
if (Input.GetKeyDown(KeyCode.N))
{
//合并
MergeObject();
}
}
private void SplitObject()
{
for (int i = 0; i < m_Child.Count; i++)
{
m_Child[i].transform.DOMove(m_TargetPoint[i], 3f, false);
}
}
private void MergeObject()
{
for (int i = 0; i < m_InitPoint.Count; i++)
{
m_Child[i].transform.DOMove(m_InitPoint[i], 3f, false);
}
}
}
Unity模型拆分(不规则模型拆分)
遇到规则的模型就用第一种方式,遇到不规则的就用第二种方。
不定时更新Unity开发技巧,觉得有用记得一键三连哦。