Description
I'm working on a script to fade UI elements in Unity, similar to a selector, where you can select the type of fading, and duration, and image to fade
I found that enum is the best option to achieve that result, but I have a problem, when I run the code only element of the enum work and the other don't, no matter if I use switch
or if
just the first statement run, I don't know what's wrong with the code
I'm using Unity version 5.3.5f1 and Visual Studio Community 2015
Goal
switch
or if
Steps
Code
Here's my code...
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;
}
}
}
The result of the Log (_fo.fadeType)
before the switch
fadeIn
UnityEngine.Debug:Log(Object)
Test:Start() (at Assets/_Scripts/Test.cs:34)
Here is something you may want to do:
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;
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.