一些帮助方法

/*************************************************************************
 *  FileName: UnityHelper.cs
 *  Author: LaiZhangYin(Eagle)   Version: 1.0   Date: 6/20/2017
 * if you have some question, please call 
 *  QQ/Wechat : 782966734
 *************************************************************************/

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine.EventSystems;

public class UnityHelper : MonoBehaviour
{
    /// <summary>
    /// 查找子节点对象
    /// </summary>
    /// <param name="goParent">父对象</param>
    /// <param name="childName">要查找的子对象的名称</param>
    /// <returns></returns>
    public static Transform FindChildNode(GameObject goParent, string childName)
    {
        Transform searchTrans = null;
        searchTrans = goParent.transform.Find(childName);
        if (searchTrans == null)
        {
            foreach (Transform trans in goParent.transform)
            {
                searchTrans = FindChildNode(trans.gameObject, childName);
                if (searchTrans != null)
                {
                    return searchTrans;
                }
            }
        }
        return searchTrans;
    }
    /// <summary>
    /// 获取子节点对象脚本
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="parent">父对象</param>
    /// <param name="childName">子对象名称</param>
    /// <returns></returns>
    public static T GetChildNodeComponentScripts<T>(GameObject parent, string childName) where T : UnityEngine.Component
    {
        Transform searchTransformNode = null;
        searchTransformNode = FindChildNode(parent, childName);
        if (searchTransformNode != null)
        {
            return searchTransformNode.gameObject.GetComponent<T>();
        }
        else
        {
            return null;
        }
    }
    /// <summary>
    /// 给子对象添加脚本
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="parent">父对象</param>
    /// <param name="childName">子对象名称</param>
    /// <returns></returns>
    public static T AddChildNodeComponent<T>(GameObject parent, string childName) where T : UnityEngine.Component
    {
        Transform searchTransform = null;
        searchTransform = FindChildNode(parent, childName);

        if (searchTransform != null)
        {
            T[] componentArray = searchTransform.GetComponents<T>();
            for (int i = 0; i < componentArray.Length; i++)
            {
                if (componentArray[i] != null)
                {
                    GameObject.Destroy(componentArray[i]);
                }
            }
            return searchTransform.gameObject.AddComponent<T>();
        }
        else
        {
            return null;
        }
    }
    /// <summary>
    /// 给子对象添加父节点
    /// </summary>
    /// <param name="parents">父对象方位</param>
    /// <param name="child">子对象的方法</param>
    /// <param name="isAngles">是否改变子对象的角度,默认为不改变</param>
    public static void AddChildNodeToParentNode(Transform parents, Transform child, bool isAngles = false)
    {
        child.SetParent(parents, false);
        child.localPosition = Vector3.zero;
        child.localScale = Vector3.one;
        if (isAngles)
        {
            child.localEulerAngles = Vector3.zero;
        }
    }

    /// <summary>
    /// 只针对在resources文件夹下面的文件名
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static List<string> GetResourcesName(string path)
    {
        List<string> nameList = new List<string>();

        DirectoryInfo di = new DirectoryInfo(Application.dataPath + "/Resources/" + path);
        FileInfo[] fileInfo = di.GetFiles();
        string name = "";
        for (int i = 0, length = di.GetFiles().Length; i < length; i++)
        {
            if (fileInfo[i].Extension.Contains(".meta")) continue;

            name = Path.GetFileNameWithoutExtension(fileInfo[i].FullName);
            nameList.Add(name);
        }

        return nameList;
    }
    /// <summary>
    /// 用来获取StreamingAssets文件夹下面的文件名
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static List<string> GetStreamingAssetsName(string path)
    {
        List<string> nameList = new List<string>();

        DirectoryInfo di = new DirectoryInfo(Application.dataPath + "/StreamingAssets/Android/" + path);
        FileInfo[] fileInfo = di.GetFiles();
        string name;
        for (int i = 0, length = fileInfo.Length; i < length; i++)
        {
            if (fileInfo[i].Name.Contains(".meta") || fileInfo[i].Name.Contains(".manifest")) continue;
            name = Path.GetFileNameWithoutExtension(fileInfo[i].FullName);
            // Debug.Log(name);
            nameList.Add(name);
        }

        return nameList;
    }
    public static void SetDepth(GameObject go, int depth, bool isLabel = false)
    {
        if (!isLabel)
        {
            UISprite sprite = go.transform.GetComponent<UISprite>();
            sprite.depth = depth;
        }
        else
        {
            UILabel label = go.transform.Find("Label").GetComponent<UILabel>();
            label.depth = depth;
        }
    }
    public static T GetGameObject<T>(string path, Vector3 pos, Transform parent = null) where T : UnityEngine.Component
    {
        GameObject go = Resources.Load(path) as GameObject;
        GameObject temp = GameObject.Instantiate(go);
        temp.transform.SetParent(parent);
        temp.transform.localPosition = pos;
        temp.transform.localScale = Vector3.one;
        T t = temp.AddComponent<T>();
        //T t = Resources.Load(path) as T;
        //T temp = GameObject.Instantiate(t) as T;
        return t;
    }

    public static T GetGameObject<T>(string path) where T : UnityEngine.Component
    {
        GameObject go = Resources.Load(path) as GameObject;
        GameObject temp = GameObject.Instantiate(go);
        T t = temp.AddComponent<T>();
        return t;
    }

    public static GameObject GetGameObject(string path)
    {
        return GameObject.Instantiate(Resources.Load<GameObject>(path));
    }
    // Vector3.Distance(a.transform.position, b.transform.position):
    //    _____        _____
    //   |     |      |     |
    //   |  x==|======|==x  |
    //   |_____|      |_____|
    //
    //
    // Utils.ClosestDistance(a.collider, b.collider):
    //    _____        _____
    //   |     |      |     |
    //   |     |x====x|     |
    //   |_____|      |_____|
    //  
    public static float ClosestDistance(Collider a, Collider b)
    {
        return Vector3.Distance(a.ClosestPointOnBounds(b.transform.position),
                                b.ClosestPointOnBounds(a.transform.position));
    }
    //检查光标是否在UI或OnGUI元素上
    //对于UI,这只适用于UI的画布
    //对于OnGUI:热控制只在点击时设置,而不是在缩放时
    public static bool IsCursorOverUserInterface()
    {
        // IsPointerOverGameObject check for left mouse (default)
        if (EventSystem.current.IsPointerOverGameObject())
            return true;


        // IsPointerOverGameObject check for touches
        for (int i = 0; i < Input.touchCount; ++i)
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(i).fingerId))
                return true;


        // OnGUI check
        return GUIUtility.hotControl != 0;
    }
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值