栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Unity SKFramework框架(十四)、Extension 扩展函数

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity SKFramework框架(十四)、Extension 扩展函数

目录

简介

一、DotNet

Array

bool

Class

DateTime

Dictionary

int 

List

Queue

Stack

string

二、Unity

AudioSource

Transform 

RectTransform


简介

该部分是框架中使用this关键字给一些类型做的拓展函数,为了支持链式编程或记录、封装一些功能,内容会持续补充,本文给出其中部分示例。

一、DotNet

Array
/// 
/// 遍历
/// 
/// 遍历事件
public static T[] ForEach(this T[] self, Action action);
/// 
/// 倒序遍历
/// 
/// 遍历事件
public static T[] ForEachReverse(this T[] self, Action action);
/// 
/// 倒序遍历
/// 
/// 遍历事件
public static T[] ForEachReverse(this T[] self, Action action);
/// 
/// 合并
/// 
/// 合并的目标
/// 返回一个新的Array 包含被合并的两个Array中的所有元素
public static T[] Merge(this T[] self, T[] target);
/// 
/// 插入排序
/// 
/// 返回排序后的数组
public static int[] SortInsertion(this int[] self);
/// 
/// 希尔排序
/// 
/// 返回排序后的数组
public static int[] SortShell(this int[] self);
/// 
/// 选择排序
/// 
/// 返回排序后的数组
public static int[] SortSelection(this int[] self);
/// 
/// 冒泡排序
/// 
/// 返回排序后的数组
public static int[] SortBubble(this int[] self);

Example

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    private void Start()
    {
        string[] exampleArray = new string[] { "AAA", "BBB" };
        //遍历
        exampleArray.ForEach((i, s) => Debug.Log(string.Format("{0}.{1}", i + 1, s)));
        //倒序遍历
        exampleArray.ForEachReverse(m => Debug.Log(m));
        //倒序遍历
        exampleArray.ForEachReverse((i, s) => Debug.Log(string.Format("{0}.{1}", i + 1, s)));

        //Array合并
        string[] target = new string[] { "CCC", "DDD" };
        string[] merge = exampleArray.Merge(target);

        int[] intArray = new int[] { 55, 32, 57, 89, 13, 87 , 9, 21};
        //希尔排序
        intArray.SortInsertion();
        //选择排序
        intArray.SortSelection();
        //冒泡排序
        intArray.SortBubble();
    }
}

bool
/// 
/// 如果bool值为true 则执行事件
/// 
/// 事件
public static bool Execute(this bool self, Action action);
/// 
/// 根据bool值执行Action类型事件
/// 
/// 事件
public static bool Execute(this bool self, Action action);
/// 
/// bool值为true则执行第一个事件 否则执行第二个事件
/// 
public static bool Execute(this bool self, Action actionIfTrue, Action actionIfFalse);
/// 
/// bool值取反
/// 
public static bool Reverse(this bool self);

Example 

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    private bool flag;

    private void Start()
    {
        //如果flag为true 则会打印日志true
        flag.Execute(() => Debug.Log("true"));
        //如果flag为true 则会打印日志true 否则打印日志false
        flag.Execute(isTrue => Debug.Log(isTrue));
        //如果flag为true 则会打印日志true 否则打印日志false
        flag.Execute(() => Debug.Log("true"), () => Debug.Log("false"));
    }
}

Class
/// 
/// 如果对象不为null则执行事件
/// 
/// 事件
/// 执行成功返回true 否则返回false
public static bool Execute(this T self, Action action);

Example

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    public class Person 
    {
        public string name;
    }
    private Person person;

    private void Start()
    {
        person.Execute(m => Debug.Log(m.name));
    }
}

DateTime
/// 
/// 获取时间戳
/// 
/// 时间戳
public static double GetTimeStamp(this DateTime self);
/// 
/// 转换为中文
/// 
/// 前缀 周/星期
/// 中文字符串
public static string ToChinese(this DayOfWeek self, string prefix);

Example

using System;
using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    private void Start()
    {
        DateTime now  = DateTime.Now;
        //获取时间戳
        double timeStamp = now.GetTimeStamp();

        DayOfWeek day = DayOfWeek.Monday;
        //转化为中文 周一
        string chinese = day.ToChinese("周");
        //转化为中文 星期一
        chinese = day.ToChinese("星期");
    }
}

Dictionary
/// 
/// 拷贝字典
/// 
public static Dictionary Copy(this Dictionary self);
/// 
/// 遍历字典
/// 
/// 遍历事件
public static Dictionary ForEach(this Dictionary self, Action action);
/// 
/// 合并字典
/// 
/// 被合并的字典
/// 若存在相同键,是否覆盖对应值
/// 合并后的字典
public static Dictionary AddRange(this Dictionary self, Dictionary target, bool isOverride = false);
/// 
/// 将字典的所有值放入一个列表
/// 
/// 列表
public static List Value2List(this Dictionary self);
/// 
/// 将字典的所有值放入一个数组
/// 
/// 数组
public static V[] Value2Array(this Dictionary self);
/// 
/// 尝试添加
/// 
/// 键
/// 值
/// 若不存在相同键,添加成功并返回true,否则返回false
public static bool TryAdd(this Dictionary self, K k, V v);

Example

using UnityEngine;
using SK.Framework;
using System.Collections.Generic;

public class Example : MonoBehaviour
{
    private void Start()
    {
        Dictionary dic = new Dictionary() { { 5, "AAA" }, { 10, "BBB" } };

        //拷贝字典
        Dictionary copy = dic.Copy();
        //遍历字典
        dic.ForEach(m => Debug.Log(string.Format("Key{0} Value{1}", m.Key, m.Value)));

        Dictionary target = new Dictionary() { { 11, "CCC" }, { 20, "DDD" } };
        //合并字典
        dic.AddRange(target);

        //将字典的所有值放入到一个列表中
        List list = dic.Value2List();
        //将字典的所有值放入到一个Array中
        string[] array = dic.Value2Array();

        //尝试添加 
        if (dic.TryAdd(20, "DDD")) Debug.Log("添加成功");
    }
}

int 
/// 
/// 转化为字母 (1-26表示字母A-Z)
/// 
/// 字母字符
public static char ToLetter(this int self);
/// 
/// 阶乘
/// 
/// 阶乘结果
public static int Fact(this int self);

Example 

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    private void Start()
    {
        //转化为字母 => B
        char letter = 2.ToLetter();
        //阶乘 => 5 * 4 * 3 * 2 * 1
        int fact = 5.Fact();
    }
}

List
/// 
/// 遍历
/// 
/// 遍历事件
public static List ForEach(this List self, Action action);
/// 
/// 遍历
/// 
/// 遍历事件
public static List ForEach(this List self, Action action);
/// 
/// 倒序遍历
/// 
/// 遍历事件
public static List ForEachReverse(this List self, Action action);
/// 
/// 倒序遍历
/// 
/// 遍历事件
public static List ForEachReverse(this List self, Action action);
/// 
/// 拷贝
/// 
/// 返回一个具有相同元素的新的列表
public static List Copy(this List self);
/// 
/// 尝试添加
/// 
/// 添加的目标
/// 添加成功返回true 否则返回false
public static bool TryAdd(this List self, T t);

Example

using UnityEngine;
using SK.Framework;
using System.Collections.Generic;

public class Example : MonoBehaviour
{
    private void Start()
    {
        List list = new List() { "AAA", "BBB" };
        
        //遍历
        list.ForEach(m => Debug.Log(m));
        //遍历
        list.ForEach((i, m) => Debug.Log(string.Format("{0}.{1}", i + 1, m)));
        //倒序遍历
        list.ForEachReverse(m => Debug.Log(m));
        //倒序遍历
        list.ForEachReverse((i, m) => Debug.Log(string.Format("{0}.{1}", i + 1, m)));
        
        //拷贝列表
        List copy = list.Copy();

        //尝试添加
        if (list.TryAdd("CCC")) Debug.Log("添加成功");
    }
}

Queue
/// 
/// 遍历
/// 
/// 遍历事件
public static Queue ForEach(this Queue self, Action action);
/// 
/// 合并 目标队列中的元素将依次出列被合并
/// 
/// 合并的目标
public static Queue Merge(this Queue self, Queue target);
/// 
/// 拷贝
/// 
/// 返回一个包含相同元素的新的队列
public static Queue Copy(this Queue self);

Example

using UnityEngine;
using SK.Framework;
using System.Collections.Generic;

public class Example : MonoBehaviour
{
    private void Start()
    {
        Queue queue = new Queue();
        
        //遍历队列
        queue.ForEach(m => Debug.Log(m));

        Queue target = new Queue();
        //合并队列
        queue.Merge(target);

        //拷贝队列
        Queue copy = queue.Copy();
    }
}

Stack
/// 
/// 遍历
/// 
/// 遍历事件
public static Stack ForEach(this Stack self, Action action);

Example

using UnityEngine;
using SK.Framework;
using System.Collections.Generic;

public class Example : MonoBehaviour
{
    private void Start()
    {
        Stack stack = new Stack();
        //遍历
        stack.ForEach(m => Debug.Log(m));
    }
}

string
/// 
/// 计算目标字符在字符串中出现的次数 不区分大小写
/// 
/// 目标字符
/// 次数
public static int CharCount(this string self, char target);
/// 
/// 尝试转化为枚举
/// 
/// 枚举类型
/// 转化结果
public static T ToEnum(this string self);
/// 
/// 尝试转化为枚举
/// 
/// 枚举类型
/// 是否忽略大小写
/// 转化结果
public static T ToEnum(this string self, bool ignoreCase);
/// 
/// 首字母大写
/// 
/// 字符串
public static string UppercaseFirst(this string self);
/// 
/// 判断文件是否存在
/// 
/// 是否存在
public static bool FileExists(this string self);
/// 
/// 根据路径删除文件
/// 
/// 删除结果
public static bool DeleteFile(this string self);
/// 
/// 判断文件夹是否存在
/// 
/// 是否存在
public static bool DirectoryExists(this string self);
/// 
/// 根据路径创建文件夹
/// 
/// 文件夹路径
public static string CreateDirectory(this string self);
/// 
/// 根据路径删除文件夹
/// 
/// 删除结果
public static bool DeleteDirectory(this string self);
/// 
/// 合并路径
/// 
/// 被合并的路径
/// 合并后的路径
public static string PathCombine(this string self, string beCombined);
/// 
/// 转化为base64字符串
/// 
/// base64字符串
public static string ToBase64String(this string self);
/// 
/// 判断字符串是否包含中文
/// 
/// 字符串
/// 若字符串包含中文返回true,否则返回false
public static bool IsContainChinese(this string self);
/// 
/// 判断字符串是否为16进制数据
/// 
/// 若字符串符合16进制数据格式返回true,否则返回false
public static bool IsMatchHexadecimal(this string self);
/// 
/// 判断字符串是否为url
/// 
/// 若字符串符合url地址格式返回true,否则返回false
public static bool IsMatchURL(this string self);
/// 
/// 判断字符串是否为IP地址
/// 
/// 若字符串符合IP地址格式返回true,否则返回false
public static bool IsMatchIPAddress(this string self);
/// 
/// 判断字符串是否为Email邮箱
/// 
/// 若字符串符合Email邮箱格式返回true,否则返回false
public static bool IsMatchEmail(this string self);
/// 
/// 判断字符串是否为手机号码
/// 
/// 若字符串符合手机号码格式返回true,否则返回false
public static bool IsMatchMobilePhoneNumber(this string self);

