最近做项目需要把一个设备进行拆分,写了一个拆分功能,感觉实现的效果不过,特此贴出来和大家分享。
功能大体的思路是以设备中心为原点,原点左边的零件向左移动,原点右边的零件向右移动,原点上边的零件向上移动,原点下边的零件向下移动。
具体实现方法:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class XianWeiJingHandler : MonoBehaviour
{
public Vector3 originPos = Vector3.zero;
public int index = 0;
public bool isEnable = true;
public enum MoveAxis
{
None,
XAxisForward,
XAxisBack,
YAxisUp,
YAxisDown,
ZAxisForward,
ZAxisBack
}
public MoveAxis moveAxis = 0;
private float moveDistance = 0.15f;
//private Highlighter highlighter = null;
private bool isCanInteractive = false;
public string name = null;
public string intro = null;
public string work = null;
/// <summary>
/// 防止enable在start之前执行
/// </summary>
private bool isCanReset = false;
// Start is called before the first frame update
void Start()
{
originPos = transform.position;
isCanReset = true;
//highlighter = transform.GetComponent<Highlighter>();
}
private void OnEnable()
{
if (isCanReset)
{
//有多个设备的时候,拆分一个设备后隐藏,防止这个设备在出现的时候还是拆分状态
ResetSelfPosMethod();
}
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// 拆分零件
/// </summary>
public void ChangeSelfPosMethid()
{
switch (moveAxis)
{
case MoveAxis.None:
break;
case MoveAxis.XAxisForward:
transform.DOMoveX(originPos.x + moveDistance, 1);
break;
case MoveAxis.XAxisBack:
transform.DOMoveX(originPos.x - moveDistance, 1);
break;
case MoveAxis.YAxisUp:
transform.DOMoveY(originPos.y + moveDistance, 1);
break;
case MoveAxis.YAxisDown:
transform.DOMoveY(originPos.y - moveDistance, 1);
break;
case MoveAxis.ZAxisForward:
transform.DOMoveZ(originPos.z + moveDistance, 1);
break;
case MoveAxis.ZAxisBack:
transform.DOMoveZ(originPos.z - moveDistance, 1);
break;
default:
break;
}
//if (highlighter != null)
//{
// //highlighter.FlashingOn();
// isCanInteractive = true;
//}
}
/// <summary>
/// 组装零件
/// </summary>
public void ResetSelfPosMethod()
{
transform.DOMove(originPos, 1);
//if (highlighter != null)
//{
// highlighter.FlashingOff();
// isCanInteractive = false;
// FindObjectOfType<MSceneManager>().ShowMsgViewMethod(0);
//}
}
}