- Home /
how can i clone ui button ?
hi...
I searched a lot on the internet and found many answers , but noting work for me
please how can fix this problem ..
I mad a prefab contain Panel and in the panel there is an image and text
by using this code I clone the button
RectTransform _content;
_content = Instantiate( CloneButton , new Vector2(0,0) ,transform.rotation ) as RectTransform;
_content.parent = transform;
_content.anchoredPosition = new Vector2 (0,-(ContentHeight + ContentSpace) * currentCount + Start_PosY );
_content.name = "Button";
_content.localScale = new Vector2(1f,1f);
_content.GetComponent<Button>().onClick.AddListener(() => ButtonPressed_ImageVideo(info) );
the button will clone and if I press on it , will work and every things OK except ,
the image show good , but the text show out of the UI rectangle
List item
no color for the panel and the button ( just gray ) and if I at run time , in the editor change the color , nothing will happen
what should I do ?
Answer by Johnz1234 · Jun 19, 2015 at 05:07 PM
public GameObject myUiButton; public Transform myPosition; void Start() { Instantiate(myUiButton,myPosition.position,myPosition.rotation); } } make simple ui image delete the image renderer then rename it as spawnPoint- now add script to the camera ,drag spawnPoint in myPosition field and your ui button as prefab this shoud work.
I have to put the new button child of other object , how can I do that ? , because now appearing out of canvas
Answer by Fadi · Jun 19, 2015 at 06:20 PM
finally I found the problem :)
I convert the RectTransform to GameObject and
_content.localScale = new Vector2(1f,1f);
to
_content.GetComponent().localScale = CloneButton.GetComponent().localScale;