105 lines
2.2 KiB
C#
Raw Normal View History

2025-05-18 01:04:31 +08:00
// ***********************************************************************
// Assembly : Unity
// Author : Kimch
// Created : 2019-06-17
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "Ef_uv_loop" company="Kunpo"></copyright>
// <summary></summary>
// ***********************************************************************
using UnityEngine;
namespace G
{
/// <summary>
/// uv 动画
/// </summary>
public class Ef_uv_loop : MonoBehaviour
{
public int uvAnimationTileX = 4;
public int uvAnimationTileY = 4;
public int framesPerSecond = 20;
public int impact = 1;
public bool loop;
private int _index;
private int _lastIndex = -1;
private float _startTime;
private int _lastFrame;
private Vector2 _size;
private Renderer _myrenderer;
private Collider _mycollider;
MaterialPropertyBlock _propertyBlock;
private void Awake()
{
_propertyBlock = new MaterialPropertyBlock();
_myrenderer = this.GetComponentInChildren<Renderer>();
_mycollider = this.GetComponent<Collider>();
if (_mycollider)
{
_mycollider.isTrigger = true;
_mycollider.enabled = false;
}
}
private void Start()
{
_startTime = 0f;
_lastFrame = uvAnimationTileX * uvAnimationTileY;
_size = new Vector2(1f / uvAnimationTileX, 1f / uvAnimationTileY);
}
private void Update()
{
if (!_myrenderer.enabled)
{
return;
}
_startTime += Time.deltaTime;
_index = (int)(_startTime * framesPerSecond);
if (loop)
{
_index %= _lastFrame;
}
if (_index != _lastIndex)
{
if (_index >= _lastFrame)
{
_startTime = 0f;
_lastIndex = -1;
_mycollider.enabled = false;
this.gameObject.SetActive(false);
}
else if (_index >= impact)
{
_mycollider.enabled = true;
}
int uIndex = _index % uvAnimationTileX;
int vIndex = _index / uvAnimationTileX;
_propertyBlock.SetVector("_MainTex_ST", new Vector4(_size.x, _size.y, uIndex * _size.x, 1f - _size.y - vIndex * _size.y));
_myrenderer.SetPropertyBlock(_propertyBlock);
_lastIndex = _index;
}
}
}
}