Advertisement
MaoChessy

ext

Aug 4th, 2022
1,325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using Object = UnityEngine.Object;
  6. using Random = UnityEngine.Random;
  7.  
  8. namespace MaoUtility.MaoExts
  9. {
  10.     public static class MaoExt
  11.     {
  12.         // Enumerable
  13.  
  14.         public static T GetRandom<T>(this IEnumerable<T> collection)
  15.         {
  16.             var l = collection.ToList();
  17.             return l[Random.Range(0, l.Count)];
  18.         }
  19.  
  20.         // If
  21.  
  22.         public static T IsNull<T>(this T t, Action callback)
  23.         {
  24.             if (t == null) callback();
  25.             return t;
  26.         }
  27.  
  28.         public static T IsNotNull<T>(this T t, Action callback)
  29.         {
  30.             if (t != null) callback();
  31.             return t;
  32.         }
  33.        
  34.         public static T IsNull<T>(this T t, Action<T> callback)
  35.         {
  36.             if (t == null) callback(t);
  37.             return t;
  38.         }
  39.  
  40.         public static T IsNotNull<T>(this T t, Action<T> callback)
  41.         {
  42.             if (t != null) callback(t);
  43.             return t;
  44.         }
  45.  
  46.         public static T IfTrue<T>(this T t, Func<bool> validate, Action callback)
  47.         {
  48.             if (validate()) callback();
  49.             return t;
  50.         }
  51.  
  52.         public static T IfFalse<T>(this T t, Func<bool> validate, Action callback)
  53.         {
  54.             if (!validate()) callback();
  55.             return t;
  56.         }
  57.        
  58.         public static T IfTrue<T>(this T t, Func<bool> validate, Action<T> callback)
  59.         {
  60.             if (validate()) callback(t);
  61.             return t;
  62.         }
  63.  
  64.         public static T IfFalse<T>(this T t, Func<bool> validate, Action<T> callback)
  65.         {
  66.             if (!validate()) callback(t);
  67.             return t;
  68.         }
  69.  
  70.         // Spanw GO
  71.        
  72.         public static GameObject Spawn(this GameObject obj) => Object.Instantiate(obj, Vector3.zero, Quaternion.identity);
  73.         public static GameObject Spawn(this GameObject obj, Vector3 pos) => Object.Instantiate(obj, pos, Quaternion.identity);
  74.         public static GameObject Spawn(this GameObject obj, Vector3 pos, Quaternion quaternion) => Object.Instantiate(obj, pos, quaternion);
  75.         public static GameObject Spawn(this GameObject obj, Transform transform) => Object.Instantiate(obj, transform);
  76.  
  77.         public static GameObject SpawnLocal(this GameObject obj, Transform transform, Vector3 pos) =>
  78.             Object.Instantiate(obj, transform).With(x => x.transform.localPosition = pos);
  79.  
  80.         public static GameObject SpawnGlobal(this GameObject obj, Transform transform, Vector3 pos) =>
  81.             Object.Instantiate(obj, transform).With(x => x.transform.position = pos);
  82.  
  83.         // Spawn by t where t is monobeh
  84.        
  85.         public static T Spawn<T>(this T obj) where T : Component => Object.Instantiate(obj, Vector3.zero, Quaternion.identity);
  86.         public static T Spawn<T>(this T obj, Vector3 pos) where T : Component => Object.Instantiate(obj, pos, Quaternion.identity);
  87.         public static T Spawn<T>(this T obj, Vector3 pos, Quaternion quaternion) where T : Component => Object.Instantiate(obj, pos, quaternion);
  88.         public static T Spawn<T>(this T obj, Transform transform) where T : Component => Object.Instantiate(obj, transform);
  89.  
  90.         public static T SpawnLocal<T>(this T obj, Transform transform, Vector3 pos) where T : Component =>
  91.             Object.Instantiate(obj, transform).With(x => x.transform.localPosition = pos);
  92.  
  93.         public static T SpawnGlobal<T>(this T obj, Transform transform, Vector3 pos) where T : Component =>
  94.             Object.Instantiate(obj, transform).With(x => x.transform.position = pos);
  95.  
  96.         // Delete
  97.        
  98.         public static void DeleteComponent<T>(this T self) where T : MonoBehaviour => Object.Destroy(self);
  99.        
  100.         public static void DeleteComponent<T>(this T self, float delay) where T : MonoBehaviour => Object.Destroy(self, delay);
  101.  
  102.         public static void DeleteGO<T>(this T self) where T : MonoBehaviour => DeleteGO(self.gameObject);
  103.  
  104.         public static void DeleteGO<T>(this T self, float delay) where T : MonoBehaviour => DeleteGO(self.gameObject, delay);
  105.        
  106.         public static void DeleteGO(this GameObject self) => Object.Destroy(self);
  107.        
  108.         public static void DeleteGO(this GameObject self, float delay) => Object.Destroy(self, delay);
  109.  
  110.         // With
  111.  
  112.         public static T With<T>(this T self, Action<T> set)
  113.         {
  114.             set(self);
  115.             return self;
  116.         }
  117.  
  118.        
  119.         public static T With<T>(this T self, Func<bool> when, Action<T> set)
  120.         {
  121.             if (when()) set(self);
  122.             return self;
  123.         }
  124.  
  125.        
  126.         public static T With<T>(this T self, Action<T> set, Func<bool> when)
  127.         {
  128.             if (when()) set(self);
  129.             return self;
  130.         }
  131.  
  132.         public static T With<T>(this T self, Action<T> set, bool when)
  133.         {
  134.             if (when) set(self);
  135.             return self;
  136.         }
  137.        
  138.         public static T With<T>(this T self,bool when, Action<T> set)
  139.         {
  140.             if (when) set(self);
  141.             return self;
  142.         }
  143.  
  144.         public static bool InMyHierarchy(this Transform self, Transform target)
  145.         {
  146.             if (target == self) return true;
  147.             var lastParent = target.parent;
  148.             while (lastParent!=null)
  149.             {
  150.                 if (lastParent == self) return true;
  151.                 lastParent = lastParent.parent;
  152.             }
  153.  
  154.             return false;
  155.         }
  156.        
  157.         public static bool InMyHierarchy(this Transform self, GameObject target) => self.InMyHierarchy(target.transform);
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement