BlaXuan 发表于 2022-1-10 15:40

Unity教程:如何使用枚举来帮助简化游戏开发

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SkillInput : MonoBehaviour
{

float fadeRate = 4f; //Used to adjust image fade speed
enum Selection { None, Up, Down, Left, Right }; //Will be used to keep track of what's selected
Selection currentSel; // Create a Selection object that will be used throughout script
Image imgUp, imgDown, imgLeft, imgRight; //These variables will be used for fading the buttons when selected
Button buttonUp, buttonDown, buttonLeft, buttonRight; //Will be used to invoke Button functions
void Start()
{
currentSel = Selection.None; //assign currentSel to None.
//Grab the Image components of all our buttons
imgUp = transform.FindChild("Up").GetComponent();
imgDown = transform.FindChild("Down").GetComponent();
imgLeft = transform.FindChild("Left").GetComponent();
imgRight = transform.FindChild("Right").GetComponent();
//Grab the Button components of all our buttons
buttonUp = transform.FindChild("Up").GetComponent();
buttonDown = transform.FindChild("Down").GetComponent();
buttonLeft = transform.FindChild("Left").GetComponent();
buttonRight = transform.FindChild("Right").GetComponent();
}
void Update()
{
//Standard input calls.
if (Input.GetButtonDown("Up"))
{
if (currentSel == Selection.Up)
{
//Executes if we already have up selected and user presses up again
buttonUp.onClick.Invoke(); //Call up button's OnClick() function
currentSel = Selection.None; //set currentSel back to None
}
else
{
currentSel = Selection.Up; // changes currentSel to Up.
StartCoroutine(FadeIcon(imgUp, currentSel)); //Begins fading the icon
}
}
//The same code pattern from above is repeated for the rest of the inputs
else if (Input.GetButtonDown("Down"))
{
if (currentSel == Selection.Down)
{
buttonDown.onClick.Invoke();
currentSel = Selection.None;
}
else
{
currentSel = Selection.Down;
StartCoroutine(FadeIcon(imgDown, currentSel));
}
}
else if (Input.GetButtonDown("Left"))
{
if (currentSel == Selection.Left)
{
buttonLeft.onClick.Invoke();
currentSel = Selection.None;
}
else
{
currentSel = Selection.Left;
StartCoroutine(FadeIcon(imgLeft, currentSel));
}
}
else if (Input.GetButtonDown("Right"))
{
if (currentSel == Selection.Right)
{
buttonRight.onClick.Invoke();
currentSel = Selection.None;
}
else
{
currentSel = Selection.Right;
StartCoroutine(FadeIcon(imgRight, currentSel));
}
}
}
IEnumerator FadeIcon(Image img, Selection sel)
{
//basic Fade Coroutine. For more Information:
//https://www.studica.com/blog/create-a-fading-splash-screen-using-coroutines-in-unity-3d
float alpha = 1f;
while (currentSel == sel)
{
while (img.color.a > 0)
{
alpha -= Time.deltaTime * fadeRate;
img.color = new Color(img.color.r, img.color.g, img.color.b, alpha);
yield return null;
}
while (img.color.a < 1)
{
alpha += Time.deltaTime * fadeRate;
img.color = new Color(img.color.r, img.color.g, img.color.b, alpha);
yield return null;
}
yield return null;
}
img.color = new Color(img.color.r, img.color.g, img.color.b, 1f);
}
}
Now, we need to setup some OnClick functionality for our buttons. First thing’s first, create a new C# Script called “TestMessage”. Open up the script and copy this code into it:
using UnityEngine;
using System.Collections;
public class TestMessage : MonoBehaviour {
void Start ()
{
}
void Update ()
{
}
public void Testing()
{
Debug.Log("Test Succeeded!");
}
}
//Now, we need to setup some OnClick functionality for our buttons. First thing’s first, create a new C# Script called “TestMessage”. Open up the script and copy this code //into it:
using UnityEngine;
using System.Collections;
public class TestMessage : MonoBehaviour {
void Start ()
{
}
void Update ()
{
}
public void Testing()
{
Debug.Log("Test Succeeded!");
}
}
页: [1]
查看完整版本: Unity教程:如何使用枚举来帮助简化游戏开发