Untitled
unknown
plain_text
a year ago
4.1 kB
12
Indexable
using UnityEngine;
using System.Collections;
public class SlotMachine : MonoBehaviour
{
public GameObject[] slot1Objects; // Array for the first slot's GameObjects
public GameObject[] slot2Objects; // Array for the second slot's GameObjects
public GameObject[] slot3Objects; // Array for the third slot's GameObjects
public GameObject[] resultObjects; // Array for special GameObjects corresponding to results 0-0-0, 1-1-1, etc.
public float spinDuration = 3.0f; // Duration of the spin in seconds
public float interval = 0.1f; // Interval between switching GameObjects during the spin
public int specialOccurrenceInterval = 5; // The interval for triggering a special result
private int pressCount = 0; // Tracks the number of times the button is pressed
void Start()
{
// Initialize press count
pressCount = 0;
}
public void StartSlotMachine()
{
pressCount++;
StartCoroutine(SpinSlotMachine());
}
IEnumerator SpinSlotMachine()
{
float elapsedTime = 0f;
while (elapsedTime < spinDuration)
{
// Randomly activate one GameObject in each slot and deactivate the others
RandomizeSlot(slot1Objects);
RandomizeSlot(slot2Objects);
RandomizeSlot(slot3Objects);
// Wait for the interval time before switching again
yield return new WaitForSeconds(interval);
// Increment the elapsed time
elapsedTime += interval;
}
// After spinning, finalize with a random GameObject active in each slot
int finalIndex1;
int finalIndex2;
int finalIndex3;
// If press count matches the special occurrence interval, trigger special result
if (pressCount % specialOccurrenceInterval == 0)
{
int specialIndex = (pressCount / specialOccurrenceInterval - 1) % slot1Objects.Length;
finalIndex1 = FinalizeSlot(slot1Objects, specialIndex);
finalIndex2 = FinalizeSlot(slot2Objects, specialIndex);
finalIndex3 = FinalizeSlot(slot3Objects, specialIndex);
// Trigger the corresponding special object based on the pattern
TriggerSpecialObject(specialIndex);
}
else
{
// Otherwise, randomize the final selection
finalIndex1 = FinalizeSlot(slot1Objects);
finalIndex2 = FinalizeSlot(slot2Objects);
finalIndex3 = FinalizeSlot(slot3Objects);
}
}
void RandomizeSlot(GameObject[] slotObjects)
{
// Deactivate all objects in the slot
foreach (GameObject obj in slotObjects)
{
obj.SetActive(false);
}
// Randomly select one object to activate
int randomIndex = Random.Range(0, slotObjects.Length);
slotObjects[randomIndex].SetActive(true);
}
int FinalizeSlot(GameObject[] slotObjects, int specificIndex = -1)
{
// Ensure all objects are deactivated first
foreach (GameObject obj in slotObjects)
{
obj.SetActive(false);
}
// If a specific index is provided, use it, otherwise randomize
int finalIndex = specificIndex == -1 ? Random.Range(0, slotObjects.Length) : specificIndex;
// Activate the object at the final index
slotObjects[finalIndex].SetActive(true);
// Return the index of the activated object
return finalIndex;
}
void TriggerSpecialObject(int index)
{
// Ensure all special objects are deactivated first
foreach (GameObject obj in resultObjects)
{
obj.SetActive(false);
}
// Activate the special object corresponding to the result (e.g., 0-0-0, 1-1-1, etc.)
if (index >= 0 && index < resultObjects.Length)
{
resultObjects[index].SetActive(true);
}
}
}
Editor is loading...
Leave a Comment