【问题标题】:How can I access the Unity 'Light 2D (Script)' component via Script?如何通过脚本访问 Unity 'Light 2D (Script)' 组件?
【发布时间】:2019-09-26 16:19:02
【问题描述】:

在我的 Unity 2D 游戏中,我有一个带有光源组件的角色 Lightweight RP 包中的“Light 2D(Script)”。

我想用下面的代码改变 Light 2D 的强度。 但我无法在 Unity Inspector 面板中将“Light 2D(Script)”分配给 public Light LightSource

我尝试过使用 public Light2D LightSource 类,但它似乎不存在。

还有其他方法可以访问 2D Light 组件还是我做错了什么? 如果有帮助,我还添加了 Inspector 窗格的屏幕截图。

如果您需要更多信息,请告诉我,我希望有人可以提供帮助。谢谢

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


public class Lighting : MonoBehaviour
{
    public Light LightSource;
    public float lightIntensity;
    public float minIntensity = 0.35f, maxIntensity = 0.65f;



    void Update()
    {
        lightIntensity = Random.Range(minIntensity, maxIntensity);
        LightSource.intensity = lightIntensity;
    }
}


Character Inspector Screenshot

【问题讨论】:

  • 你有什么错误,你在编辑器中连接了Light吗?
  • 您的问题不是很清楚您在场景中实际拥有什么。截图会有很大帮助。无论如何尝试将行从public Light LightSource;更改为public Light2D LightSource;,然后在检查器中将Light2D组件拖到它上面。
  • 是的,可能是通过使用正确的字段类型...虽然我不知道任何 Light2D 类,它也没有出现在 API 中。你从哪里得到的?
  • @derHugo 我也不确定,但我假设它可能是the LWRP Light2D Component
  • @Ruzihm 或 this ;) 但是是的,你的接缝更近了

标签: c# unity3d 2d lighting


【解决方案1】:

假设您使用 2D 照明的通用渲染管道,这是我找到的解决方案。

在 Light2D 脚本中(您可以通过单击 Light2D 组件右上角的三个垂直点并点击编辑脚本来打开该脚本),其中声明了命名空间 UnityEngine.Experimental.Rendering.Universal 以引用 Light2D只需在脚本的顶部编写

using UnityEngine.Experimental.Rendering.Universal;

执行此操作时可能会出现错误,但请按。从这里开始,您将能够像引用任何其他组件一样引用 Light2D 组件,例如使用 GetComponent<Light2D>() 如果其中任何一个在您的代码编辑器中产生错误仍然编译以测试它是否有效,我有一个使用此方法的功能齐全的脚本,但 Visual Studio 仍然认为 Light2D 不存在。无论如何,这似乎有效,我希望这也适用于其他所有人。

