//********************************************************// // // // Copyright © All rights reserved. MyNameIsVoo. 2020. // // // // COPYING FORBIDEN // // // //********************************************************// using UnityEngine; using UnityEngine.UI; namespace ICWM { namespace Loot { public class SearchLootIcon : MonoBehaviour { [Header("ATTRIBUTES")] [SerializeField] private int IDSeach = 0; [SerializeField] private Material m_Material; [Header("textures ATTRIBUTES")] [SerializeField] private int colCount = 4; [SerializeField] private int rowCount = 4; [Header("animation ATTRIBUTES")] [SerializeField] private int rowNumber = 0; //Zero Indexed [SerializeField] private int colNumber = 0; //Zero Indexed [SerializeField] private int totalCells = 4; [SerializeField] private float fps = 10; private float time = 0f; private void Start() { time = 0f; GetComponent().material = Instantiate(m_Material); GetComponent().material.name = IDSeach.ToString(); } private void Update() { SetSpriteAnimation(colCount, rowCount, rowNumber, colNumber, totalCells, fps); } #region PUBLIC public void ResetMaterial() { time = 0f; // Size of every cell float sizeX = 1.0f / colCount; float sizeY = 1.0f / rowCount; Vector2 size = new Vector2(sizeX, sizeY); // split into horizontal and vertical index var uIndex = 0 % colCount; var vIndex = 0 / colCount; // build offset // v coordinate is the bottom of the image in opengl so we need to invert. float offsetX = (uIndex + colNumber) * size.x; float offsetY = (1.0f - size.y) - (vIndex + rowNumber) * size.y; Vector2 offset = new Vector2(offsetX, offsetY); GetComponent().material.SetTextureOffset("_MainTex", offset); GetComponent().material.SetTextureScale("_MainTex", size); } #endregion #region PRIVATE private void SetSpriteAnimation(int colCount, int rowCount, int rowNumber, int colNumber, int totalCells, float fps) { // Calculate index int index = 0; if (IDSeach == 1) // Persent { if (LootBase.instance.GetDeltaTimeLootSearching() >= fps) return; index = (int)(LootBase.instance.GetDeltaTimeLootSearching() * fps); } else { index = (int)(time * fps); time += Time.deltaTime; } // Repeat when exhausting all cells index = index % totalCells; // Size of every cell float sizeX = 1.0f / colCount; float sizeY = 1.0f / rowCount; Vector2 size = new Vector2(sizeX, sizeY); // split into horizontal and vertical index var uIndex = index % colCount; var vIndex = index / colCount; // build offset // v coordinate is the bottom of the image in opengl so we need to invert. float offsetX = (uIndex + colNumber) * size.x; float offsetY = (1.0f - size.y) - (vIndex + rowNumber) * size.y; Vector2 offset = new Vector2(offsetX, offsetY); GetComponent().material.SetTextureOffset("_MainTex", offset); GetComponent().material.SetTextureScale("_MainTex", size); } #endregion } } }