繁体   English   中英

在Unity C#中的类外枚举

[英]Enum outside class in Unity C#

我正在创建一个脚本来管理Unity3D中UI元素的淡入,有时我会陷入枚举

前几天,我看到了一种非常酷的资产来淡化元素,并决定将其反转生成

经过一段时间的研究和阅读本页面中的问题之后,我陷入了使用枚举的困境,我不知道如何从另一个类访问枚举,所以我想寻求帮助

我正在使用Unity版本5.3.5f1

目标

  • 在Unity中淡化UI元素(图像,按钮等)
  • 通过脚本淡入
  • 使用枚举做到这一点
  • 课外的枚举

如何复制

  • 新的Unity项目(与2D或3D无关)

  • 空的游戏对象

  • UI元素(图像)

  • 图像填充屏幕并显示其颜色(任何颜色)

  • 新的C#脚本,我称它为Test

完成

  • 阅读有关枚举的信息
  • 基本类与枚举

代码C#

这是我的代码(到目前为止)

using UnityEngine;
using UnityEngine.UI;

[System.Serializable]
public class FadeOperations
{
   public enum FadeManager
{
    fadeIn,
    fadeOut
};

  [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
{
  //Where do I acces the enum inside this class??

  //This is the variable for the inspector to see the elements inside the other class
  public FadeOperations[] fadeOperations;

  private void Start()
  {
  }
}

我很高兴阅读良好的解释和友好的答案

谢谢

从技术上讲,如果您在类内声明一个枚举,则该枚举将充当嵌套类。

因此,使用当前的代码库,您需要在FadeOperations之外引用FadeManager,如下所示:

public class Test : MonoBehaviour
{
  // the variable of FadeManager type outside FadeOperations
  public FadeOperations.FadeManager fadeManager;

  public FadeOperations[] fadeOperations;

  private void Start()
  {
  }
}

但是,您可能会发现将枚举移到FadeOperations类之外更为实用:

public enum FadeManager
{
    fadeIn,
    fadeOut
};

[System.Serializable]
public class FadeOperations
{
    //FadeOperations body goes here...
}

然后,您可以直接在FadeOperations和其他类中按其名称访问FadeManager类。

由您决定哪个更适合您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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