少年修仙传客户端代码仓库
client_Hale
2019-04-15 c79dbe6647429b864d15a21b3baa77ffa8d48768
Utility/VesselExtension.cs
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public static class VesselExtension
{
@@ -20,4 +21,85 @@
        return dictionary == null || dictionary.Count == 0;
    }
    public static T GetFirst<T>(this List<T> list)
    {
        if (list == null)
        {
            throw new ArgumentNullException("List is null.");
        }
        if (list.Count == 0)
        {
            Debug.Log("List count can't be zero.");
            return default(T);
        }
        return list[0];
    }
    public static T GetLast<T>(this List<T> list)
    {
        if (list == null)
        {
            throw new ArgumentNullException("List is null.");
        }
        if (list.Count == 0)
        {
            Debug.Log("List count can't be zero.");
            return default(T);
        }
        return list[list.Count - 1];
    }
    public static T GetFirst<T>(this T[] list)
    {
        if (list == null)
        {
            throw new ArgumentNullException("Array is null.");
        }
        if (list.Length == 0)
        {
            Debug.Log("Array count can't be zero.");
            return default(T);
        }
        return list[0];
    }
    public static T GetLast<T>(this T[] list)
    {
        if (list == null)
        {
            throw new ArgumentNullException("Array is null.");
        }
        if (list.Length == 0)
        {
            Debug.Log("Array count can't be zero.");
            return default(T);
        }
        return list[list.Length - 1];
    }
    public static T GetRandom<T>(this List<T> list)
    {
        if (list == null)
        {
            throw new ArgumentNullException("List is null.");
        }
        if (list.Count == 0)
        {
            Debug.Log("List count can't be zero.");
            return default(T);
        }
        var randomIndex = UnityEngine.Random.Range(0, list.Count);
        return list[randomIndex];
    }
}