描述
我正在编写一个脚本来淡化Unity中的UI元素,类似于选择器,您可以在其中选择淡入淡出的类型,持续时间和淡化图像
我发现enum是实现这个结果的最佳选择,但是我有一个问题,当我运行enum工作的唯一代码而另一个不用时,无论我是否使用{{1或者switch
只是第一个语句运行,我不知道代码有什么问题
我使用Unity版本5.3.5f1和Visual Studio Community 2015
目标
if
或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;
}
}
}
(_fo.fadeType)
的结果
switch
答案 0 :(得分:2)
以下是您可能想要做的事情:
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;
}
}
}
}