| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- using LuaInterface;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine.UI;
- namespace UnityEngine
- {
- public static class UnityEngineUtils
- {
- #region Instantiate
- public static T Instantiate<T>(this T original) where T : Object
- {
- return Object.Instantiate(original) as T;
- }
- public static GameObject Root(this GameObject gameObject)
- {
- return gameObject.transform.root.gameObject;
- }
-
- public static void SetSafeActive(this GameObject gameObject, bool bValue)
- {
- if (gameObject == null)
- {
- return;
- }
- gameObject.SetActive(bValue);
- }
- public static void SetSafeActive(this Transform transform, bool bValue)
- {
- if (transform == null)
- {
- return;
- }
- transform.gameObject.SetActive(bValue);
- }
-
- public static void SetVisible(this MonoBehaviour component, bool bValue)
- {
- if (component == null)
- {
- return;
- }
- component.gameObject.SetActive(bValue);
- }
- public static bool IsVisible(this MonoBehaviour component)
- {
- return !(component == null) && component.gameObject.activeSelf;
- }
- public static void SetSafeEnable(this MonoBehaviour component, bool bValue)
- {
- if (component == null)
- {
- return;
- }
- component.enabled = bValue;
- }
- public static void Destroy(this GameObject go)
- {
- if (go == null) return;
- ((Object)go).Destroy();
- }
- public static void DestroyComponent(this GameObject go, Type comName)
- {
- if (go == null) return;
- var com = go.GetComponent(comName.ToString());
- if (com == null) return;
- GameObject.DestroyImmediate(com);
- }
- #endregion
- #region
- public static GameObject getGameObject(this Transform tr, string name)
- {
- if (tr == null)
- {
- DebugHelper.LogError("No such Transform ");
- return null;
- }
- Transform trs = tr.Find(name);
- if (trs)
- {
- return trs.gameObject;
- }
- else
- {
- DebugHelper.LogError("No such item {0}" , name);
- return null;
- }
- }
- public static void RemoveAllChild(this Transform transform)
- {
- if (transform == null)
- {
- return;
- }
- int i = 0;
- int childCount = transform.childCount;
- while (i < childCount)
- {
- Transform child = transform.GetChild(i);
- child.gameObject.SetActive(false);
- GameObject.Destroy(child.gameObject);
- i++;
- }
- }
- [NoToLua]
- public static T GetComponentUpwards<T>(this GameObject _go) where T : Component
- {
- Transform parent = _go.transform.parent;
- while (parent != null)
- {
- T t = parent.GetComponent(typeof(T)) as T;
- if (t != null)
- {
- return t;
- }
- parent = parent.parent;
- }
- return (T)((object)null);
- }
- [NoToLua]
- public static T GetComponentInChildrenFast<T>(this GameObject go) where T : Component
- {
- Component component = go.GetComponent(typeof(T));
- if (component != null)
- {
- return component as T;
- }
- Transform transform = go.transform;
- if (transform != null)
- {
- int childCount = transform.childCount;
- for (int i = 0; i < childCount; i++)
- {
- Transform child = transform.GetChild(i);
- T componentInChildrenFast = child.gameObject.GetComponentInChildrenFast<T>();
- if (componentInChildrenFast != null)
- {
- return componentInChildrenFast;
- }
- }
- }
- return (T)((object)null);
- }
- [NoToLua]
- public static T GetComponentInChildrenFast<T>(this Component component) where T : Component
- {
- return component.gameObject.GetComponentInChildrenFast<T>();
- }
- [NoToLua]
- public static T GetComponent<T>(this GameObject go, bool create) where T : Component
- {
- T t = go.GetComponent(typeof(T)) as T;
- if (create && t == null)
- {
- t = (go.AddComponent(typeof(T)) as T);
- }
- return t;
- }
- public static Component GetOrAddComponent(this GameObject go, Type type)
- {
- Component comp = null;
- if (type != null)
- {
- comp = go.GetComponent(type);
- if (!comp)
- comp = go.AddComponent(type);
- }
- return comp;
- }
- /// <summary>
- /// Gets or add a component. Usage example:
- /// BoxCollider boxCollider = transform.GetOrAddComponent<BoxCollider>();
- /// </summary>
- [NoToLua]
- public static T GetOrAddComponent<T>(this Component child) where T : Component
- {
- T result = child.GetComponent<T>();
- if (result == null)
- {
- result = child.gameObject.AddComponent<T>();
- }
- return result;
- }
- [NoToLua]
- public static T GetComponent<T>(this Component component, bool create) where T : Component
- {
- T t = component.GetComponent(typeof(T)) as T;
- if (create && t == null)
- {
- t = (component.gameObject.AddComponent(typeof(T)) as T);
- }
- return t;
- }
- #endregion
- public static void SetLayer(GameObject go, string layerName)
- {
- if (go == null) return;
- LayerMask mask = LayerMask.NameToLayer(layerName);
- SetLayer(go.transform, mask);
- }
- private static void SetLayer(Transform tr, LayerMask mask)
- {
- if (tr == null)
- return;
- for (int i = 0; i < tr.childCount; ++i)
- {
- SetLayer(tr.GetChild(i), mask);
- }
- tr.gameObject.layer = mask;
- }
- public static Material GetMaterial(this Renderer render)
- {
- #if UNITY_EDITOR
- return render.material;
- #else
- return render.sharedMaterial;
- #endif
- }
- public static void SetMaterialBlack(this GameObject go, bool black = false)
- {
- if (go == null) return;
- Renderer[] renders = go.GetComponentsInChildren<Renderer>();
- if (renders != null)
- {
- string shaderName = "";
- for (int i = 0; i < renders.Length; ++i)
- {
- Material mMaterial;
- #if UNITY_EDITOR
- mMaterial = renders[i].material;
- #else
- mMaterial = renders[i].sharedMaterial;
- #endif
- if (mMaterial != null)
- {
- shaderName = mMaterial.shader.name;
- if (black)
- {
- if(shaderName.Contains("Particles"))
- {
- continue;
- }
- mMaterial.shader = Shader.Find("Diffuse");
- mMaterial.SetVector("_Color", new Vector4(0,0,0,1));
- //mMaterial.EnableKeyword("BLACKMODE");
- }
- else
- {
- if (shaderName.Contains("Diffuse"))
- {
- mMaterial.shader = Shader.Find("Mx_JS/HeroShow_RimTail");
- }
- }
- }
- shaderName = null;
- }
- }
- }
- public static void SetMaterialGray(this GameObject go, bool gray = false)
- {
- Renderer[] renders = go.GetComponentsInChildren<SkinnedMeshRenderer>();
- if (renders != null)
- {
- for (int i = 0; i < renders.Length; ++i)
- {
- Material mMaterial = renders[i].material;
- if (mMaterial != null && mMaterial.HasProperty("_Strength"))
- {
- if (gray)
- {
- mMaterial.SetFloat("_Strength", 0.314f);
- }
- else
- {
- mMaterial.SetFloat("_Strength", 1);
- }
- }
- }
- }
- }
- public static Color SetAlpha(this Color clr,float alpha)
- {
- clr.a = alpha;
- return clr;
- }
- public static void PauseEffectParticle(GameObject effectGameObject)
- {
- if (effectGameObject == null)
- {
- return;
- }
- ParticleSystem[] particals = effectGameObject.GetComponentsInChildren<ParticleSystem>(true);
- foreach (ParticleSystem p in particals)
- {
- p.Pause(false);
- }
- }
- public static void ResumeEffectParticle(GameObject effectGameObject)
- {
- if (effectGameObject == null)
- {
- return;
- }
- ParticleSystem[] particals = effectGameObject.GetComponentsInChildren<ParticleSystem>(true);
- foreach (ParticleSystem p in particals)
- {
- p.Play(false);
- }
- }
- public static void SetUnscalTimePartical(GameObject effectGameObject)
- {
- if (effectGameObject != null && effectGameObject.GetComponent<UnscaleTimeEffect>() == null)
- {
- effectGameObject.AddComponent<UnscaleTimeEffect>();
- }
- }
- public static void DestroyImmediate(this Object obj)
- {
- GameObject.DestroyImmediate(obj);
- }
- public static Transform GetTargetInChildens<T>(string targets, Transform transform) where T : Component
- {
- if (string.IsNullOrEmpty(targets))
- {
- return transform;
- }
- Transform[] componentsInChildren = transform.GetComponentsInChildren<Transform>(true);
- for (int i = 0; i < componentsInChildren.Length; i++)
- {
- if (componentsInChildren[i].name.Equals(targets))
- {
- return componentsInChildren[i];
- }
- }
- return transform;
- }
- /// <summary>
- /// 获取top物体path路径下物体的T组件
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="top"></param>
- /// <param name="path"></param>
- /// <param name="create"></param>
- /// <returns></returns>
- [NoToLua]
- public static T GetComponentByPath<T>(this GameObject top, string path, bool create = false) where T : Component
- {
- Transform t = top != null ? (string.IsNullOrEmpty(path) ? top.transform : top.transform.Find(path)) : null;
- if (t != null)
- {
- T c = t.GetComponent<T>();
- if (c == null && create)
- c = t.gameObject.AddComponent<T>();
- return c;
- }
- return null;
- }
- public static void Destroy(this Object obj)
- {
- GameObject.Destroy(obj);
- }
- public static string GetParentPath(this Transform tr, Transform root)
- {
- string transPath = string.Empty;
- if (tr != null && root != null && root != tr)
- {
- List<string> path = new List<string>();
- path.Add(tr.name);
- GetParentPathEX(tr.parent, ref path, root);
- if (path.Count > 0)
- {
- StringBuilder sb = new StringBuilder(1024);
- for (int i = path.Count - 1; i >= 0; --i)
- {
- sb.Append(path[i]);
- if (i != 0)
- sb.Append('/');
- }
- transPath = sb.ToString();
- }
- }
- else if (tr != null)
- {
- transPath = tr.name;
- }
- return transPath;
- }
- public static List<string> GetParentPathEX(Transform tr, ref List<string> path, Transform root)
- {
- if (tr != null && root != tr)
- {
- path.Add(tr.name);
- return GetParentPathEX(tr.parent, ref path, root);
- }
- return path;
- }
- public static void SetParentNormalize(this GameObject go, GameObject parent)
- {
- if (go == null || parent == null)
- {
- return;
- }
- go.transform.SetParentNormalize(parent.transform);
- }
- public static void SetParentNormalize(this GameObject go, Transform parent)
- {
- if (go == null)
- {
- return;
- }
- go.transform.SetParentNormalize(parent);
- }
- public static void SetParentNormalize(this Transform tr, Transform parent)
- {
- if (tr == null)
- {
- return;
- }
- tr.SetParent(parent);
- tr.ResetTransform();
- }
- public static void ResetTransform(this GameObject go)
- {
- if (go != null)
- go.transform.ResetTransform();
- }
- public static void ResetTransform(this Transform tr)
- {
- if (tr != null)
- {
- tr.localPosition = Vector3.zero;
- tr.localEulerAngles = Vector3.zero;
- tr.localScale = Vector3.one;
- }
- }
- public static Transform RecurisiveFindTransformChild(Transform t, string name)
- {
- if (t == null)
- return null;
- if (string.IsNullOrEmpty(name) || name.CompareTo(t.name) == 0)
- return t;
- for (int i = 0; i < t.childCount; i++)
- {
- Transform childT = RecurisiveFindTransformChild(t.GetChild(i), name);
- if (childT != null)
- return childT;
- }
- return null;
- }
- public static void DisplayMobileKeyboard(this InputField input, bool display)
- {
- input.keyboardType = display ? TouchScreenKeyboardType.Default :(TouchScreenKeyboardType)(-1);
- input.ActivateInputField();
- }
- public static bool CheckBoundIsInCamera(this Bounds bound, Camera camera)
- {
- System.Func<Vector4, int> ComputeOutCode = (projectionPos) =>
- {
- int _code = 0;
- if (projectionPos.x < -projectionPos.w) _code |= 1;
- if (projectionPos.x > projectionPos.w) _code |= 2;
- if (projectionPos.y < -projectionPos.w) _code |= 4;
- if (projectionPos.y > projectionPos.w) _code |= 8;
- if (projectionPos.z < -projectionPos.w) _code |= 16;
- if (projectionPos.z > projectionPos.w) _code |= 32;
- return _code;
- };
- Vector4 worldPos = Vector4.one;
- int code = 63;
- for (int i = -1; i <= 1; i += 2)
- {
- for (int j = -1; j <= 1; j += 2)
- {
- for (int k = -1; k <= 1; k += 2)
- {
- worldPos.x = bound.center.x + i * bound.extents.x;
- worldPos.y = bound.center.y + j * bound.extents.y;
- worldPos.z = bound.center.z + k * bound.extents.z;
- code &= ComputeOutCode(camera.projectionMatrix * camera.worldToCameraMatrix * worldPos);
- }
- }
- }
- return code == 0 ? true : false;
- }
- }
- }
|