Noticias

¡RECUERDA QUE SI ERES UN NUEVO USUARIO, DEBES PRESENTARTE PARA PODER PUBLICAR MENSAJES! | TENEMOS CANAL OFICIAL DE TELEGRAM: t.me/unity3dspain

Timer para C# - Aporte

Iniciado por francoe1, Marzo 04, 2016, 11:38:06 AM

Tema anterior - Siguiente tema
Buenas a todos, espero que anden bien.Como dice el titulo voy a aportar un pequeño componente que he creado hace un tiempo ya, se trata de un Timer, este nos permite gestionar el tiempo en segundo para efectuar acciones, consta de una muy simple implementacion, solo importamos una DLL a nuestro proyecto, luego agregamos en nuestra escena el Controlador del Timer.Con esto ya estaría configurado para utilizar, ahora vamos al script donde queramos agregar un Timer y agregamos la librería CustomTool.¡Script de Ejemplo!
using UnityEngine;
using System.Collections;
using CustomTool;

public class Weapon : MonoBehaviour
{
private TimerItem                _timerReload;

private int                   _cadence = 1;
private float                   _tmpCadence = 0;

private void Start()
{
   _timerReload = Timer.Singletone.QuickTimer (1, false, true);
   ReloadEventSetup ();
}

private void Update()
{
   if(Input.GetKeyDown(KeyCode.R))
   {
      _timerReload.Start ();
   }

   if (Input.GetMouseButton (0) && !_timerReload.IsRuning)
   {
      if (_checkCadence)
      {
         Shot ();
      }
   }
}


private bool _checkCadence{get{ if(_tmpCadence < Time.time){ _tmpCadence = _cadence + Time.time; return true; } else return false; }}

public virtual void Shot()
{
   print ("shoting");
}

public virtual void Reload()
{      
   print ("Finish Reload");
}

private void ReloadEventSetup()
{
   _timerReload.EventStart += () =>
   {
      print ("start reloading");
   };

   _timerReload.EventFinish += () =>
   {
      Reload();
   };

   _timerReload.EventStop += () =>
   {
      
   };
}
}
Como veras se maneja por Eventos, estos son muy descriptivos con sus propios nombre.1- Para crear el Timer utilizamos la siguiente función Timer.Singletone.QuickTimer(TiempoDeEspera,AutoRepeticion,GestionManual);2- Utilizamos la Funcion Start para dar inicio al Contador.3- Si deseamos terminar con el contador antes de que este finalice utilizamos Stop.4- Para limpiar todos los contadores utilizar Timer.Singletone.ClearAll().5- Para detener todos los contadores utilizar Timer.Singletone.StopAll(). Existen algunas cositas mas, esta completamente en Español podes guiarte muy fácilmente. ¡Saludos!
http://unityspain.com/applications/core/interface/file/attachment.php?id=9273">Timer.rar
Link en MEGA por si falla el del Forohttps://mega.nz/#!cslnkShQ!jnJnbSSBtYg7gxeAHvQ00IUxyR0_tbrChxJ-pXZGvz8 

Marzo 15, 2016, 08:51:59 AM #1 Ultima modificación: Marzo 15, 2016, 08:52:13 AM por ftejada
Gracias por el aporte francoe1... En cuanto tenga un hueco le hecharé lo descargaré y le hecharé un ojo. Un saludo

Etiquetas: