Last active
October 3, 2018 12:52
-
-
Save glebov21/f95950f1b8ca5e6846d0fa9e9408fcdb to your computer and use it in GitHub Desktop.
Extensions utils (unity3d)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenTK; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Events; | |
using UnityEngine.EventSystems; | |
using UnityEngine.Profiling; | |
using System.Linq; | |
using UnityEngine.UI; | |
public static class Extension | |
{ | |
public static IEnumerable<T> GetComponentsInCurrentScene<T>(bool includeInactive) | |
{ | |
foreach(var root in SceneManager.GetActiveScene().GetRootGameObjects()) | |
{ | |
foreach(var rootChild in root.GetComponentsInChildren<T>(includeInactive)) | |
{ | |
yield return rootChild; | |
} | |
} | |
} | |
public static string GetDateInPrintableFormat(this DateTime date, bool isSkipWeekDay = true) | |
{ | |
int year = date.Year; | |
int month = date.Month; | |
int day = date.Day; | |
string dateString = String.Empty; | |
switch (month) | |
{ | |
case 1: | |
dateString = "Jan"; | |
break; | |
case 2: | |
dateString = "Feb"; | |
break; | |
case 3: | |
dateString = "Mar"; | |
break; | |
case 4: | |
dateString = "Apr"; | |
break; | |
case 5: | |
dateString = "May"; | |
break; | |
case 6: | |
dateString = "Jun"; | |
break; | |
case 7: | |
dateString = "Jul"; | |
break; | |
case 8: | |
dateString = "Aug"; | |
break; | |
case 9: | |
dateString = "Sep"; | |
break; | |
case 10: | |
dateString = "Oct"; | |
break; | |
case 11: | |
dateString = "Nov"; | |
break; | |
case 12: | |
dateString = "Dec"; | |
break; | |
default: | |
break; | |
} | |
if (isSkipWeekDay) | |
{ | |
dateString += " " + day + ", " + year; | |
} | |
else | |
{ | |
dateString += " " + day + ", " + year + " " + date.DayOfWeek.ToString().Substring(0, 3); | |
} | |
return dateString; | |
} | |
public static void DoAfterUpdate(this MonoBehaviour sender, Action action) | |
{ | |
if(sender && sender.isActiveAndEnabled) | |
sender.StartCoroutine(DoActionAfterUpdate(action)); | |
} | |
private static IEnumerator DoActionAfterUpdate(Action action) | |
{ | |
yield return new WaitForEndOfFrame(); | |
yield return new WaitForEndOfFrame(); | |
action.Invoke(); | |
} | |
public static void DeleteChildrenOnly(this GameObject go) | |
{ | |
if (go != null) | |
{ | |
foreach (Transform tr in go.transform) | |
UnityEngine.Object.Destroy(tr.gameObject); | |
} | |
} | |
public static Vector3 ToVector3(this SVector3 sVector) | |
{ | |
return new Vector3(sVector.X, sVector.Y, sVector.Z); | |
} | |
public static Quaternion ToQuaternion(this SQuaternion sQuaternion) | |
{ | |
return new Quaternion(sQuaternion.X, sQuaternion.Y, sQuaternion.Z, sQuaternion.W); | |
} | |
public static GameObject AddChild(this GameObject parent, GameObject prefab) | |
{ | |
GameObject go = GameObject.Instantiate<GameObject>(prefab); | |
go.gameObject.SetParent(parent); | |
return go; | |
} | |
public static T AddChild<T>(this GameObject parent, T prefab) where T : Component | |
{ | |
T go = GameObject.Instantiate<T>(prefab); | |
go.gameObject.SetParent(parent); | |
return go; | |
} | |
public static void SetParent(this GameObject go, GameObject parent) | |
{ | |
if (go != null) | |
{ | |
Transform t = go.transform; | |
if (parent != null) | |
{ | |
t.SetParent(parent.transform); | |
go.layer = parent.layer; | |
ChangeChildsLayer(go.transform); | |
} | |
t.localPosition = Vector3.zero; | |
t.localRotation = Quaternion.identity; | |
t.localScale = Vector3.one; | |
} | |
} | |
public static void ChangeChildsLayer(Transform parent) | |
{ | |
foreach (Transform tr in parent) | |
{ | |
tr.gameObject.layer = parent.gameObject.layer; | |
ChangeChildsLayer(tr); | |
} | |
} | |
public static void SetBtnImageColor(this Button btn, Color color) | |
{ | |
btn.GetComponent<Image>().color = color; | |
} | |
public static void SetNormalColor(this Button btn, Color color) | |
{ | |
float h, s, v; | |
Color.RGBToHSV(color, out h, out s, out v); | |
ColorBlock colorBlock = btn.colors; | |
colorBlock.normalColor = color; | |
colorBlock.highlightedColor = Color.HSVToRGB(h, s, Mathf.Clamp01(v + 0.2F)); | |
colorBlock.pressedColor = Color.HSVToRGB(h, s, Mathf.Clamp01(v - 0.2F)); | |
btn.colors = colorBlock; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <typeparam name="T">PointerEventData</typeparam> | |
/// <param name="gameObject"></param> | |
/// <param name="eventType"></param> | |
/// <param name="action"></param> | |
public static void SetEvent<T>(this GameObject gameObject, EventTriggerType eventType, Action<T> action) where T : BaseEventData | |
{ | |
var trigger = gameObject.GetOrSetAndGetEventTrigger(); | |
trigger.SetEvent(eventType, action); | |
} | |
public static void RemoveEvents(this GameObject gameObject, EventTriggerType eventType) | |
{ | |
var trigger = gameObject.GetOrSetAndGetEventTrigger(); | |
trigger.RemoveEvent(eventType); | |
} | |
private static EventTrigger GetOrSetAndGetEventTrigger(this GameObject go) | |
{ | |
var component = go.GetComponent<EventTrigger>(); | |
if (component == null) | |
{ | |
component = go.AddComponent<EventTrigger>(); | |
} | |
return component; | |
} | |
public static void SetEvent<T>(this EventTrigger trigger, EventTriggerType eventType, Action<T> action) where T : BaseEventData | |
{ | |
EventTrigger.Entry entry = new EventTrigger.Entry(); | |
entry.eventID = eventType; | |
entry.callback.SetListener((eventData) => action((T)eventData)); | |
trigger.triggers.RemoveAll(x => x.eventID == eventType); | |
trigger.triggers.Add(entry); | |
} | |
public static void RemoveEvent(this EventTrigger trigger, EventTriggerType eventType) | |
{ | |
trigger.triggers.RemoveAll(x => x.eventID == eventType); | |
} | |
public static void SetListener(this UnityEvent Event, UnityAction action) | |
{ | |
Event.RemoveAllListeners(); | |
Event.AddListener(action); | |
} | |
public static void SetListener<T0>(this UnityEvent<T0> Event, UnityAction<T0> action) | |
{ | |
Event.RemoveAllListeners(); | |
Event.AddListener(action); | |
} | |
public static void SetListener<T0, T1>(this UnityEvent<T0, T1> Event, UnityAction<T0, T1> action) | |
{ | |
Event.RemoveAllListeners(); | |
Event.AddListener(action); | |
} | |
public static void SetListener<T0, T1, T2>(this UnityEvent<T0, T1, T2> Event, UnityAction<T0, T1, T2> action) | |
{ | |
Event.RemoveAllListeners(); | |
Event.AddListener(action); | |
} | |
public static void SetListener<T0, T1, T2, T3>(this UnityEvent<T0, T1, T2, T3> Event, UnityAction<T0, T1, T2, T3> action) | |
{ | |
Event.RemoveAllListeners(); | |
Event.AddListener(action); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment