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

Unity常用的一些简易扩展方法

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

Unity常用的一些简易扩展方法

Unity常用的一些简易扩展方法 引言

在unity项目的日常开发中,会发现unity现有的一些类的方法不是特别够用,但有些方法使用频率又很高,这时候,我们就可以用到Extension Method 扩展方法,实现自定义的扩展方法。

其次,扩展方法也可以使写法更简便一些。

附:Unity官方教程:教程

实现方法 步骤一

创建一个静态类,没有任何继承。

using UnityEngine;
using UnityEngine.UI;

/// 
/// unity扩展类
/// image
/// 
public static class ImageExtension
{
	
}
步骤二

创建一个静态方法,这个方法的第一个参数使用this,类型为你需要扩展的类名,如Transform,Image等,之后的参数根据自己需求添加。

using UnityEngine;
using UnityEngine.UI;

/// 
/// Unity扩展类
/// Image
/// 
public static class ImageExtension
{
    /// 
    /// 改变图片的透明度
    /// 
    /// 
    /// 透明度,[0, 1]
    public static void ChangeAlpha(this Image image, float alpha)
    {
        Color oldColor = image.color;
        image.color = new Color(oldColor.r, oldColor.g, oldColor.b, alpha);
    }
}
使用方式
Image img;
img.ChangeAlpha(1);
常用的一些扩展

以下的扩展是我自己在使用的过程中,觉得比较好使的一些扩展。

图片Image
using UnityEngine;
using UnityEngine.UI;

/// 
/// Unity扩展类
/// Image
/// 
public static class ImageExtension
{
    /// 
    /// 改变图片的透明度
    /// 
    /// 
    /// 透明度,[0, 1]
    public static void ChangeAlpha(this Image image, float alpha)
    {
        Color oldColor = image.color;
        image.color = new Color(oldColor.r, oldColor.g, oldColor.b, alpha);
    }
}
Gameobject
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 
/// Unity扩展类
/// Gameobject
/// 
public static class GameobjectExtension
{

    /// 
    /// 改变对象的层级
    /// 
    /// 对象
    /// 层级名称
    public static void ChangeLayer(this GameObject gameobject, string layerName)
    {
        int layer = LayerMask.NameToLayer(layerName);
        if (gameobject)
        {
            foreach (Transform traform in gameobject.GetComponentsInChildren(true))
            {
                traform.gameObject.layer = layer;
            }
        }
    }

    /// 
    /// 改变对象的层级
    /// 
    /// 对象
    /// 层级编号
    public static void ChangeLayer(this GameObject gameobject, int layer)
    {
        if (gameobject)
        {
            foreach (Transform traform in gameobject.GetComponentsInChildren(true))
            {
                traform.gameObject.layer = layer;
            }
        }
    }

    /// 
    /// 给对象加上元件
    /// 对象身上不存在这个元件
    /// 存在则不作处理
    /// 
    /// 元件
    /// 对象
    /// 
    public static T AddComponentIfNotFound(this GameObject gameObject) where T : Component
    {
        T component = gameObject.GetComponent();

        if (component == null)
        {
            component = gameObject.AddComponent();
        }

        return component;
    }

    /// 
    /// 移除物体上挂载的脚本
    /// 
    /// 
    /// 
    public static void Remove(this GameObject gameObject) where T : MonoBehaviour
    {
        if (gameObject.GetComponent())
        {
            GameObject.Destroy(gameObject.GetComponent());
        }
    }
}
MonoBehaviour
using System.Collections;
using UnityEngine;
using UnityEngine.Events;

/// 
/// Unity扩展类
/// MonoBehaviour
/// 
public static class MonoBehaviourExtension
{

    /// 
    /// 起协程
    /// 先等待延迟,然后运行事件
    /// 
    /// 事件
    /// 延迟时间
    /// 
    public static Coroutine WaitSomeTime(this MonoBehaviour mono, float time, UnityAction action)
    {
        return mono.StartCoroutine(WaitSomeTime(action, time));
    }
    private static IEnumerator WaitSomeTime(UnityAction action, float time)
    {
        yield return new WaitForSeconds(time);
        if (action != null)
        {
            action();
        }
    }

    /// 
    /// 起协程
    /// 先等待延迟,然后运行事件
    /// 
    /// 事件
    /// 延迟时间
    /// 
    public static Coroutine WaitOneUpdate(this MonoBehaviour mono, UnityAction action)
    {
        return mono.StartCoroutine(WaitOneUpdate(action));
    }
    private static IEnumerator WaitOneUpdate(UnityAction action)
    {
        yield return null;
        if (action != null)
        {
            action();
        }
    }

    /// 
    /// 起协程
    /// 先等待延迟,然后运行事件
    /// 
    /// 事件
    /// 延迟时间
    /// 
    public static Coroutine WaitEndOfFrame(this MonoBehaviour mono, UnityAction action)
    {
        return mono.StartCoroutine(WaitrEndOfFrame(action));
    }
    private static IEnumerator WaitrEndOfFrame(UnityAction action)
    {
        yield return null;
        if (action != null)
        {
            action();
        }
    }
}
Transform
using UnityEngine;
using System.Collections.Generic;

/// 
/// transform扩展类
/// 
public static class TransformExtension
{

    /// 
    /// 改变对象的层级
    /// 
    /// 对象
    /// 层级名称
    public static void ChangeLayer(this Transform tran, string layerName)
    {
        int layer = LayerMask.NameToLayer(layerName);
        if (tran)
        {
            foreach (Transform traform in tran.GetComponentsInChildren(true))
            {
                traform.gameObject.layer = layer;
            }
        }
    }

    /// 
    /// 改变对象的层级
    /// 
    /// 对象
    /// 层级编号
    public static void ChangeLayer(this Transform tran, int layer)
    {
        if (tran)
        {
            foreach (Transform traform in tran.GetComponentsInChildren(true))
            {
                traform.gameObject.layer = layer;
            }
        }
    }

    /// 
    /// 设置绝对位置的x坐标
    /// 
    /// 对象
    /// x 坐标值
    public static void SetPositionX(this Transform transform, float newValue)
    {
        Vector3 originalPos = transform.position;
        originalPos.x = newValue;
        transform.position = originalPos;
    }

    /// 
    /// 设置绝对位置的y坐标
    /// 
    /// 对象
    /// y 坐标值
    public static void SetPositionY(this Transform transform, float newValue)
    {
        Vector3 originalPos = transform.position;
        originalPos.y = newValue;
        transform.position = originalPos;
    }

    /// 
    /// 设置绝对位置的z坐标
    /// 
    /// 对象
    /// z 坐标值
    public static void SetPositionZ(this Transform transform, float newValue)
    {
        Vector3 originalPos = transform.position;
        originalPos.z = newValue;
        transform.position = originalPos;
    }
}
结语

写的不好,望各种大佬指正。

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

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

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