简体   繁体   中英

How can I make a switch statement work with an enum? C# Unity

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

  • Please explain your answer
  • Please explain why the code is wrong
  • Please give feedback on how to improve

I'm using Unity version 5.3.5f1 and Visual Studio Community 2015

Goal

  • Make the enum work properly using either switch or if
  • Be able to use the variables inside the FadeOperations class to make the calculations inside the Test class
  • Select from an array the type of desired operation
  • Select an UI element from Heriarchy and fade it

Steps

  • Create new Unity project (2D or 3D)
  • Create UI Image
  • Create Empty game object
  • Create new C# script (I called it Test)
  • Attach new script to empty game object

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM