Galaxy Generator
This commit is contained in:
124
Assets/GalaxyContainer.cs
Normal file
124
Assets/GalaxyContainer.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class GalaxyContainer : MonoBehaviour
|
||||
{
|
||||
public GameObject[] objects;
|
||||
public GameObject galaxyPrefab; // Prefab des Galaxy-Objekts
|
||||
public int numberOfGalaxies; // Anzahl der zu erstellenden Galaxien
|
||||
public float distanceFromCenter; // Abstand der Galaxien vom Hauptobjekt
|
||||
|
||||
void Start()
|
||||
{
|
||||
Vector3 centerPosition = transform.position; // Position des Hauptobjekts
|
||||
|
||||
// Berechne die Anzahl der Zeilen und Spalten basierend auf der Anzahl der Galaxien
|
||||
int rows = Mathf.CeilToInt(Mathf.Sqrt(numberOfGalaxies));
|
||||
int columns = Mathf.CeilToInt((float)numberOfGalaxies / rows);
|
||||
|
||||
// Berechne den Abstand zwischen den Galaxien auf jeder Achse
|
||||
float xSpacing = distanceFromCenter / (columns - 1);
|
||||
float ySpacing = distanceFromCenter / (rows - 1);
|
||||
|
||||
// Erstelle alle Galaxien
|
||||
for (int i = 0; i < numberOfGalaxies; i++)
|
||||
{
|
||||
// Berechne die x- und y-Position basierend auf dem Index
|
||||
int rowIndex = i % rows;
|
||||
int columnIndex = i / rows;
|
||||
|
||||
// Berechne die Position basierend auf den Indizes und den Abst<73>nden
|
||||
float xPos = centerPosition.x - (distanceFromCenter / 2f) + columnIndex * xSpacing;
|
||||
float yPos = centerPosition.y - (distanceFromCenter / 2f) + rowIndex * ySpacing;
|
||||
|
||||
// Erzeuge ein neues GameObject aus dem Prefab
|
||||
GameObject galaxy = Instantiate(galaxyPrefab);
|
||||
|
||||
// Setze die Position des Galaxy-Objekts
|
||||
galaxy.transform.position = new Vector3(xPos, yPos, centerPosition.z);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*void Start()
|
||||
{
|
||||
for (int i = 0; i < objectsToDeactivate0.Length; i++)
|
||||
{
|
||||
objectsToDeactivate0[i].SetActive(false); // Deactivate the game object
|
||||
}
|
||||
|
||||
// Erstelle f<>r jeden Button ein Raw Image
|
||||
for (int i = 0; i < planetTextures.Length; i++)
|
||||
{
|
||||
// Erzeuge ein neues GameObject f<>r den Button
|
||||
GameObject buttonGO = new GameObject("Button" + i);
|
||||
buttonGO.transform.SetParent(transform);
|
||||
|
||||
// F<>ge das Raw Image Komponente hinzu
|
||||
RawImage rawImage = buttonGO.AddComponent<RawImage>();
|
||||
|
||||
// Weise die Planeten-Textur dem Raw Image zu
|
||||
rawImage.texture = planetTextures[i];
|
||||
|
||||
// Setze die Position des Raw Images entsprechend der Button-Position
|
||||
buttonGO.transform.localPosition = new Vector3(buttonPositions[i].x, buttonPositions[i].y, 0f);
|
||||
|
||||
// F<>ge eine Button-Komponente hinzu
|
||||
Button button = buttonGO.AddComponent<Button>();
|
||||
|
||||
// F<>ge einen EventListener hinzu, um die Funktion OnButtonClick aufzurufen
|
||||
int buttonIndex = i; // Speichere den Index in einer separaten Variable, um den richtigen Index im EventListener zu verwenden
|
||||
button.onClick.AddListener(() => OnButtonClick(buttonIndex));
|
||||
|
||||
// Skaliere den Button
|
||||
RectTransform buttonRectTransform = buttonGO.GetComponent<RectTransform>();
|
||||
buttonRectTransform.localScale = buttonScale;
|
||||
}
|
||||
}
|
||||
|
||||
void OnButtonClick(int buttonIndex)
|
||||
{
|
||||
// Diese Methode wird aufgerufen, wenn ein Button geklickt wird
|
||||
Debug.Log("Button " + buttonIndex + " wurde geklickt!");
|
||||
if (buttonIndex == 0)
|
||||
{
|
||||
for (int i = 0; i < objectsToDeactivate1.Length; i++)
|
||||
{
|
||||
objectsToDeactivate1[i].SetActive(false); // Deactivate the game object
|
||||
}
|
||||
for (int i = 0; i < objectsToDeactivate0.Length; i++)
|
||||
{
|
||||
objectsToDeactivate0[i].SetActive(true); // activate the game object
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
public void GoBack()
|
||||
{
|
||||
// Diese Methode wird aufgerufen, wenn Back Button geklickt wird
|
||||
for (int i = 0; i < objectsToDeactivate1.Length; i++)
|
||||
{
|
||||
objectsToDeactivate1[i].SetActive(true); // activate the game object
|
||||
}
|
||||
for (int i = 0; i < objectsToDeactivate0.Length; i++)
|
||||
{
|
||||
objectsToDeactivate0[i].SetActive(false); // deactivate the game object
|
||||
}
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user