unity 项目常常用到的一些工具函数SomeCommon

作者:雨辰 发布于:2016-1-20 10:10 Wednesday 分类:Unity3D

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

/// <summary>
/// zhuwh
/// </summary>
public class SomeCommon
{
    public static void SetRenderQueue(Transform tran, int q)
    {
        Renderer[] rs = tran.GetComponentsInChildren<Renderer>();
        foreach (Renderer r in rs)
        {
            r.sharedMaterial.renderQueue = q;
        }
    }
    public static void NGUIAddDepth(Transform par, int add)
    {
        UIWidget[] t = par.GetComponentsInChildren<UIWidget>(true);
        foreach (UIWidget w in t)
        {
            w.depth += add;
        }
    }
    public static void DestoryAllChildComponent<T>(Transform par)
where T : Component
    {
        T[] t = par.GetComponentsInChildren<T>(true);
        foreach (T item in t)
        {
            GameObject.DestroyImmediate(item.gameObject);
        }
    }

    public static void DestoryAllChildComponent(GameObject par)
    {
        while (par.transform.childCount > 0)
        {
            GameObject.DestroyImmediate(par.transform.GetChild(0).gameObject);
        }
    }

    public static T EnumParse<T>(string s)
    {
        return (T)System.Enum.Parse(typeof(T), s);
    }

    public static void UI_SetCollider(GameObject obj, bool _enable)
    {
        BoxCollider collider = obj.GetComponent<BoxCollider>();
        if (collider != null)
        {
            collider.enabled = _enable;
        }
        else
        {
            BoxCollider[] colliders = obj.GetComponentsInChildren<BoxCollider>();
            if (colliders != null)
            {
                foreach (BoxCollider bc in colliders)
                {
                    bc.enabled = _enable;
                }
            }
        }
    }

    public static List<T> List_Revert<T>(List<T> list)
    {
        List<T> newList = new List<T>(list);
        newList.Reverse();
        return newList;
    }
    public static List<string> String2List_String(string str)
    {
        string[] sA = str.Split('|');
        return new List<string>(sA);
    }
    public static List<int> String2List_Int(string str)
    {
        string[] sA = str.Split('|');
        List<int> list = new List<int>();

        foreach (string s in sA)
        {
            list.Add(int.Parse(s));
        }
        return list;
    }

    public static Vector3 GetMouseLocalPosition()
    {
        return Input.mousePosition - new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 0);
    }
     public static void RemoveColor(GameObject go)
        {
            UIWidget[] w = go.GetComponentsInChildren<UIWidget>();
            if (null == w)
            {
                return;
            }
            for (int i = 0; i < w.Length; i++)
            {
                if (w[i].GetType() == typeof(UISprite))
                {
                    w[i].color = new Color(0,1,1);
                }
                else if (w[i].GetType() == typeof(UILabel))
                {
                    w[i].color = new Color(0.5f, 0.5f, 0.5f);
                }
            }
        }
     public static void ReductionColor(GameObject go)
        {
            UIWidget[] w = go.GetComponentsInChildren<UIWidget>();
            if (null == w)
            {
                return;
            }
            for (int i = 0; i < w.Length; i++)
            {
                w[i].color = new Color(1, 1, 1);
            }
        }
 }

标签: Unity3D C#

发表评论:

雨辰 joyimp|@2011-2018 京ICP备16030765号