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 ?

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.

finally I found the problem :slight_smile:

I convert the RectTransform to GameObject and

_content.localScale = new Vector2(1f,1f);

to

_content.GetComponent().localScale = CloneButton.GetComponent().localScale;