Example

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    private void Start()
    {
        string str = "ABcaD";
        //字符a在字符串中出现的次数 => 2
        int count = str.CharCount('a');
        //字符串转枚举
        KeyCode a = "a".ToEnum(true);
        //首字母大写 => CoderZ
        string fuc = "coderZ".UppercaseFirst();

        string path = Application.dataPath + "/SKFramework/Cube.prefab";
        //判断文件是否存在
        bool fileExists = path.FileExists();
        //根据路径删除文件
        path.DeleteFile();

        path = Application.dataPath + "/SKFramework";
        //文件夹是否存在
        bool dirExists = path.DirectoryExists();
        //根据路径创建文件夹
        path.CreateDirectory();
        //根据路径删除文件夹
        path.DeleteDirectory();

        //路径合并
        Application.dataPath.PathCombine("SKFramework");
       
        //转化为Base64字符串 
        string base64 = str.ToBase64String();

        //字符串是否包含中文
        str.IsContainChinese();
        //字符串是否为16进制数据
        str.IsMatchHexadecimal();
        //字符串是否是url链接
        str.IsMatchURL();
        //字符串是否是ip地址
        str.IsMatchIPAddress();
        //字符串是否为Eamil邮箱
        str.IsMatchEmail();
        //字符串是否为手机号码
        str.IsMatchMobilePhoneNumber();
    }
}

二、Unity

AudioSource
public static AudioSource SetClip(this AudioSource source, AudioClip clip);
public static AudioSource SetPitch(this AudioSource source, float pitch);
public static AudioSource SetPriority(this AudioSource source, int priority);
public static AudioSource SetLoop(this AudioSource source, bool loop);
public static AudioSource SetPlayOnAwake(this AudioSource source, bool playOnAwake);
public static AudioSource SetVolume(this AudioSource source, float volume);
public static AudioSource SetPanStereo(this AudioSource source, float panStereo);
public static AudioSource SetSpatialBlend(this AudioSource source, float spatialBlend);
public static AudioSource Play(this AudioSource source, AudioClip clip);

Example

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    private void Start()
    {
        new GameObject().AddComponent()
            .SetClip(null)
            .SetVolume(.5f)
            .SetPitch(1f)
            .SetLoop(true)
            .SetPlayOnAwake(false)
            .SetPanStereo(0)
            .SetSpatialBlend(0)
            .SetPriority(128)
            .Play();
    }
}