【讨论】:

    【解决方案2】:

    好的,我知道你的答案了。

    首先,您需要打开 Light2D(脚本) 执行以下操作:

    1.) 在脚本顶部添加以下内容:

    using System.Collections;
    using UnityEngine;
    

    2.) 您需要将 Light2D 脚本的类设置为“public”(以便您可以在“lighting.cs”脚本中访问它。):

    public class Light2DManager : IDisposable
    

    3.) 完成后保存 light2d 脚本。


    好的,现在我们要访问它了……

    在您的 Lighting 脚本中,当我们想要访问另一个脚本时,我们通常会这样做:

    GameObject TheLight = GameObject.Find("The Gameobject the script is on");
    
    UnityEngine.Experimental.Rendering.LWRP.Light2D The2DLights = TheLight.GetComponent  <UnityEngine.Experimental.Rendering.LWRP.Light2D> ();
    

    现在您应该可以访问了,只需输入以下内容进行确认:

    The2DLights.(您将获得强度等选项)

    这是我第一次回答问题。原来我遇到了同样的问题,很乐意提供帮助。

    【讨论】:

    • 嘿@ez_moneyy,
    • 抱歉第一个评论,我一直忘记按回车键保存评论
    • 嘿@ez_moneyy,感谢您的回答。我完成了您告诉我的步骤,但在访问脚本的部分我遇到了一些问题。如果我将在 Unity Inspector 中引用 LightSource 的 Light2D 组件,则(当然)称为 LightSource 示例代码的哪些部分我必须替换,还有什么我需要更改的吗?很抱歉用这些愚蠢的问题打扰您,但再次感谢您的帮助:-)
    • @neal 抱歉,Unity 无法将 light2d 识别为可以预制并拖入公共变量的光源。您将不得不使用您的脚本访问您在游戏对象上拥有的 light2d 脚本并为其添加一个随机范围,如下所示: Lights.intensity = Random.Range(2f, 5f);在我给出的前 3 个步骤之后,您应该能够使用我回答的最后一部分访问和操作 light2d 脚本中的变量。加我不和谐,我会带你走过它 Lunatiq #0247
    • 所以我应该将改变强度的代码添加到 Light2D 组件脚本中,而不是为它制作额外的脚本?
    【解决方案3】:

    今天遇到了这个问题,在顶部扩展了 using 语句的解决方案(在我的情况下,只需添加它在 2020.2 上不起作用),您可以执行以下操作:

    using Light2DE = UnityEngine.Experimental.Rendering.Universal.Light2D;
    

    然后在您的脚本中使用 Light2DE 作为类而不是 Light2D。

    light = transform.GetComponent<Light2DE>();
    

    【讨论】:

      【解决方案4】:

      我不太清楚这个问题,但您可能只是在寻找“GetComponent”。

      这是一个随机的例子,

      public class CamToUI:MonoBehaviour
          {
          [System.NonSerialized] public WebCamTexture wct;
          public Text nameDisplay;
      
          private RawImage rawImage;
          private RectTransform rawImageRT;
          private AspectRatioFitter rawImageARF;
          private Material rawImageMaterial;
      
          void Awake()
              {
              rawImage = GetComponent<RawImage>();
              rawImageRT = rawImage.GetComponent<RectTransform>();
              rawImageARF = rawImage.GetComponent<AspectRatioFitter>();
              }
      

      GetComponent 查找附加到同一个游戏对象的“事物”(例如“light2D”)。

      • 你有一些游戏对象

      • Script.cs 脚本附加到那个游戏对象

      • 其他一些东西(比如 Light2D)附加到同一个游戏对象

      在脚本 Script.cs 中,您可以使用 GetComponent 找到其他内容。

      理想情况下,不要重复调用 GetComponent,只需在 Awake 中调用一次即可。 (但你离担心性能还有一英里远,所以不用担心。)

      【讨论】:

        【解决方案5】:
        using UnityEngine.Experimental.Rendering.Universal;
        using UnityEngine;
        
        public class Light2DController : MonoBehaviour {
        
            void Awake() {
                //assuming You have Light2D component in the same object that has this script
                Light2D light = transform.GetComponent<Light2D>();
            }
        }
        

        Unity 2019.3.0f1

        【讨论】:

          【解决方案6】:

          以下是访问 Light2D(Script) 组件的方法。

          Unity 版本:Unity 2020.1.4f1

          using UnityEngine.Experimental.Rendering.Universal; 导入您的脚本。

          请查看以下代码示例。

          using System.Collections.Generic;
          using UnityEngine;
          using UnityEngine.Experimental.Rendering.Universal;//Important
          public class Lighting : MonoBehaviour
          {
              
              Light2D theLights; // To Access the Light Components.
          
              public float maxRange = 0.54f;
              public float minRange = 0.31f;
              public float flickerSpeed = 0.5f;
          
              void Update()
              {
                 //Assuming You have Light2D component in the same object that has this script.
                 theLights.intensity = Mathf.Lerp(minRange, maxRange, Mathf.PingPong(Time.time, flickerSpeed));
                // The above Line is to create a flickering effect, You can also set a value like for example: theLights.intensity=0.5f, To set the intensity of the Light.
              }
          }
          

          您也可以通过类似的方式访问其他 Light2D 组件

          theLights.color = new Color(0, 255, 0);// 这里我正在访问颜色并设置新的颜色值。

          【讨论】:

          • 你的 GetComponent 在哪里?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-03-28
          • 1970-01-01
          • 1970-01-01
          • 2016-09-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多