JJBB/Assets/Project/Script/ObjectTween.cs

333 lines
10 KiB
C#
Raw Normal View History

2024-08-23 15:49:34 +08:00
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ObjectTween : MonoBehaviour {
private delegate void OperationFun();
public enum Operation
{
None,
rotation,
ChangeSize,
FowardCamera,
RectMoveTo,
MoveTo,
ImageAphla,
ControlOtherMove,
SizeW,
WorldMoveTo,
Shock,
ShockRate,
}
public AnimationCurve[] _AnimationCurves;
public float durtion = 0;
public float DelayTime = 0;
private float startTime = -1;
private Vector3 OldPosition = Vector3.zero;
private Vector3 OldEulerAngles = Vector3.zero;
public Operation[] m_operas;
public float m_speedX = 1.0f;
public float m_speedY = 1.0f;
public float m_speedZ = 1.0f;
private Vector3 startPos = Vector3.zero;
public Vector3 destantPos = Vector3.zero;
public bool isDestroy = true;
public bool ReSetOnDisable = false;
public int Axis; //1X轴 2Y轴 4Z轴
private int axisX = 1;
private int axisY = 2;
private int axisZ = 4;
private float StartAphla = -1;
public float ImageEndAphla = 0;
public Image m_Image;
private RectTransform m_rect;
public float StartW;
public float EndW;
private bool m_IsOver = false;
private void OnDisable()
{
if (ReSetOnDisable)
Reset();
}
public void Reset()
{
startPos = Vector3.zero;
startTime = -1;
StartAphla = -1;
m_rect = null;
m_IsOver = false;
}
private OperationFun m_operaFun = null;
// Use this for initialization
void DoAction () {
for(int i=0;i<m_operas.Length;i++)
{
switch (m_operas[i])
{
case Operation.rotation:
{
axisX = axisX & Axis;
axisY = axisY & Axis;
axisZ = axisZ & Axis;
RationSelf();
break;
}
case Operation.ChangeSize:
{
ChangeSizeSelf();
break;
}
case Operation.FowardCamera:
{
FowardCamera();
break;
}
case Operation.RectMoveTo:
{
if (startPos == Vector3.zero)
{
startPos = transform.localPosition;
}
RectMoveTo();
break;
}
case Operation.MoveTo:
{
if(startPos == Vector3.zero)
{
startPos = transform.localPosition;
}
MoveTo();
break;
}
case Operation.WorldMoveTo:
{
if (startPos == Vector3.zero)
{
startPos = transform.position;
}
WorldMoveTo();
break;
}
case Operation.ImageAphla:
{
if (StartAphla == -1)
{
StartAphla = m_Image.color.a;
}
ImageColorTween();
break;
}
case Operation.SizeW:
{
if (startTime == 0 || m_rect == null)
{
startTime = Time.unscaledTime;
m_rect = gameObject.GetComponent<RectTransform>();
}
SizeWFresh();
break;
}
case Operation.Shock:
{
if (startTime == 0)
{
startTime = Time.unscaledTime + DelayTime;
}
if(OldPosition == Vector3.zero)
OldPosition = transform.localPosition;
float timeRate = Time.unscaledTime - startTime;
if(timeRate>0)
{
float rate = timeRate;
float x = _AnimationCurves[0].Evaluate(rate);
float y = _AnimationCurves[1].Evaluate(rate);
float z = _AnimationCurves[2].Evaluate(rate);
transform.localPosition = OldPosition + new Vector3(x, y, z);
}
break;
}
case Operation.ShockRate:
{
if (startTime == 0)
{
startTime = Time.unscaledTime + DelayTime;
}
if (OldEulerAngles == Vector3.zero)
OldEulerAngles = transform.localEulerAngles;
float timeRate = Time.unscaledTime - startTime;
if (timeRate > 0)
{
float rate = timeRate;
float x = _AnimationCurves[0].Evaluate(rate);
float y = _AnimationCurves[1].Evaluate(rate);
float z = _AnimationCurves[2].Evaluate(rate);
transform.localEulerAngles = OldEulerAngles + new Vector3(x, y, z);
}
break;
}
}
}
}
// Update is called once per frame
void Update () {
if (startTime > 0 && durtion > 0 && Time.unscaledTime - startTime >= durtion)
{
if (m_IsOver == false)
{
DoAction();
m_IsOver = true;
return;
}
m_IsOver = false;
if (isDestroy)
{
gameObject.SetActive(false);
GameObject.Destroy(gameObject);
}
if (OldPosition != Vector3.zero)
transform.localPosition = OldPosition;
if (OldEulerAngles != Vector3.zero)
transform.localEulerAngles = OldEulerAngles;
OldPosition = Vector3.zero;
return;
}
if(startTime <= 0 && durtion > 0)
{
startTime = Time.unscaledTime + DelayTime;
}
if (Time.unscaledTime - startTime < 0)
return;
DoAction();
}
void SizeWFresh()
{
if(m_rect!=null)
{
Vector2 sizeDelta = m_rect.sizeDelta;
sizeDelta.x = Mathf.Lerp(StartW, EndW, (Time.unscaledTime - startTime) / durtion);
m_rect.sizeDelta = sizeDelta;
}
}
void RationSelf()
{
transform.Rotate(new Vector3(axisX*m_speedX,axisY*m_speedY, axisZ*m_speedZ));
}
void ChangeSizeSelf()
{
Projector pro = GetComponentInChildren<Projector>();
if (pro != null)
pro.orthographicSize = (transform.localScale.x + transform.localScale.y + transform.localScale.z) / 3;
}
void FowardCamera()
{
if(Camera.main!=null)
{
Vector3 lookAt = new Vector3(Camera.main.transform.position.x, transform.position.y, Camera.main.transform.position.z);
transform.LookAt(lookAt);
}
}
void RectMoveTo()
{
RectTransform rect = transform as RectTransform;
if(_AnimationCurves.Length>=3)
{
float rate = (Time.unscaledTime - startTime) / durtion;
float x = _AnimationCurves[0].Evaluate(rate);
float y = _AnimationCurves[1].Evaluate(rate);
float z = _AnimationCurves[2].Evaluate(rate);
rect.anchoredPosition = new Vector3(x, y, z);
}
else
{
if (rect != null)
{
rect.anchoredPosition = Vector2.Lerp(rect.anchoredPosition, destantPos, (Time.unscaledTime - startTime) / durtion);
}
}
}
void MoveTo()
{
if (_AnimationCurves.Length >= 3)
{
float rate = (Time.unscaledTime - startTime) / durtion;
float x = _AnimationCurves[0].Evaluate(rate);
float y = _AnimationCurves[1].Evaluate(rate);
float z = _AnimationCurves[2].Evaluate(rate);
transform.localPosition = new Vector3(x, y, z);
}
else
{
transform.localPosition = Vector3.Lerp(transform.localPosition, destantPos, (Time.unscaledTime - startTime) / durtion);
}
}
void WorldMoveTo()
{
if (_AnimationCurves.Length >= 3)
{
float rate = (Time.unscaledTime - startTime) / durtion;
float x = _AnimationCurves[0].Evaluate(rate);
float y = _AnimationCurves[1].Evaluate(rate);
float z = _AnimationCurves[2].Evaluate(rate);
transform.position = new Vector3(x, y, z);
}
else
{
transform.position = Vector3.Lerp(transform.position, destantPos, (Time.unscaledTime - startTime) / durtion);
}
}
void ImageColorTween()
{
if (m_Image == null)
return;
if (_AnimationCurves.Length >= 1)
{
float rate = (Time.unscaledTime - startTime) / durtion;
float x = _AnimationCurves[0].Evaluate(rate);
float y = _AnimationCurves[1].Evaluate(rate);
float z = _AnimationCurves[2].Evaluate(rate);
transform.position = new Vector3(x, y, z);
}
else if(StartAphla != -1)
{
Color newColor = m_Image.color;
newColor.a = Mathf.Lerp(StartAphla, ImageEndAphla, (Time.unscaledTime - startTime) / durtion);
m_Image.color = newColor;
}
}
}