C# 我如何访问Unity';灯光2D(脚本)和#x27;通过脚本的组件?

C# 我如何访问Unity';灯光2D(脚本)和#x27;通过脚本的组件?,c#,unity3d,2d,lighting,C#,Unity3d,2d,Lighting,在我的Unity 2D游戏中,我有一个带有光源组件的角色 轻量级RP包中的“Light 2D(脚本)” 我想用下面的代码更改2D灯光的强度。 但我无法在Unity Inspector面板中将“Light 2D(Script)”分配给公共光源 我尝试过使用publiclight2dlightsource类,但它似乎不存在 有没有其他方法可以访问2D灯光组件或我做错了什么? 如果有帮助的话,我还添加了Inspector窗格的屏幕截图 如果你需要更多的信息,请告诉我,我希望有人能帮助你。谢谢 usin

在我的Unity 2D游戏中,我有一个带有光源组件的角色 轻量级RP包中的“Light 2D(脚本)”

我想用下面的代码更改2D灯光的强度。 但我无法在Unity Inspector面板中将“Light 2D(Script)”分配给
公共光源

我尝试过使用
publiclight2dlightsource
类,但它似乎不存在

有没有其他方法可以访问2D灯光组件或我做错了什么? 如果有帮助的话,我还添加了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;
    }
}


我不太清楚这个问题,但您可能只是在寻找“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>();
        }
public class CamToUI:monobhavior
{
[系统非串行化]公共网络摄像头;
公共文本名称显示;
私有图像;
私有矩形图;
私有aspectratiofter rawmagearf;
私人材料;
无效唤醒()
{
rawImage=GetComponent();
rawImageRT=rawImage.GetComponent();
rawImageARF=rawImage.GetComponent();
}
GetComponent查找附加到同一游戏对象的“东西”(如“light2D”)

•您有一些游戏对象

•脚本script.cs附加到该游戏对象

•其他东西(比如Light2D)连接到同一游戏对象

在script.cs中,您可以使用GetComponent找到其他内容


理想情况下,不要反复调用GetComponent,只需在唤醒或其他情况下调用它一次即可。(但你离担心性能还有一英里远,所以不要担心它。)

好的,伙计,我知道你的答案了

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

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

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

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


好的,现在我们要访问它

在您的照明脚本中,当我们想要访问另一个脚本时,我们会执行通常的操作:

GameObject TheLight=GameObject.Find(“脚本所在的游戏对象”);
UnityEngine.Experimental.Rendering.LWRP.Light2D The2DLights=TheLight.GetComponent();
现在您应该有了访问权限,只需键入以下内容即可确认:

2灯。
(您将获得强度等选项)

这是我第一次回答问题。原来我也有同样的问题,很乐意帮忙。

使用UnityEngine.Experimental.Rendering.Universal;
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>();
    }
}
使用UnityEngine; 公共类控制器:单行为{ 无效唤醒(){ //假设在具有此脚本的同一对象中有Light2D组件 Light2D light=transform.GetComponent(); } }

Unity 2019.3.0f1

假设您使用通用渲染管道进行二维照明,这就是我找到的解决方案

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

使用UnityEngine.Experimental.Rendering.Universal;


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

以下是访问Light2D(脚本)的方法组成部分

Unity版本:Unity 2020.1.4f1

使用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=新颜色(0,255,0);//这里我正在访问颜色并设置新的颜色值


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

using Light2DE = UnityEngine.Experimental.Rendering.Universal.Light2D;
然后在脚本中使用Light2DE而不是Light2D作为类

light = transform.GetComponent<Light2DE>();
light=transform.GetComponent();

您是否有任何错误,是否在编辑器中连接了灯光?您的问题不是很清楚场景中的实际情况。屏幕截图会有很大帮助。无论如何,请尝试将行从
公共光源;
更改为
公共光源2D;
然后在中拖动
Light2D
组件检查员。是的,可能是通过使用正确的字段类型……虽然我不知道任何
Light2D
类,而且它也没有出现在API中。你从哪里得到它?@derHugo我也不确定,但我有点假设它可能是@Ruzihm或;)但是是的,Rhey@ez_money,很抱歉第一条评论,我一直忘了按enter键保存注释Hey@ez_Money,谢谢你的回答。我已经完成了你告诉我要做的步骤,但在访问脚本的过程中遇到了一些问题。如果我将在Unity Inspector中引用光源的Light2D组件是(当然)致电LightSource,您的示例代码中有哪些部分需要替换,还有哪些部分需要更改?很抱歉用这些愚蠢的问题打扰您,但再次感谢您的帮助:
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.
    }
}
using Light2DE = UnityEngine.Experimental.Rendering.Universal.Light2D;
light = transform.GetComponent<Light2DE>();