【Unity3D 实用技巧 - C# 常用基础代码汇总】

在这里插入图片描述

using System;
using System.IO;
using UnityEngine;

public class Tools : MonoBehaviour
{
    
    
    void Start()
    {
    
    
        // _ToString();
        // _GetBytes("nihaasd123"); //10
        // _StringBuilder(); //中华人民共和国
        // _Split("我爱我的国家", 0, 2); //我爱
        // _IsWhiteSpace("Hello ,World!!", 5); //True
        // _IsPunctuation("Hello我,World!!", 6); //True
        // _ClearWhiteSpace("    abcd     ");
        // _ReplaceChar();
        // _StringComparison("Hello", "Hello");
        // _IndexOf();
        // _Insert();
        // _Pad();
        // _Remove();
        // JudeFolder();
        // AATest();
        // GetDate();
    }

    /// <summary>
    /// 获取不同格式的时间
    /// </summary>
    void GetDate()
    {
    
    
        DateTime currectDateTime = new DateTime();
        currectDateTime = DateTime.Now; //获取当前年月日时分秒 
        Debug.Log(currectDateTime); // 06/02/2022 11:28:54
        Debug.Log(currectDateTime.Year); //获取当前年
        Debug.Log(currectDateTime.Month); //获取当前月
        Debug.Log(currectDateTime.Day); //获取当前日
        Debug.Log(currectDateTime.Hour); //获取当前时
        Debug.Log(currectDateTime.Minute); //获取当前分
        Debug.Log(currectDateTime.Second); //获取当前秒
        Debug.Log(currectDateTime.Millisecond); //获取当前毫秒
        Debug.Log(currectDateTime.ToString("f")); //获取当前日期(中文显示)不显示秒   2022年6月2日 11:35
        Debug.Log(currectDateTime.ToString("y")); //获取当前年月   2022年6月
        Debug.Log(currectDateTime.ToString("m")); //获取当前月日   6月2日
        Debug.Log(currectDateTime.ToString("D")); //获取当前中文年月日   2022年6月2日
        Debug.Log(currectDateTime.ToString("t")); //获取当前时分   11:39
        Debug.Log(currectDateTime.ToString("s")); //获取当前时间   2022-06-02T11:40:32
        Debug.Log(currectDateTime.ToString("u")); //获取当前时间   2022-06-02 11:40:32Z
        Debug.Log(currectDateTime.ToString("g")); //获取当前时间   2022/6/2 11:40
        Debug.Log(currectDateTime.ToString("r")); //获取当前时间   Thu, 02 Jun 2022 11:40:32 GMT
        Debug.Log(currectDateTime.ToString("yyyy-MM-dd HH:mm:ss:ffff")); //2022-06-02 11:50:08:4414
        Debug.Log(currectDateTime.ToString("yyyy-MM-dd HH:mm:ss")); //2022-06-02 11:50:37
        Debug.Log(currectDateTime.ToString("yyyy/MM/dd HH:mm:ss")); //2022/06/02 11:51:17
        Debug.Log(currectDateTime.ToString("yyyy/MM/dd HH:mm:ss dddd")); //2022/06/02 11:53:43 星期四
        Debug.Log(currectDateTime.ToString("yyyy年MM月dd日 HH时mm分ss秒 ddd")); //2022/06/02 11:53:43 周四
        Debug.Log(currectDateTime.ToString("yyyyMMdd HH:mm:ss")); //20220602 11:53:02
        Debug.Log(currectDateTime.AddDays(100).ToString("yyyy-MM-dd HH:mm:ss")); //获取100天后的时间  2022-09-10 11:57:24
    }

    /// <summary>
    /// 字符串不同的转换
    /// </summary>
    void _ToString()
    {
    
    
        Debug.Log(12345.ToString("n")); //12,345.00 
        Debug.Log(12345.ToString("C")); //¥12,345.00
        Debug.Log(12345.ToString("e")); //1.234500e+004 
        Debug.Log(12345.ToString("f4")); //12345.0000 
        Debug.Log(12345.ToString("f2")); //12345.00
        Debug.Log(12345.ToString("x")); //3039 (16进制) 
        Debug.Log(12345.ToString("p")); //1,234,500.00%
    }

    /// <summary>
    /// 字码转换为比特码,获取比特长度
    /// </summary>
    /// <param name="str"></param>
    void _GetBytes(string str)
    {
    
    
        byte[] bytStr = System.Text.Encoding.Default.GetBytes(str); //字码转换为比特码
        int len = bytStr.Length;
        Debug.Log(len);
    }

    /// <summary>
    /// 字符串相加
    /// </summary>
    void _StringBuilder()
    {
    
    
        System.Text.StringBuilder sb = new System.Text.StringBuilder("");
        sb.Append("中华");
        sb.Append("人民");
        sb.Append("共和国");

        Debug.Log(sb);
    }

    /// <summary>
    /// 字符串分割
    /// </summary>
    /// <param name="str">要分割的字符串</param>
    /// <param name="startIndex">开始分割起始位数</param>
    /// <param name="endIndex">截取几位</param>
    void _Split(string str, int startIndex, int endIndex)
    {
    
    
        string temp = str.Substring(startIndex, endIndex);
        Debug.Log(temp);
    }

    /// <summary>
    /// 检查指定位置是否为空字符串
    /// </summary>
    /// <param name="str"></param>
    /// <param name="index">索要检查字符的序号</param>
    void _IsWhiteSpace(string str, int index)
    {
    
    
        bool _bool = char.IsWhiteSpace(str, index);
        Debug.Log(_bool);
    }

