Some convenient methods of unity

Are two arrays compared the same
int[] a,int[]b
Enumerable.SequenceEqual(a,b)

Array to List: List list = new List(arr);
List to array: int[] arr = list.ToArray();
Convert integer num to 16-bit binary array: Convert.ToString(num, 2).PadLeft(16 ,'0');
Reverse the string: string reverseStr = new string(str.Reverse().ToArray());

string.TrimStrart()//Delete the spaces at the beginning of the string
string.TrimEnd()//Delete the spaces at the end of the string
string.Trim()//Delete the spaces at the beginning and end of the string

A variety of characters to split the string
string str="I# is% character, character_string"
char[]splits={'#','%',',','_'};
string[] splitStrs=str .Split(splits);
for(int i=0;i<splitStrs.Length,i++)
{ Debug.Log(splitStrs[i]); }

String interception
str.Substring(2,3); Intercept 3 characters from index 2

//Get the current timestamp
long unixTime = ConvertDateTimep(DateTime.Now);
Debug.Log(unixTime);
//Convert the timestamp to DateTime
Debug.Log(GetTime(unixTime.ToString()).ToString());

    Debug.Log(GetTime(unixTime.ToString()).Year);
    Debug.Log(GetTime(unixTime.ToString()).Month);
    Debug.Log(GetTime(unixTime.ToString()).Day);
    Debug.Log(GetTime(unixTime.ToString()).Hour);
    Debug.Log(GetTime(unixTime.ToString()).Minute);
    Debug.Log(GetTime(unixTime.ToString()).Second);

///
/// Get timestamp (accurate to second)
/// TimeTool.ConvertDateTimep(DateTime.Now)
///
/// Time
public static long ConvertDateTimep(DateTime time)
{ return ((time.ToUniversalTime(). Ticks - 621355968000000000) / 10000000); //Equivalent to: //return ((time.ToUniversalTime().Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0).Ticks) / 10000000) * 1000; } /// /// Obtain timestamp /// /// private long GetTime() { // accurate to milliseconds return new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(); } /// /// Convert timestamp to C# format time /// TimeTool.GetTime(TimeTool.ConvertDateTiemp(DateTime.Now).ToString()) ///

















/// Timestamp
///
public static DateTime GetTime(string timeStamp)
{ if (timeStamp.Length > 10) { timeStamp = timeStamp.Substring(0, 10); } DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime( 1970, 1, 1)); long lTime = long.Parse(timeStamp + “0000000”); TimeSpan toNow = new TimeSpan(lTime); return dateTimeStart.Add(toNow); } private void Update() { if (Input. GetKeyDown(KeyCode.A)) { //Get the current time and print the difference in seconds between the current time and the start time (in seconds) DateTime curtimer = DateTime.Now; Debug.Log(curtimer); int timer = GetSubSeconds(startTime , curtimer); Debug. Log(timer);


















}
}

/// <summary>
/// 获取间隔秒数
/// </summary>
/// <param name="startTimer"></param>
/// <param name="endTimer"></param>
/// <returns></returns>
public int GetSubSeconds(DateTime startTimer, DateTime endTimer)
{
    TimeSpan startSpan = new TimeSpan(startTimer.Ticks);

    TimeSpan nowSpan = new TimeSpan(endTimer.Ticks);

    TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration();

    //返回间隔秒数(不算差的分钟和小时等,仅返回秒与秒之间的差)
    //return subTimer.Seconds;

    //返回相差时长(算上分、时的差值,返回相差的总秒数)
    return (int)subTimer.TotalSeconds;
}

/// <summary>
/// 获取两个时间的相差多少分钟
/// </summary>
/// <param name="startTimer"></param>
/// <param name="endTimer"></param>
/// <returns></returns>
public int GetSubMinutes(DateTime startTimer, DateTime endTimer)
{
    TimeSpan startSpan = new TimeSpan(startTimer.Ticks);

    TimeSpan nowSpan = new TimeSpan(endTimer.Ticks);

    TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration();

    //返回相差时长(仅返回相差的分钟数)
    //return subTimer.Minutes;
    //返回相差时长(仅返回相差的总分钟数)
    return (int)subTimer.TotalMinutes;
}


/// <summary>
/// 获取两个时间的相差多少小时
/// </summary>
/// <param name="startTimer"></param>
/// <param name="endTimer"></param>
/// <returns></returns>
public int GetSubHours(DateTime startTimer, DateTime endTimer)
{
    TimeSpan startSpan = new TimeSpan(startTimer.Ticks);

    TimeSpan nowSpan = new TimeSpan(endTimer.Ticks);

    TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration();

    //返回相差时长(仅返回相差的小时)
    //return subTimer.Hours;
    //返回相差时长(返回相差的总小时数)
    return (int)subTimer.TotalHours;
}

/// <summary>
/// 获取两个时间的相差多少天
/// </summary>
/// <param name="startTimer"></param>
/// <param name="endTimer"></param>
/// <returns></returns>
public int GetSubDays(DateTime startTimer, DateTime endTimer)
{
    TimeSpan startSpan = new TimeSpan(startTimer.Ticks);

    TimeSpan nowSpan = new TimeSpan(endTimer.Ticks);

    TimeSpan subTimer = nowSpan.Subtract(startSpan).Duration();

    //返回相差时长(仅返回相差的天数)
    //return subTimer.Days;
    //返回相差时长(返回相差的总天数)
    return (int)subTimer.TotalDays;
}

A single way of writing
public class SingleTon:MonoBehaviour where T:MonoBehaviour,new()
{ private static T instance = null;

  private static readonly object locker = new object();

  public static T Instance
  {
      get
     {
         lock (locker)
         {
             if (instance == null)
                 instance = new T();
             return instance;
         }
     }
 } 

}

Guess you like

Origin blog.csdn.net/weixin_43780907/article/details/130616500