enum中使用中文 unity_Unity教程:如何使用枚举来帮助简化游戏开发

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!");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值