[英]How can I make a switch statement work with an enum? C# Unity
描述
我正在使用腳本來淡化Unity中的UI元素,類似於選擇器,您可以在其中選擇淡出的類型,持續時間和要淡入的圖像
我發現,枚舉是實現這一結果的最佳選擇,但我有一個問題,當我運行的代碼只有枚舉工作的元素和其他不這樣做,無論我是否使用switch
或者if
只是第一個語句運行,我不知道代碼有什么問題
我正在使用Unity版本5.3.5f1和Visual Studio Community 2015
目標
switch
或if
腳步
碼
這是我的代碼...
using UnityEngine;
using UnityEngine.UI;
public enum FadeManager
{
fadeIn,
fadeOut
};
[System.Serializable]
public class FadeOperations
{
[Tooltip("Type of fading")]
public FadeManager fadeType;
[Tooltip("Duration time of the fading")]
public float duration;
[Tooltip("Select the image to fade")]
public Image fadeImage;
}
public class Test : MonoBehaviour
{
[Tooltip("Select your type of fade")]
public FadeOperations[] fadeOperations;
//Reference to the class FadeOperations
private FadeOperations _fo = new FadeOperations();
//Loop for debug
private void Start()
{
Debug.Log(_fo.fadeType);
switch (_fo.fadeType)
{
//This statement works
case FadeManager.fadeIn:
Debug.Log("Fadein"); //Only this piece of code works
break;
//This statement doesn't work
case FadeManager.fadeOut:
Debug.Log("Fadeout");
break;
}
}
}
switch
前的Log (_fo.fadeType)
結果
fadeIn
UnityEngine.Debug:Log(Object)
Test:Start() (at Assets/_Scripts/Test.cs:34)
您可能要執行以下操作:
public class Test : MonoBehaviour
{
[Tooltip("Select your type of fade")]
public FadeOperations[] fadeOperations;
//Loop for debug X NOTE: Start method runs only one time.dont expect it to run it for multiple time
private void Start()
{
foreach(var operation in fadeOperations)
{
Debug.Log(operation.fadeType);
switch (operation.fadeType)
{
case FadeManager.fadeIn:
Debug.Log("Fadein"); // write your fading in code here
break;
case FadeManager.fadeOut:
Debug.Log("Fadeout"); // write your fading out code here
break;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.