Example #1
0
        //根据传入的概率数组中的概率,随机出一个数值,返回相对应数值的索引值
        public static int RandomGetIndexInPercent(IList <float> percent)
        {
            if (MNullHelper.IsNullOrEmpty(percent))
            {
                return(MCommonHelper.IsFalse);
            }

            float totalPercent = 0f;

            for (int i = 0; i < percent.Count; i++)
            {
                totalPercent += percent[i];
            }

            if (totalPercent <= 0f)
            {
                return(MCommonHelper.IsFalse);
            }

            float random = RandomExclusive(0, totalPercent);

            float proportion = 0f;

            for (int i = 0; i < percent.Count; i++)
            {
                proportion += percent[i];
                if (random <= proportion)
                {
                    return(i);
                }
            }
            return(MCommonHelper.IsFalse);
        }
Example #2
0
        //得到所有的父节点
        public static Stack <Transform> GetAllParentNode(Transform go, Transform endTransform = null, bool isContainEnd = true)
        {
            Stack <Transform> parentTransforms = new Stack <Transform>();

            if (MNullHelper.IsNull(go))
            {
                return(parentTransforms);
            }
            for (; ;)
            {
                go = go.parent;
                if (go == null)
                {
                    return(parentTransforms);
                }

                if (go == endTransform)
                {
                    if (isContainEnd)
                    {
                        parentTransforms.Push(go);
                    }
                    return(parentTransforms);
                }

                parentTransforms.Push(go);
            }
        }
Example #3
0
 //如果给定的物体是空的则创建物体
 public static void CreateGameObjectIfNull(ref GameObject data, GameObject prefab, Transform parent, Action finishCreateMethod = null)
 {
     if (MNullHelper.IsNull(data))
     {
         data = CreateGameObject(prefab, parent);
         if (finishCreateMethod != null)
         {
             finishCreateMethod();
         }
     }
 }
Example #4
0
 public static void CreateGameObjectIfNull <T>(ref T data, T prefab, Transform parent, Action finishCreateMethod = null) where T : MonoBehaviour
 {
     if (MNullHelper.IsNull(data))
     {
         data = CreateGameObject(prefab, parent);
         if (finishCreateMethod != null)
         {
             finishCreateMethod();
         }
     }
 }
Example #5
0
 //引用是否失效
 public static bool IsInvalid(WeakReference reference)
 {
     if (reference.IsAlive == false)
     {
         return(true);
     }
     if (MNullHelper.IsNull(reference.Target))
     {
         return(true);
     }
     return(false);
 }
Example #6
0
 //对列表中的相应的数据进行替换
 public static bool Replace <T>(IList <T> list, T oldData, T newData)
 {
     for (int i = 0; i < list.Count; i++)
     {
         if (MNullHelper.IsEqual(list[i], oldData))
         {
             list[i] = newData;
             return(true);
         }
     }
     return(false);
 }
Example #7
0
        public static void SafeSetParent(Transform child, Transform parent)
        {
            if (MNullHelper.IsNull(child) || MNullHelper.IsNull(parent))
            {
                return;
            }

            if (!child.IsChildOf(parent))
            {
                child.SetParent(parent, false);
            }
        }
Example #8
0
 public static bool IsArrayEquel <T>(T[] array, IList <T> list)
 {
     if (array.Length != list.Count)
     {
         return(false);
     }
     for (int i = 0; i < array.Length; i++)
     {
         if (!MNullHelper.IsEqual(array[i], list[i]))
         {
             return(false);
         }
     }
     return(true);
 }
Example #9
0
 public static bool IsArrayContain <T>(T[] array, T data)
 {
     if (array == null)
     {
         return(false);
     }
     for (int i = 0; i < array.Length; i++)
     {
         if (MNullHelper.IsEqual(array[i], data))
         {
             return(true);
         }
     }
     return(false);
 }
Example #10
0
 //移除list中与data相同的所有数据
 public static void RemoveMany <T>(List <T> list, IList <T> data)
 {
     if (MNullHelper.IsNullOrEmpty(list) || MNullHelper.IsNullOrEmpty(data))
     {
         return;
     }
     for (int i = 0; i < data.Count; i++)
     {
         for (int j = 0; j < list.Count; j++)
         {
             if (MNullHelper.IsEqual(list[j], data[i]))
             {
                 list.RemoveAt(j);
                 break;
             }
         }
     }
 }
Example #11
0
        //查找子物体
        //此方法支持物体名称查找、全部或部分路径查找
        //查找方式为:先序遍历物体树状结构,返回找到的第一个
        public static Transform FindChildrenTransformRecursion(Transform root, string targetName)
        {
            Transform targetTransform;

            targetTransform = root.Find(targetName);
            if (MNullHelper.IsNull(targetTransform))
            {
                for (int i = 0; i < root.childCount; i++)
                {
                    Transform child = root.GetChild(i);
                    targetTransform = FindChildrenTransformRecursion(child, targetName);
                    if (!MNullHelper.IsNull(targetTransform))
                    {
                        break;
                    }
                }
            }
            return(targetTransform);
        }
Example #12
0
        //查找所有名字相同的子物体
        //此查找方法比较消耗性能
        //此方法只支持物体名称查找,不支持路径查找
        public static List <Transform> FindChildrenTransformsRecursion(Transform root, string targetName)
        {
            List <Transform> targetTransforms = new List <Transform>();

            if (!MNullHelper.IsNull(root) && !string.IsNullOrEmpty(targetName))
            {
                Transform child;
                for (int i = 0; i < root.childCount; i++)
                {
                    child = root.GetChild(i);
                    if (child.name == targetName)
                    {
                        targetTransforms.Add(child);
                    }
                    targetTransforms.AddRange(FindChildrenTransformsRecursion(child, targetName));
                }
            }

            return(targetTransforms);
        }
Example #13
0
        //得到物体在场景中的路径
        public static string GetGameObjectPath(Transform transform, Transform endTransform = null, bool isContainEnd = true)
        {
            if (MNullHelper.IsNull(transform))
            {
                return("");
            }

            Stack <Transform> paths = GetAllParentNode(transform, endTransform, isContainEnd);

            var path       = MSharedStringBuilderManager.Get();
            int pathsCount = paths.Count;

            for (int i = 0; i < pathsCount; i++)
            {
                Transform go = paths.Pop();

                path.Append(go.name);
                path.Append('/');
            }
            path.Append(transform.name);
            return(path.ToString());
        }