找回密码
 立即注册
查看: 306|回复: 0

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

[复制链接]
发表于 2022-1-10 15:40 | 显示全部楼层 |阅读模式
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SkillInput : MonoBehaviour
{
[SerializeField]
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!");
}
}
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2025-5-15 03:42 , Processed in 0.132008 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表