|
??????Unity?????????????????????????????????,??????????????????????????????????enums?????UI???
Enum????
?????enum?????????????????????????????????????????????????????????enum????????????7??????:Sat?Sun?Mon?Tue???????????????????????????Days.Mon?
?????????????????:
- enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
复制代码
??????(E?G:Sat?Sun?Mon)????????????????int????????????Sat?Sun?Mon??0 1 2?????????????????????????????????????????
?????Enum ?
?????????????????????????????????????????????????????????????????????????????Unity????????????????????????????5????bools?????????????????enum?????????????5?bools????????????if-????????????bug?????????????????????????????????enum???????????????????
?????????????
?????????????????;???????????????4????UI?????????????????UI?????????????????????????????????????????????????????
??????????????????????????????????????????????????????????up???????????????????????????????????????????????
?????????????????????????????????????unity????????????????2D??????????????????????????????
?????????????????????
?????????????????????:
?UI????4???????????????????????????????????????????E?G?Up????????????
?????????????????????????????????Up????????????
???????????????????????????
???????????????????18???22????????????????????????????????????????????????????????????
???????????????????????????????????????????????????????????????????????
?????????????Panel???????????????cscript???????????IDE????????
???????????:
- 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<Image>();
- imgDown = transform.FindChild("Down").GetComponent<Image>();
- imgLeft = transform.FindChild("Left").GetComponent<Image>();
- imgRight = transform.FindChild("Right").GetComponent<Image>();
-
- //Grab the Button components of all our buttons
- buttonUp = transform.FindChild("Up").GetComponent<Button>();
- buttonDown = transform.FindChild("Down").GetComponent<Button>();
- buttonLeft = transform.FindChild("Left").GetComponent<Button>();
- buttonRight = transform.FindChild("Right").GetComponent<Button>();
- }
-
- 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 things 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 things 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!");
- }
- }
复制代码
????????????????????TestMessage???????
????????OnClick??????+????????OnClick????OnClick()???????GameObject???????(Object)????
?????No?????TestMessage??()
????????????????????
??????????
????????????????????????????????????????????????????????????????????????????????????????????????????????????
??
?????????????????????????????bools???enum?????????????????????????????????????if???????????????????bools??????????????????????????????????????????????????????????
via?????
????????????????????????????????????????
|
|