lgw2003
3/4/2020 - 1:39 AM

Unity 常用脚本

单例 计时器 获取IP 延迟调用 附件镜头震动

//实现单例
#region make singletone
  public static Singletone Instance { get; private set; }
  void Awake()
  {
    if (Instance == null)
      Instance = this;
    else if (Instance != this)
      Destroy(gameObject);
  }
  #endregion
public static string GetIP()
{
  string ip4 = "";
  //Dns.GetHostName()获取本机名Dns.GetHostAddresses()根据本机名获取ip地址组
  IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
  foreach (IPAddress ip in ips)
  {
    if (ip.AddressFamily == AddressFamily.InterNetwork)
    {
      ip4 = ip.ToString();  //ipv4
    }
  }
  return ip4;
}
using System;

public class Timer
{
    public float Duration;
    public float LeftTime;
    
    private Action _updateAction;
    private Action _callAction;
    private bool _isPause;

    /// <summary>
    /// 计时函数
    /// </summary>
    /// <param name="duration">持续时间</param>
    /// <param name="updateAction">更新函数</param>
    /// <param name="callAction">回调函数</param>
    /// <param name="intiAction">初始函数</param>
    public Timer(float duration, Action updateAction = null, Action callAction = null, Action intiAction = null)
    {
        LeftTime = duration;
        Duration = duration;
        if (intiAction != null) intiAction.Invoke();
        _updateAction = updateAction;
        _callAction = callAction;
        _isPause = false;
    }

    /// <summary>
    /// 计时更新函数
    /// </summary>
    /// <param name="deltaTime"></param>
    public void OnUpdate(float deltaTime)
    {
        LeftTime -= deltaTime;
        if (LeftTime <= 0)
        {
            if (_callAction != null)
                _callAction.Invoke();
        }
        else
        {
            if (_updateAction != null && !_isPause)
                _updateAction.Invoke();
        }
    }
    
    public void SetTimerTrick(bool b)
    {
        _isPause = b;
    }
}
using System.Collections.Generic;
using UnityEngine;

public class TimerManager : MonoBehaviour
{
    public static TimerManager Instance;
    
    private List<Timer> _timers;
    private Dictionary<string, Timer> _timerDict;
    
    private void Awake()
    {
        Instance = this;
        _timers = new List<Timer>();
        _timerDict = new Dictionary<string, Timer>();
    }
    
    private void Update()
    {
        for (int i = 0; i < _timers.Count; i++)
        {
            _timers[i].OnUpdate(Time.deltaTime);
        }
    }
    
    /// <summary>
    /// 增加计时器,同名计时器可以设置增量时间模式或重置时间模式
    /// </summary>
    /// <param name="str">计时器名字</param>
    /// <param name="timer">计时器对象</param>
    /// <param name="IsIncrement">是否增量模式 True时间增量模式 False为时间重置模式</param>
    public void AddTimer(string str, Timer timer,bool IsIncrement)
    {
        if (HasTimer(str))
        {
            if (IsIncrement)
            {
                _timerDict[str].LeftTime += _timerDict[str].Duration;
            }
            else
            {
                _timerDict[str].LeftTime = _timerDict[str].Duration;
            }
        }
        else
        {
            _timerDict.Add(str, timer);
            _timers.Add(timer);
        }
    }

    public bool HasTimer(string str)
    {
        if (_timerDict.ContainsKey(str))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public void RemoveTimer(string str)
    {
        var timer = _timerDict[str];
        if (timer != null)
        {
            _timers.Remove(timer);
            _timerDict.Remove(str);
        }
    }
}
using System;
using System.Collections;
using UnityEngine;

//协程类 延迟执行
public class DelayToInvoke : MonoBehaviour
{
    public static IEnumerator DelayToInvokeDo(Action action, float delaySeconds)

    {
        yield return new WaitForSeconds(delaySeconds);

        action();

    }
}

 //协程延迟调用方法
StartCoroutine(DelayToInvoke.DelayToInvokeDo(()=>
{
  Instantiate(prop, position, rotation);
},0.5f));