简体   繁体   English

如何在一个脚本和另一个脚本中使用枚举?

[英]How can i use enum in one script and another script?

In the first script: 在第一个脚本中:

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

public class LightsEffects : MonoBehaviour
{
    public enum Directions
    {
        Forward, Backward
    };

    private Renderer[] renderers;
    private float lastChangeTime;
    private int greenIndex = 0;

    public void LightsEffect(List<GameObject> objects, Color instantiateColor, Color colorEffect)
    {
        renderers = new Renderer[objects.Count];
        for (int i = 0; i < renderers.Length; i++)
        {
            renderers[i] = objects[i].GetComponent<Renderer>();
            renderers[i].material.color = Color.red;
        }

        // Set green color to the first one
        greenIndex = 0;
        renderers[greenIndex].material.color = Color.green;
    }

    public void LightsEffectCore(float delay, bool changeDirection)
    {
        // Change color each `delay` seconds
        if (Time.time > lastChangeTime + delay)
        {
            lastChangeTime = Time.time;

            // Set color of the last renderer to red
            // and the color of the current one to green
            renderers[greenIndex].material.color = Color.red;
            if (changeDirection == true)
            {
                Array.Reverse(renderers);
                changeDirection = false;
            }
            greenIndex = (greenIndex + 1) % renderers.Length;
            renderers[greenIndex].material.color = Color.green;
        }
    }
}

In this script i want to use the enum Directions instead the bool in the core method: 在此脚本中,我想在核心方法中使用枚举Directions代替bool:

public void LightsEffectCore(float delay, bool changeDirection)

Instead bool changeDirection to use the enum to decide what direction Forward or Backward. 取而代之的是,使用change来确定枚举的前进或后退方向。

And then to use this script in another one for example in another script i did: 然后在另一个脚本中使用这个脚本,例如在另一个脚本中,我做了:

public LightsEffects lightseffect;

Then in Start: 然后在开始中:

void Start()
{
 lightseffect.LightsEffect(objects, Color.red, Color.green);
}

Then in Update i want to decide the direction: 然后在更新中,我想确定方向:

void Update()
{
  lightseffect.LightsEffectCore(0.1f, false);
}

But instead false/true i want to use also here Forward/Backward Maybe something like: 但是相反,我要在这里使用false / true前进/后退也许像这样:

lightseffect.LightsEffectCore(0.1f, lightseffect.Forward);

Since false/true not really have a meaning of what it's doing. 由于false / true并没有真正意义上的含义。

Another option is to leave it with false/true but to add a description to the method when the user typing: lightseffect.LightsEffectCore( 另一个选择是将其保留为false / true,但在用户键入时在方法中添加说明:lightseffect.LightsEffectCore(

When he type ( he will see a description of what the method do and when he will type , It will tell him what the false/true will do. But i'm not sure how to make a description for that. 当他键入时(他会看到该方法的描述以及何时键入,它将告诉他该false / true会做什么。但是我不确定如何对此进行描述。

Directions is the name of the enum you're interested in. It's also contained within the LightsEffects class. Directions是您感兴趣的枚举的名称。它也包含在LightsEffects类中。

Thus the way to reference it somewhere else would be LightsEffects.Directions and then another dot and then the value ( Forward ,etc). 因此,在其他地方引用它的方式将是LightsEffects.Directions ,然后是另一个点,然后是值( Forward等)。 This is similar to a Fully Qualified Name , but with part of that scope already declared (whatever import/using that gets you LightsEffects ). 这类似于“ 完全限定名称” ,但是已经声明了该范围的一部分(无论使用什么导入/使用,都可以获取LightsEffects )。

So your line: 所以你的线:

lightseffect.LightsEffectCore(0.1f, lightseffect.Forward);

Becomes: 变为:

lightseffect.LightsEffectCore(0.1f, LightsEffects.Directions.Forward);

Depending on your IDE, it should even be able to resolve this for you if you had used Directions.Forward : your IDE would have noticed the error (unknown reference Directions) and suggested a fix (change to LightsEffects.Directions), I know that Visual Studio does this, as I have a similar case in a project I'm working on now. 根据您的IDE,如果您使用过Directions.Forward ,它甚至应该能够为您解决此问题:您的IDE会注意到该错误(未知的Directions)并提出了修复建议(更改为LightsEffects.Directions),我知道Visual Studio会这样做,因为我正在研究的项目中也有类似的案例。

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

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