Question by
KevinDasPikachu · Mar 01, 2020 at 06:43 PM ·
c#scripting problembuttonbuttonsautomatic
How to create buttons with script?
I want to create Buttons for my Level Selector automatically. I use the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Levels : MonoBehaviour
{
Button locked;
Button unlocked;
Rect rc;
private GUIStyle guiStyle = new GUIStyle();
public Texture lockedTexture;
public Texture unlockedTexture;
public int levelAmount;
int level = 1;
void Start()
{
}
void Update()
{
}
void OnGUI()
{
for (int i = 0; i <= levelAmount; i++)
{
print(getRect(i));
if (i <= level)
{ // entsperrt
GUI.Button(getRect(i), unlockedTexture);
}
else
{ // nicht ensperrt
GUI.Button(getRect(i), lockedTexture);
}
}
}
Rect getRect(int currentLevel)
{
int Height = currentLevel * 210;
int Width = currentLevel * 210;
int page = 0;
int temp = (currentLevel - page) / 8 ;
if(temp >= 0 && temp < 1)
{
rc = new Rect(-752 + Width, 296 + Height, 10, 10);
}
else if(temp >= 1 && temp < 2)
{
rc = new Rect(-752 + Width, 296 + Height, 10, 10);
}
else if (temp >= 2 && temp < 3)
{
rc = new Rect(-752 + Width, 296 + Height, 10, 10);
}
else if (temp >= 3 && temp < 4)
{
rc = new Rect(-752 + Width, 296 + Height, 10, 10);
}
else if(temp > 4)
{
page = currentLevel;
}
return rc;
}
}
Comment
Your answer
Follow this Question
Related Questions
How to change a button image with script? 1 Answer
help to connect two objects together 0 Answers
How can I start a Coroutine with the onClick of a button? 0 Answers
OnClick() event script from a prefab 0 Answers
button.onClick.AddListener in for loop 2 Answers