Transform 
public static T SetSiblingIndex(this T self, int index) where T : Component;
public static T SetAsFirstSibling(this T self) where T : Component;
public static T SetAsLastSibling(this T self) where T : Component;
public static T GetSiblingIndex(this T self, out int index) where T : Component;
public static T GetPosition(this T self, out Vector3 position) where T : Component;
public static T SetPosition(this T self, Vector3 pos) where T : Component;
public static T SetPosition(this T self, float x, float y, float z) where T : Component;
public static T SetPositionX(this T self, float x) where T : Component;
public static T SetPositionY(this T self, float y) where T : Component;
public static T SetPositionZ(this T self, float z) where T : Component;
public static T PositionIdentity(this T self) where T : Component;
public static T RotationIdentity(this T self) where T : Component;
public static T SetEulerAngles(this T self, Vector3 eulerAngles) where T : Component;
public static T SetEulerAngles(this T self, float x, float y, float z) where T : Component;
public static T SetEulerAnglesX(this T self, float x) where T : Component;
public static T SetEulerAnglesY(this T self, float y) where T : Component;
public static T SetEulerAnglesZ(this T self, float z) where T : Component;
public static T EulerAnglesIdentity(this T self) where T : Component;
public static T SetLocalPosition(this T self, Vector3 localPos) where T : Component;
public static T SetLocalPosition(this T self, float x, float y, float z) where T : Component;
public static T SetLocalPositionX(this T self, float x) where T : Component;
public static T SetLocalPositionY(this T self, float y) where T : Component;
public static T SetLocalPositionZ(this T self, float z) where T : Component;
public static T LocalPositionIdentity(this T self) where T : Component;
public static T LocalRotationIdentity(this T self) where T : Component;
public static T SetLocalEulerAngles(this T self, Vector3 localEulerAngles) where T : Component;
public static T SetLocalEulerAngles(this T self, float x, float y, float z) where T : Component;
public static T SetLocalEulerAnglesX(this T self, float x) where T : Component;
public static T SetLocalEulerAnglesY(this T self, float y) where T : Component;
public static T SetLocalEulerAnglesZ(this T self, float z) where T : Component;
public static T LocalEulerAnglesIdentity(this T self) where T : Component;
public static T SetLocalScale(this T self, Vector3 localScale) where T : Component;
public static T SetLocalScale(this T self, float x, float y, float z) where T : Component;
public static T SetLocalScaleX(this T self, float x) where T : Component;
public static T SetLocalScaleY(this T self, float y) where T : Component;
public static T SetLocalScaleZ(this T self, float z) where T : Component;
public static T LocalScaleIdentity(this T self) where T : Component;
public static T Identity(this T self) where T : Component;
public static T LocalIdentity(this T self) where T : Component;
public static T SetParent(this T self, Component parent, bool worldPositionStays = true) where T : Component;
public static T SetAsRootTransform(this T self) where T : Component;
public static T DetachChildren(this T self) where T : Component;
public static T LookAt(this T self, Vector3 worldPosition) where T : Component;
public static T LookAt(this T self, Vector3 worldPosition, Vector3 worldUp) where T : Component;
public static T LookAt(this T self, Transform target) where T : Component;
public static T LookAt(this T self, Transform target, Vector3 worldUp) where T : Component;
public static T Rotate(this T self, Vector3 eulers) where T : Component;
public static T Rotate(this T self, Vector3 eulers, Space relativeTo) where T : Component;
public static T Rotate(this T self, Vector3 axis, float angle) where T : Component;
public static T Rotate(this T self, Vector3 axis, float angle, Space relativeTo) where T : Component;
public static T Rotate(this T self, float xAngle, float yAngle, float zAngle) where T : Component;
public static T Rotate(this T self, float xAngle, float yAngle, float zAngle, Space relativeTo) where T : Component;
public static T CopyTransformValues(this T self, Component target) where T : Component;
public static T GetFullName(this T self, out string fullName) where T : Component;
public static T GetComponentOnChild(this Transform self, int childIndex) where T : Component;

Example

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    private void Start()
    {
        new GameObject().transform
            .SetPositionX(0f)
            .SetPositionY(0f)
            .SetPositionZ(0f)
            .SetPosition(Vector3.zero)
            .SetPosition(0f, 0f, 0f)
            .PositionIdentity()
            .SetEulerAnglesX(0f)
            .SetEulerAnglesY(0f)
            .SetEulerAnglesZ(0f)
            .SetEulerAngles(Vector3.zero)
            .SetEulerAngles(0f, 0f, 0f)
            .EulerAnglesIdentity()
            .SetLocalScaleX(1f)
            .SetLocalScaleY(1f)
            .SetLocalScaleZ(1f)
            .SetLocalScale(Vector3.one)
            .SetLocalScale(1f, 1f, 1f)
            .LocalIdentity()
            .Identity()
            .LocalIdentity();
    }
}

