shaoxiadiablo/Assets/AGame/Scripts/Game/Effect/Ef_uv_loop_simple.cs
2025-05-18 01:04:31 +08:00

102 lines
2.1 KiB
C#

// ***********************************************************************
// Assembly : Game
// Author : Kimch
// Created : 2020-09-02
// Description :
// Last Modified By :
// Last Modified On :
// ***********************************************************************
// <copyright file= "Ef_uv_loop_simple" company="Kimch"></copyright>
// <summary></summary>
// ***********************************************************************
using UnityEngine;
namespace G
{
public class Ef_uv_loop_simple : MonoBehaviour
{
public int uvAnimationTileX = 4;
public int uvAnimationTileY = 4;
public int framesPerSecond = 20;
public bool loop;
public bool instance_material;
public bool timetune;
private int _index;
private int _lastIndex = -1;
private float _startTime;
private int _lastFrame;
private Vector2 _size;
private Renderer _renderer;
private Material _material;
private void Awake()
{
_renderer = this.GetComponent<Renderer>();
if (!_renderer)
_renderer = this.GetComponentInChildren<Renderer>();
}
private void Start()
{
_startTime = 0f;
_lastFrame = uvAnimationTileX * uvAnimationTileY;
_size = new Vector2(1f / uvAnimationTileX, 1f / uvAnimationTileY);
#if UNITY_EDITOR
_material = _renderer.material;
#else
_material = instance_material ? _renderer.material : _renderer.sharedMaterial;
#endif
}
private void Update()
{
if (!_renderer.enabled)
{
return;
}
if (!timetune)
{
_startTime += Time.deltaTime;
}
else
{
_startTime += Time.unscaledDeltaTime;
}
_index = (int)(_startTime * framesPerSecond);
if (loop)
{
_index %= _lastFrame;
}
if (_index != _lastIndex)
{
if (_index >= _lastFrame)
{
_startTime = 0f;
_lastIndex = -1;
this.gameObject.SetActive(false);
}
int _uIndex = _index % uvAnimationTileX;
int _vIndex = _index / uvAnimationTileX;
var _offset = Vector2.right * _uIndex * _size.x + Vector2.up * (1f - _size.y - _vIndex * _size.y);
_material.mainTextureOffset = _offset;
_material.mainTextureScale = _size;
_lastIndex = _index;
}
}
}
}