/// <summary> /// Find component in Target Child /// </summary> public static T FindDeepChild <T>(GameObject _target, string _childName) where T : Component { Transform resultTrs = UGUITools.FindDeepChild(_target, _childName); if (resultTrs != null) { return(resultTrs.gameObject.GetComponent <T>()); } return((T)((object)null)); }
/// <summary> /// Find Deep child with name /// </summary> public static Transform FindDeepChild(GameObject _target, string _childName) { Transform resultTrs = null; resultTrs = _target.transform.Find(_childName); if (resultTrs == null) { foreach (Transform trs in _target.transform) { resultTrs = UGUITools.FindDeepChild(trs.gameObject, _childName); if (resultTrs != null) { return(resultTrs); } } } return(resultTrs); }
// <summary> /// 给目标添加Collider背景 /// Add Collider Background for target /// </summary> public static GameObject AddColliderBgToTarget(GameObject target, string maskNames, bool isTransparent) { // 添加UIPaneldepth最小上面 // 保证添加的Collider放置在屏幕中间 Transform windowBg = UGUITools.FindDeepChild(target, "WindowBg"); if (windowBg == null) { //GameObject targetParent = GameUtility.GetPanelDepthMaxMin(target, false, true); //if (targetParent == null) // targetParent = target; GameObject temp = new GameObject("WindowBg"); UGUITools.AddChildToTarget(target.transform, temp.transform); RectTransform rt = temp.AddComponent <RectTransform>(); rt.offsetMax = Vector2.zero; rt.offsetMin = Vector2.zero; rt.anchorMax = Vector2.one; rt.anchorMin = Vector2.zero; windowBg = rt.transform; windowBg.SetAsFirstSibling(); } Transform Masker = UGUITools.FindDeepChild(target, "WindowColliderBg(Cool)"); if (Masker == null) { Transform root = UICenterMasterManager.Instance.GetTargetRoot(UIWindowType.ForegroundLayer); GameObject res = Resources.Load("UIPrefab/Mask/" + maskNames) as GameObject; Masker = UGUITools.AddChild(root.gameObject, res).transform; Masker.name = "WindowColliderBg(Cool)"; SetRectTransformOffset(Masker.gameObject, Vector2.zero, Vector2.zero); res = null; UGUITools.AddChildToTarget(windowBg, Masker.transform); if (isTransparent) { Masker.GetComponent <UnityEngine.UI.Image>().color = new Color(1, 1, 1, 0.1f); } else { Masker.GetComponent <UnityEngine.UI.Image>().color = new Color(1, 1, 1, 0.5f); } // add sprite or widget to ColliderBg //UIWidget widget = null; //if (!isTransparent) // widget = NGUITools.AddSprite(windowBg.gameObject, altas, maskName); //else // widget = NGUITools.AddWidget<UIWidget>(windowBg.gameObject); //widget.name = "WindowColliderBg(Cool)"; //bg = widget.transform; //// fill the screen //// You can use the new Anchor system //UIStretch stretch = bg.gameObject.AddComponent<UIStretch>(); //stretch.style = UIStretch.Style.Both; //// set relative size bigger //stretch.relativeSize = new Vector2(1.5f, 1.5f); //// set a lower depth //widget.depth = -5; //// set alpha //widget.alpha = 0.6f; //// add collider //NGUITools.AddWidgetCollider(bg.gameObject); } return(Masker.gameObject); }