- Home /
Instantiate buttons , each with a unique positioning
How could I Instantiate level buttons (like 80 ), each with a unique positioning? I need to set the position manually for each of them. (i know is weird, but this is the desing)
I know that I would need empty gameObjects for each position, but I don't Know how to put this in the Instantiate code. So if anybody has any suggestion, I would really appreciate !
Thank you!!
Answer by tanoshimi · Apr 15, 2016 at 02:50 PM
Position is the second parameter you pass to Instantiate...
Instantiate(UniqueButtonPrefab, new Vector3(UniqueButtonX, UniqueButtonY, UniqueButtonZ), Quaternion.identity);
http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
Thanks for answering! @tanoshimi I was wondering if there is a method to do this for all the button, and not write code for each. I detailed a bit more in the comment above. I would like to see your approach, because I am a self taught C# and It would help me a lot.
Of course.
// populate these via the inspector
public List<GameObject> Buttons;
public List<Vector3> Positions;
for (int i=0; i<ListOfButtons.Count; i++){
Instantiate(Buttons[i], Positions[i], Quaternion.identity);
}
tanoshimi method works and will be efficient if you have a few buttons to set up.
But you said in your question that you had something like 80 buttons ? Populate your arrays with the inspector is not a good idea. You should define some rules that allow you to "calculate" the properties of the button you are creating.
Answer by Blue-Cut · Apr 15, 2016 at 03:31 PM
Hello,
What you need to have is a class (a script) that is in charge of instantiating a button. This class knows a prefab that is your button model :
public class ButtonMaker : MonoBehaviour {
public Prefab buttonModel;
public void CreateButton()
{
RectTransform myButton = Instantiate(buttonModel as RectTransform);
}
}
Call the CreateButton method for each button you need to create.
Then you put the properties of the button like you want. Complete the ButtonMaker class to be able to set the properties you need as you like.
For example, put the ButtonMaker on the GameObject parent of all your buttons then do :
myButton.SetParent (transform, false);
Thanks for answering! @Blue Cut I was wondering if there is a way to make this without writing code for every button.Is is possible to have a List<> where i would store all the positions, and have in Instantiate:
if(spawnPosition.ID = button$$anonymous$$odel.ID) { Instantiate(button$$anonymous$$odel, spawnPosition.position) }
Yes, you absolutely can. If your button are positionned in a very "precise and not calculable" way, you can do :
public Vector3[] positions;
Then complete the positions in the inspector. But if it's possible in your case, it would be more efficient to calculate the next position depending on the previous one. Imagine that your next button is always at x+10 from the previous button :
public class Button$$anonymous$$aker : $$anonymous$$onoBehaviour {
public Prefab button$$anonymous$$odel;
public int buttonCount = 80;
public Vector3 nextPos = Vector3.zero;
public int spacingX = 10;
void Start()
{
for(int i=0; i<buttonCount; i++)
{
CreateButton();
}
}
public void CreateButton()
{
RectTransform myButton = Instantiate(button$$anonymous$$odel as RectTransform);
myButton.SetParent(transform, false);
myButton.localPosition = nextPos;
nextPos.x += spacingX;
}
}
I am not sure if this completly answer your question, because I am not considering the "id" your talking about. Precise a bit more what you want to do if it's not helping ;)
Yes, this is what I was looking for.Thank you for the detailed answer! It helped me a lot!
Your answer
Follow this Question
Related Questions
Destroying assets is not permitted to avoid data loss. 0 Answers
Instantiated GameObjects scale down based on screen size. (Mobile) 0 Answers
My objects instantiate at a strange z coordinate 0 Answers
How to manipulate a variable of a prefab script (instantiated) while the game is runnning . 1 Answer
Distribute terrain in zones 3 Answers