RectTransform
public static RectTransform SetAnchoredPosition(this RectTransform self, Vector2 anchoredPosition);
public static RectTransform SetAnchoredPosition(this RectTransform self, float x, float y);
public static RectTransform SetAnchoredPositionX(this RectTransform self, float x);
public static RectTransform SetAnchoredPositionY(this RectTransform self, float y);
public static RectTransform SetOffsetMax(this RectTransform self, Vector2 offsetMax);
public static RectTransform SetOffsetMax(this RectTransform self, float x, float y);
public static RectTransform SetOffsetMaxX(this RectTransform self, float x);
public static RectTransform SetOffsetMaxY(this RectTransform self, float y);
public static RectTransform SetOffsetMin(this RectTransform self, Vector2 offsetMin);
public static RectTransform SetOffsetMin(this RectTransform self, float x, float y);
public static RectTransform SetOffsetMinX(this RectTransform self, float x);
public static RectTransform SetOffsetMinY(this RectTransform self, float y);
public static RectTransform SetAnchoredPosition3D(this RectTransform self, Vector3 anchoredPosition3D);
public static RectTransform SetAnchoredPosition3D(this RectTransform self, float x, float y);
public static RectTransform SetAnchoredPosition3DX(this RectTransform self, float x);
public static RectTransform SetAnchoredPosition3DY(this RectTransform self, float y);
public static RectTransform SetAnchorMin(this RectTransform self, Vector2 anchorMin);
public static RectTransform SetAnchorMin(this RectTransform self, float x, float y);
public static RectTransform SetAnchorMinX(this RectTransform self, float x);
public static RectTransform SetAnchorMinY(this RectTransform self, float y);
public static RectTransform SetAnchorMax(this RectTransform self, Vector2 anchorMax);
public static RectTransform SetAnchorMax(this RectTransform self, float x, float y);
public static RectTransform SetAnchorMaxX(this RectTransform self, float x);
public static RectTransform SetAnchorMaxY(this RectTransform self, float y);
public static RectTransform SetPivot(this RectTransform self, Vector2 pivot);
public static RectTransform SetPivot(this RectTransform self, float x, float y);
public static RectTransform SetPivotX(this RectTransform self, float x);
public static RectTransform SetPivotY(this RectTransform self, float y);
public static RectTransform SetSizeDelta(this RectTransform self, Vector2 sizeDelta);
public static RectTransform SetSizeDelta(this RectTransform self, float x, float y);
public static RectTransform SetSizeDeltaX(this RectTransform self, float x);
public static RectTransform SetSizeDeltaY(this RectTransform self, float y);
public static RectTransform SetWidthWithCurrentAnchors(this RectTransform self, float width);
public static RectTransform SetHeightWithCurrentAnchors(this RectTransform self, float height);

Example

using UnityEngine;
using SK.Framework;

public class Example : MonoBehaviour
{
    private void Start()
    {
        GetComponent()
            .SetAnchoredPositionX(0f)
            .SetAnchoredPositionY(0f)
            .SetAnchoredPosition(0f, 0f)
            .SetAnchoredPosition(Vector2.zero)
            .SetOffsetMaxX(0f)
            .SetOffsetMaxY(0f)
            .SetOffsetMax(0f, 0f)
            .SetOffsetMax(Vector2.zero)
            .SetOffsetMinX(0f)
            .SetOffsetMinY(0f)
            .SetOffsetMin(0f, 0f)
            .SetOffsetMin(Vector2.zero)
            .SetAnchoredPosition3DX(0f)
            .SetAnchoredPosition3DY(0f)
            .SetAnchoredPosition3D(0f, 0f)
            .SetAnchoredPosition(Vector2.zero)
            .SetAnchorMinX(0f)
            .SetAnchorMinY(0f)
            .SetAnchorMin(0f, 0f)
            .SetAnchorMin(Vector2.zero)
            .SetAnchorMaxX(0f)
            .SetAnchorMaxY(0f)
            .SetAnchorMax(0f, 0f)
            .SetAnchorMax(Vector2.zero)
            .SetPivotX(0f)
            .SetPivotY(0f)
            .SetPivot(0f, 0f)
            .SetPivot(Vector2.zero)
            .SetSizeDeltaX(0f)
            .SetSizeDeltaY(0f)
            .SetSizeDelta(0f, 0f)
            .SetSizeDelta(Vector2.zero)
            .SetWidthWithCurrentAnchors(0f)
            .SetHeightWithCurrentAnchors(0f);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/947218.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号