游戏开发论坛

 找回密码
 立即注册
搜索
查看: 9099|回复: 0

Unity??:???????????????

[复制链接]

5万

主题

5万

帖子

8万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
87302
发表于 2017-8-18 13:32:07 | 显示全部楼层 |阅读模式
??????Unity?????????????????????????????????,??????????????????????????????????enums?????UI???

Enum????

?????enum?????????????????????????????????????????????????????????enum????????????7??????:Sat?Sun?Mon?Tue???????????????????????????Days.Mon?

?????????????????:

  1. 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?????????????????????????????????????????????????????

1.jpg

??????????????????????????????????????????????????????????up???????????????????????????????????????????????

?????????????????????????????????????unity????????????????2D??????????????????????????????

?????????????????????

?????????????????????:

2.png

?UI????4???????????????????????????????????????????E?G?Up????????????

3.png

?????????????????????????????????Up????????????

4.png

???????????????????????????

???????????????????18???22????????????????????????????????????????????????????????????

5.png

???????????????????????????????????????????????????????????????????????

?????????????Panel???????????????cscript???????????IDE????????

???????????:

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;

  4. public class SkillInput : MonoBehaviour
  5. {

  6. [SerializeField]
  7. float fadeRate = 4f; //Used to adjust image fade speed

  8. enum Selection { None, Up, Down, Left, Right }; //Will be used to keep track of what's selected
  9. Selection currentSel; // Create a Selection object that will be used throughout script

  10. Image imgUp, imgDown, imgLeft, imgRight; //These variables will be used for fading the buttons when selected
  11. Button buttonUp, buttonDown, buttonLeft, buttonRight; //Will be used to invoke Button functions

  12. void Start()
  13. {
  14. currentSel = Selection.None; //assign currentSel to None.

  15. //Grab the Image components of all our buttons
  16. imgUp = transform.FindChild("Up").GetComponent<Image>();
  17. imgDown = transform.FindChild("Down").GetComponent<Image>();
  18. imgLeft = transform.FindChild("Left").GetComponent<Image>();
  19. imgRight = transform.FindChild("Right").GetComponent<Image>();

  20. //Grab the Button components of all our buttons
  21. buttonUp = transform.FindChild("Up").GetComponent<Button>();
  22. buttonDown = transform.FindChild("Down").GetComponent<Button>();
  23. buttonLeft = transform.FindChild("Left").GetComponent<Button>();
  24. buttonRight = transform.FindChild("Right").GetComponent<Button>();
  25. }

  26. void Update()
  27. {
  28. //Standard input calls.
  29. if (Input.GetButtonDown("Up"))
  30. {
  31. if (currentSel == Selection.Up)
  32. {
  33. //Executes if we already have up selected and user presses up again
  34. buttonUp.onClick.Invoke(); //Call up button's OnClick() function
  35. currentSel = Selection.None; //set currentSel back to None
  36. }
  37. else
  38. {
  39. currentSel = Selection.Up; // changes currentSel to Up.
  40. StartCoroutine(FadeIcon(imgUp, currentSel)); //Begins fading the icon
  41. }
  42. }
  43. //The same code pattern from above is repeated for the rest of the inputs
  44. else if (Input.GetButtonDown("Down"))
  45. {
  46. if (currentSel == Selection.Down)
  47. {
  48. buttonDown.onClick.Invoke();
  49. currentSel = Selection.None;
  50. }
  51. else
  52. {
  53. currentSel = Selection.Down;
  54. StartCoroutine(FadeIcon(imgDown, currentSel));
  55. }
  56. }
  57. else if (Input.GetButtonDown("Left"))
  58. {
  59. if (currentSel == Selection.Left)
  60. {
  61. buttonLeft.onClick.Invoke();
  62. currentSel = Selection.None;
  63. }
  64. else
  65. {
  66. currentSel = Selection.Left;
  67. StartCoroutine(FadeIcon(imgLeft, currentSel));
  68. }
  69. }
  70. else if (Input.GetButtonDown("Right"))
  71. {
  72. if (currentSel == Selection.Right)
  73. {
  74. buttonRight.onClick.Invoke();
  75. currentSel = Selection.None;
  76. }
  77. else
  78. {
  79. currentSel = Selection.Right;
  80. StartCoroutine(FadeIcon(imgRight, currentSel));
  81. }
  82. }
  83. }

  84. IEnumerator FadeIcon(Image img, Selection sel)
  85. {
  86. //basic Fade Coroutine. For more Information:
  87. //https://www.studica.com/blog/create-a-fading-splash-screen-using-coroutines-in-unity-3d
  88. float alpha = 1f;

  89. while (currentSel == sel)
  90. {
  91. while (img.color.a > 0)
  92. {
  93. alpha -= Time.deltaTime * fadeRate;
  94. img.color = new Color(img.color.r, img.color.g, img.color.b, alpha);
  95. yield return null;
  96. }
  97. while (img.color.a < 1)
  98. {
  99. alpha += Time.deltaTime * fadeRate;
  100. img.color = new Color(img.color.r, img.color.g, img.color.b, alpha);
  101. yield return null;
  102. }
  103. yield return null;
  104. }
  105. img.color = new Color(img.color.r, img.color.g, img.color.b, 1f);
  106. }
  107. }

  108. 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:
  109. using UnityEngine;
  110. using System.Collections;

  111. public class TestMessage : MonoBehaviour {

  112. void Start ()
  113. {

  114. }

  115. void Update ()
  116. {

  117. }

  118. public void Testing()
  119. {
  120. Debug.Log("Test Succeeded!");
  121. }
  122. }


  123. //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:
  124. using UnityEngine;
  125. using System.Collections;

  126. public class TestMessage : MonoBehaviour {

  127. void Start ()
  128. {

  129. }

  130. void Update ()
  131. {

  132. }

  133. public void Testing()
  134. {
  135. Debug.Log("Test Succeeded!");
  136. }
  137. }
复制代码

????????????????????TestMessage???????

????????OnClick??????+????????OnClick????OnClick()???????GameObject???????(Object)????

6.png

?????No?????TestMessage??()

7.jpg

????????????????????

??????????

????????????????????????????????????????????????????????????????????????????????????????????????????????????

??

?????????????????????????????bools???enum?????????????????????????????????????if???????????????????bools??????????????????????????????????????????????????????????

via?????

????????????????????????????????????????

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-2-24 18:25

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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