static void CreatRawImage() { if (Selection.activeTransform) { if (Selection.activeTransform.GetComponentInParent <Canvas>()) { GameObject go = new GameObject("ImageNum", typeof(RectTransform)); go.transform.SetParent(Selection.activeTransform); go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.Euler(0, 0, 0); go.transform.localScale = Vector3.one; go.layer = LayerMask.NameToLayer("UI"); Image baseImg = UIRaycastTarget.CreatImage(); baseImg.transform.parent = go.transform; baseImg.transform.name = "defalut"; ImageNum imageNum = go.AddComponent <ImageNum>(); imageNum.baseImg = baseImg; imageNum.numCount = 1; GridLayoutGroup grid = go.AddComponent <GridLayoutGroup>(); grid.childAlignment = TextAnchor.MiddleCenter; } } }
static Image InstantiateImage(Sprite sprite, string name) { Image img = UIRaycastTarget.CreatImage(); if (img) { img.sprite = sprite; img.name = name; img.SetNativeSize(); return(img); } return(null); }