158 lines
2.7 KiB
C#
158 lines
2.7 KiB
C#
// ***********************************************************************
|
|
// Assembly : Unity
|
|
// Author : Kimch
|
|
// Created :
|
|
//
|
|
// Last Modified By : Kimch
|
|
// Last Modified On :
|
|
// ***********************************************************************
|
|
// <copyright file= "KUIImage" company=""></copyright>
|
|
// <summary></summary>
|
|
// ***********************************************************************
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
[AddComponentMenu("UGUICustom/KUIImage")]
|
|
[RequireComponent(typeof(Image))]
|
|
public class KUIImage : MonoBehaviour
|
|
{
|
|
#region Field
|
|
|
|
public int framesPerSecond;
|
|
|
|
public bool loop;
|
|
|
|
public Sprite[] sprites;
|
|
|
|
public bool usePolygon;
|
|
|
|
private int _loopIndex;
|
|
private float _loopTime;
|
|
|
|
private Image _image;
|
|
|
|
#endregion
|
|
|
|
#region Property
|
|
|
|
public Sprite sprite
|
|
{
|
|
get { return _image.overrideSprite ?? _image.sprite; }
|
|
set { _image.overrideSprite = value; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Method
|
|
|
|
public void SetDefaultSprite(int index)
|
|
{
|
|
if (!_image)
|
|
{
|
|
_image = GetComponent<Image>();
|
|
}
|
|
|
|
if (sprites != null && index >= 0 && index < sprites.Length)
|
|
{
|
|
_image.sprite = sprites[index];
|
|
}
|
|
}
|
|
|
|
public void ShowSprite(int index)
|
|
{
|
|
if (!_image)
|
|
{
|
|
_image = GetComponent<Image>();
|
|
}
|
|
|
|
if (sprites != null && index >= 0 && index < sprites.Length)
|
|
{
|
|
_image.overrideSprite = sprites[index];
|
|
}
|
|
else
|
|
{
|
|
_image.overrideSprite = null;
|
|
}
|
|
}
|
|
|
|
public void ShowSprite(string name)
|
|
{
|
|
if (!_image)
|
|
{
|
|
_image = GetComponent<Image>();
|
|
}
|
|
|
|
if (sprites != null)
|
|
{
|
|
foreach (var sprite in sprites)
|
|
{
|
|
if (sprite && name == sprite.name)
|
|
_image.overrideSprite = sprite;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_image.overrideSprite = null;
|
|
}
|
|
}
|
|
|
|
public void ShowRandom()
|
|
{
|
|
if (sprites != null)
|
|
{
|
|
_image.overrideSprite = sprites[Random.Range(0, sprites.Length)];
|
|
}
|
|
}
|
|
|
|
public void ShowGray(bool gray)
|
|
{
|
|
if (!_image)
|
|
{
|
|
_image = GetComponent<Image>();
|
|
}
|
|
_image.material = gray ? Resources.Load<Material>("Materials/UIGray") : null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 剪影
|
|
/// </summary>
|
|
/// <param name="sketch"></param>
|
|
public void ShowSketch(bool sketch)
|
|
{
|
|
if (!_image)
|
|
{
|
|
_image = GetComponent<Image>();
|
|
}
|
|
_image.material = sketch ? Resources.Load<Material>("Materials/UISketch") : null;
|
|
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
_image = GetComponent<Image>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (sprites != null && framesPerSecond > 0)
|
|
{
|
|
var per = 1f / framesPerSecond;
|
|
_loopTime += Time.deltaTime;
|
|
if (_loopTime > per)
|
|
{
|
|
_loopTime -= per;
|
|
_loopIndex++;
|
|
if (loop || _loopIndex < sprites.Length)
|
|
{
|
|
_loopIndex %= sprites.Length;
|
|
_image.overrideSprite = sprites[_loopIndex];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|