Files
2026-02-21 16:45:37 +08:00

161 lines
3.5 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class Waver : MonoBehaviour
{
public List<GameObject> gameObjects = new List<GameObject>();
public bool isActive = true;
public float Amplitude;
public float Duration;
public float Randomize;
public float PhaseShift;
public float Delay;
public int repeat;
public bool bounce = true;
public bool bothDirections;
public iTween.LoopType loopType = iTween.LoopType.pingPong;
public iTween.EaseType easeType = iTween.EaseType.easeInOutQuad;
public bool RotationX;
public bool RotationY;
public bool RotationZ;
public bool PosX;
public bool PosY;
public bool ScaleX;
public bool ScaleY;
public bool Alpha;
private int currentRepetition;
private void Awake()
{
if (gameObjects.Count == 0)
{
gameObjects.Add(base.gameObject);
}
if (bounce)
{
repeat *= 2;
}
if (isActive)
{
StartWaver();
}
}
private void Update()
{
if (!isActive)
{
}
}
public void StartWaver()
{
GameObject target = gameObjects[0];
float amplitude = Amplitude;
float min = Amplitude - Amplitude * Randomize;
amplitude = Random.Range(min, Amplitude);
if (RotationX)
{
iTween.RotateBy(target, iTween.Hash("name", "Rotation", "x", amplitude / 360f, "time", Duration, "easeType", easeType, "loopType", loopType, "delay", Delay, "oncomplete", "CallbackOnComplete"));
}
if (RotationY)
{
iTween.RotateBy(target, iTween.Hash("name", "Rotation", "y", amplitude / 360f, "time", Duration, "easeType", easeType, "loopType", loopType, "delay", Delay, "oncomplete", "CallbackOnComplete"));
}
if (RotationZ)
{
iTween.RotateBy(target, iTween.Hash("name", "Rotation", "z", amplitude / 360f, "time", Duration, "easeType", easeType, "loopType", loopType, "delay", Delay, "oncomplete", "CallbackOnComplete"));
}
if (ScaleX)
{
iTween.ScaleBy(target, iTween.Hash("name", "ScaleX", "x", amplitude, "time", Duration, "easeType", easeType, "loopType", loopType, "delay", Delay, "oncomplete", "CallbackOnComplete"));
}
if (ScaleY)
{
iTween.ScaleBy(target, iTween.Hash("name", "ScaleY", "y", amplitude, "time", Duration, "easeType", easeType, "loopType", loopType, "delay", Delay, "oncomplete", "CallbackOnComplete"));
}
if (PosX)
{
iTween.MoveBy(target, iTween.Hash("name", "PosX", "x", amplitude, "time", Duration, "easeType", easeType, "loopType", loopType, "delay", Delay, "oncomplete", "CallbackOnComplete"));
}
if (PosY)
{
iTween.MoveBy(target, iTween.Hash("name", "PosY", "y", amplitude, "time", Duration, "easeType", easeType, "loopType", loopType, "delay", Delay, "oncomplete", "CallbackOnComplete"));
}
if (Alpha)
{
iTween.FadeTo(target, iTween.Hash("name", "Alpha", "alpha", amplitude, "time", Duration, "easeType", easeType, "loopType", loopType, "delay", Delay, "oncomplete", "CallbackOnComplete"));
}
}
public void StopWaver()
{
GameObject target = gameObjects[0];
iTween.Stop(target);
currentRepetition = 0;
}
public void RestartWaver()
{
}
public void PauseWaver(bool pause)
{
GameObject target = gameObjects[0];
if (pause)
{
iTween.Pause(target);
}
else
{
iTween.Resume(target);
}
}
public void CallbackOnComplete()
{
if (repeat > 0)
{
currentRepetition++;
if (currentRepetition >= repeat)
{
StopWaver();
}
return;
}
currentRepetition++;
if (bothDirections && currentRepetition % 2 == 0)
{
Amplitude *= -1f;
StopWaver();
StartWaver();
}
if (Randomize > 0f && currentRepetition % 2 == 0)
{
Amplitude *= -1f;
StopWaver();
StartWaver();
}
}
}