【问题标题】:How to reference an enum from another script in Unity如何从 Unity 中的另一个脚本引用枚举
【发布时间】:2020-11-02 08:31:12
【问题描述】:

所以,我一直在尝试访问名为“ModulesList”的脚本中的枚举类。我尝试了以下方法:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ModuleIdentifier : MonoBehaviour
{
    public string CurrentSongModule;
    ModulesList moduleList = new ModulesList();

    // Start is called before the first frame update
    void Start()
    {

    }
}

但它不起作用。如何引用此枚举?

【问题讨论】:

  • 请提供定义枚举的代码。你是什​​么意思“它不起作用”?你有什么样的错误?
  • 不要将枚举视为类,将它们视为带有名称的数字。像数字一样使用它们,例如MyEnumType variableName = MyEnumType.NamedValue;

标签: c# unity3d enums


【解决方案1】:

所以从你的班级ModuleList 我将提供一个示例枚举,因为你没有说明这些枚举是什么

假设你的枚举像这样在 ModuleList 中

class ModuleList:MonoBehaviour
{

public enum YourEnum
     {
         None, SampleA, SampleB;
     }

}

那么这就是你从ModuleIdentifier 脚本中调用它的方式

class ModuleIdentifier : MonoBehaviour
{

    ModuleList ML = new ModuleList();
    if(ML.YourEnum == ML.None)
    {
        //do something
    }
}

【讨论】:

  • 我在两个脚本中有一个匹配的枚举。在脚本 A 中,我想将其设置为脚本 B 中的枚举选择
  • 超时...我按照这个例子到了 T。我在两个脚本中有一个匹配的枚举。 1 :) 在脚本 A 中,我想将其设置为脚本 B 中的枚举选择。'Aenum = m_ScriptComponent.B;'给出一个必须强制转换的错误;当我想要变量时,我一直在尝试使用类型。
猜你喜欢
  • 1970-01-01
  • 2015-04-12
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-27
  • 2013-08-26
  • 1970-01-01
相关资源
最近更新 更多