?物体拆分和组合
/// <summary> 此原有距离就是未拆分时子物体位置到旋转物体父节点位置的相对位置 </summary>
[Header("设置x轴、y轴、z轴需要放大到原有距离的多少倍")]
[SerializeField]
private float xMul, yMul, zMul;
[Header("拆分中心点")]
[SerializeField]
private Transform m_CenterPoint;
[Header("绑定UI")]
[SerializeField]
private Button splitBtn;
private bool isSplit = false;
[Header("拆分物体父节点")]
[SerializeField]
private Transform m_ParObj;//父节点
private List<Transform> m_Childs = new List<Transform>();//收集全部子节点全部子对象
private List<Vector3> m_InitPoint = new List<Vector3>();//记录下所有子物体的初始位置
private void Awake()
{
for (int i = 0; i < m_ParObj.childCount; i++)
{
m_Childs.Add(m_ParObj.GetChild(i));
}
for (int i = 0; i < m_Childs.Count; i++)
{
m_InitPoint.Add(m_Childs[i].position);
}
splitBtn.onClick.AddListener(SplitBtn);
}
/// <summary> 按钮运行 </summary>
private void SplitBtn()
{
if (!isSplit)
{
SplitObject();
splitBtn.transform.GetChild(0).GetComponent<Text>().text = "组合";
}
else
{
CombineObject();
splitBtn.transform.GetChild(0).GetComponent<Text>().text = "拆分";
}
isSplit = !isSplit;
}
/// <summary> 进行拆分 </summary>
private void SplitObject()
{
for (int i = 0; i < m_Childs.Count; i++)
{
m_Childs[i].transform.DOMove(SplitObjectPos(m_CenterPoint, m_Childs[i].transform), 0.2f, false);
}
}
/// <summary> 进行组合 </summary>
private void CombineObject()
{
for (int i = 0; i < m_InitPoint.Count; i++)
{
m_Childs[i].transform.DOMove(m_InitPoint[i], 0.2f, false);
}
}
/// <summary> 将子物体移动到新位置 </summary>
public Vector3 SplitObjectPos(Transform centerPoint, Transform child)
{
Vector3 temp;
temp.x = (child.position.x - centerPoint.position.x) * xMul + centerPoint.position.x;
temp.y = (child.position.y - centerPoint.position.y) * yMul + centerPoint.position.y;
temp.z = (child.position.z - centerPoint.position.z) * zMul + centerPoint.position.z;
return temp;
}
绑定相机进行绕物体旋转
[Header("获得旋转目标")]
[SerializeField]
private Transform target;//获取旋转目标
[Header("绑定相机")]
[SerializeField]
private Camera camera1;//绑定相机
public float maxField;//控制相机FieldOfView的最大角度
public float minField;//控制相机FieldOfView的最小角度
private void Start()
{
//初始化相机面向物体
camera1.transform.forward = (target.position - camera1.transform.position).normalized;
}
private void Update()
{
CameraRotate();
CameraZoom();
}
/// <summary> 摄像机围绕目标旋转操作 </summary>
private void CameraRotate()
{
var mouse_x = Input.GetAxis("Mouse X");//获取鼠标X轴移动
var mouse_y = -Input.GetAxis("Mouse Y");//获取鼠标Y轴移动
if (Input.GetKey(KeyCode.Mouse0))
{
transform.RotateAround(target.transform.position, Vector3.up, mouse_x * 5);
transform.RotateAround(target.transform.position, transform.right, mouse_y * 5);
}
}
/// <summary> 摄像机滚轮缩放(通过修改相机Field Of View的来进行缩放) </summary>
private void CameraZoom()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0 && camera1.fieldOfView > minField)
{
camera1.fieldOfView -= Input.GetAxis("Mouse ScrollWheel") * 5f;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 && camera1.fieldOfView < maxField)
{
camera1.fieldOfView -= Input.GetAxis("Mouse ScrollWheel") * 5f;
}
}
转动:?
缩放:
主要API
Transform.RotateAround
public void RotateAround (Vector3 point, Vector3 axis, float angle);
描述:
将变换围绕穿过世界坐标中的?point ?的?axis ?旋转?angle ?度。
这会修改变换的位置和旋转。
Camera.fieldOfView
public float fieldOfView ;
描述:
摄像机的视野(以度为单位)。
Unity API链接:
Transform-RotateAround - Unity 脚本 API
Camera-fieldOfView - Unity 脚本 API
|