    /// <summary>
    /// 检查指定位置的字符是否为标点符号
    /// </summary>
    /// <param name="str"></param>
    /// <param name="index"></param>
    void _IsPunctuation(string str, int index)
    {
    
    
        bool _bool = char.IsPunctuation(str, index);
        Debug.Log(_bool);
    }

    /// <summary>
    ///  清除字串前后空格
    /// </summary>
    /// <param name="str"></param>
    void _ClearWhiteSpace(string str)
    {
    
    
        Debug.Log("Clear Before");
        Debug.Log(str);
        Debug.Log("Clear After");
        string temp = str.Trim();
        Debug.Log(temp);
    }

    /// <summary>
    /// 字符串替换
    /// </summary>
    void _ReplaceChar()
    {
    
    
        string str = "我今年三十,明年三十一";
        str = str.Replace("三", "二");
        Debug.Log(str); //我今年二十,明年二十一
    }

    /// <summary>
    /// 字符串对比的几种方式
    /// </summary>
    /// <param name="strA"></param>
    /// <param name="strB"></param>
    void _StringComparison(string strA, string strB)
    {
    
    
        //方式一
        // bool _bool = strA.EndsWith(strB); //检测字串str1是否以字串str2结尾
        //方式二
        // bool _bool = strA.Equals(strB);//检测字串str1是否与字串str2相等
        //方式三
        bool _bool = Equals(strA, strB); //检测字串str1是否与字串str2相等

        Debug.Log(_bool);
    }

    /// <summary>
    /// 查找字符的位置
    /// </summary>
    void _IndexOf()
    {
    
    
        string str = "我爱我的祖国,我爱我的家乡";
        Debug.Log(str.IndexOf("爱")); // 1   查找字符在字符串中首次出现的位置
        Debug.Log(str.IndexOf("的家")); // 10   查找字符串“的家”中的第一个字符在字符串中首次出现的位置
        Debug.Log(str.LastIndexOf("爱")); // 8   查找字符在字符串中最后一次出现的位置
    }

    /// <summary>
    /// 在字串中指定索引位插入指定字符
    /// </summary>
    void _Insert()
    {
    
    
        string str = "我爱我的祖国";
        str = str.Insert(1, "深");
        Debug.Log(str);
    }

    void _Pad()
    {
    
    
        //str.PadLeft() 在字串左加空格或指定char字符,使字串达到指定长度
        //str.PadRight() 在字串又加空格或指定char字符,使字串达到指定长度
        string str = "哈士奇";
        Debug.Log(str.PadLeft(10, '一')); //一一一一一一一哈士奇
        Debug.Log(str.PadRight(10, '一')); //哈士奇一一一一一一一
    }

    /// <summary>
    /// 从指定位置开始删除指定数的字符
    /// </summary>
    void _Remove()
    {
    
    
        string str = "哈士奇是条狗";
        str = str.Remove(0, 3);
        Debug.Log(str); //是条狗
    }

    /// <summary>
    /// 判断是否存在文件夹,没有则创建
    /// </summary>
    void JudeFolder()
    {
    
    
        if (Directory.Exists("D:/AA") == false)
        {
    
    
            Directory.CreateDirectory("D:/AA");
        }
    }

    void AATest()
    {
    
    
        AA aa = new AA();
        int a = 1, b = 2;
        aa.Sum(a, b);
        Debug.Log($"a:{
      
      a}"); //1

        aa.Sum_Ref(ref a, b);
        Debug.Log($"ref a:{
      
      a}"); //3

        aa.Sum_Out(out a, b);
        Debug.Log($"out a:{
      
      a}"); //4
    }
}

public class AA
{
    
    
    /*
     *方法中的参数,返回值 ref 型参数:该种类型的参数传递变量地址给方法(引用传递),传递前变量必须初始化。
     *ref与out型的区别在于:
     * 1)ref 型传递变量前,变量必须初始化,否则编译器会报错, 而 out 型则不需要初始化。
     * 2)ref 型传递变量,数值可以传入方法中,而 out 型无法将数据传入方法中。换而言之,ref 型有进有出,out 型只出不进。 out 型参数:与 ref 型类似,仅用于传回结果。
     * 注意:
     * 1) out 型数据在方法中必须要赋值,否则编译器会报错。 如:如下若将代码中的 sum 1 方法的方法体 改为 a+=b ; 则编译器会报错。原因:out 型只出不进,在没给 a 赋值前是不能使用的 改为 b+=b+2 ; 编译器也会报错。原因:out 型数据在方法中必须要赋值。
     * 2) 重载方法时若两个方法的区别仅限于一个参数类型为 ref 另一个方法中为 out ,编译器会报错 如:若将下面的代码中将方法名 vsum1 改为 sum(或者将方法名 sum 改为 sum1),编译器会报错。
     * Error 1 Cannot define overloaded method ‘sum’ because it differs from another method only on ref and out 原因:参数类型区别仅限于 为 ref 与为 out 时,若重载对编译器而言两者的元数据表示完全相同。
     */
    public void Sum(int a, int b)
    {
    
    
        a += b;
    }

    public void Sum_Ref(ref int a, int b)
    {
    
    
        a += b;
    }

    public void Sum_Out(out int a, int b)
    {
    
    
        a = b + 2;
    }
}

参考:https://developer.unity.cn/projects/5f969b0fedbc2a001e5916f0

猜你喜欢

转载自blog.csdn.net/qq_42862278/article/details/125099140