// SetupController<Generic> private static void SetupController <TComp>( ref GameObject myGO, Transform myParent, string myName, bool needMyComponent, DPadArrowType arrowType = DPadArrowType.none) where TComp : MonoBehaviour { myGO = new GameObject(myName, typeof(Image)); myGO.GetComponent <Image>().color = defaultColor; myGO.layer = LayerMask.NameToLayer("UI"); myGO.transform.localScale = Vector3.one; myGO.transform.SetParent(myParent); if (needMyComponent) { myGO.AddComponent <TComp>(); } if (arrowType != DPadArrowType.none) { myGO.GetComponent <TCKDPadArrow>().arrowType = arrowType; CalcDPadSizeAndPos(dpadMain.GetComponent <RectTransform>(), myGO.GetComponent <RectTransform>(), arrowType); } }
// CalcDPadSizeAndPos private static void CalcDPadSizeAndPos(RectTransform mainRect, RectTransform childRect, DPadArrowType arrowType) { childRect.sizeDelta = new Vector2(mainRect.sizeDelta.x / 3.4f, mainRect.sizeDelta.y / 3.4f); switch (arrowType) { case DPadArrowType.UP: childRect.anchoredPosition = new Vector2(0f, -childRect.sizeDelta.y / 2f); childRect.anchorMin = new Vector2(.5f, 1f); childRect.anchorMax = new Vector2(.5f, 1f); childRect.rotation = Quaternion.Euler(mainRect.rotation.x, mainRect.rotation.y, mainRect.rotation.z + 90f); break; case DPadArrowType.DOWN: childRect.anchoredPosition = new Vector2(0f, childRect.sizeDelta.y / 2f); childRect.anchorMin = new Vector2(.5f, 0f); childRect.anchorMax = new Vector2(.5f, 0f); childRect.rotation = Quaternion.Euler(mainRect.rotation.x, mainRect.rotation.y, mainRect.rotation.z + 270f); break; case DPadArrowType.LEFT: childRect.anchoredPosition = new Vector2(childRect.sizeDelta.x / 2f, 0f); childRect.anchorMin = new Vector2(0f, .5f); childRect.anchorMax = new Vector2(0f, .5f); childRect.rotation = Quaternion.Euler(mainRect.rotation.x, mainRect.rotation.y, mainRect.rotation.z + 180f); break; case DPadArrowType.RIGHT: childRect.anchoredPosition = new Vector2(-childRect.sizeDelta.x / 2f, 0f); childRect.anchorMin = new Vector2(1f, .5f); childRect.anchorMax = new Vector2(1f, .5f); childRect.rotation = Quaternion.Euler(mainRect.rotation.x, mainRect.rotation.y, mainRect.rotation.z); break; } }