[英]How can I access the Unity 'Light 2D (Script)' component via Script?
在我的 Unity 2D 游戲中,我有一個角色,其中包含來自 Lightweight RP package 的光源組件“Light 2D(Script)”。
我想用下面的代碼改變 Light 2D 的強度。 但我無法將“Light 2D(Script)”分配給 Unity Inspector 面板中的public Light LightSource
。
我試過使用public Light2D LightSource
class 但它似乎不存在。
有沒有其他方法可以訪問 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;
}
}
假設您使用 2D 照明的通用渲染管道,這是我找到的解決方案。
在 Light2D 腳本中(您可以通過單擊 Light2D 組件右上角的三個垂直點並點擊編輯腳本來打開該腳本),有命名空間 UnityEngine.Experimental.Rendering.Universal 的聲明。你的腳本頂部寫
using UnityEngine.Experimental.Rendering.Universal;
執行此操作時可能會出現錯誤,但請按。 從這里開始,您將能夠像引用任何其他組件一樣引用 Light2D 組件,例如使用GetComponent<Light2D>()
如果其中任何一個在您的代碼編輯器中出現錯誤,仍然可以編譯以測試它是否有效,我有一個使用這種方法的功能齊全的腳本,但 Visual Studio 仍然認為 Light2D 不存在。 無論如何,這似乎有效,我希望這也適用於其他所有人。
好的,我得到了你的答案。
首先,您需要打開 Light2D(腳本)執行以下操作:
1.)在腳本頂部添加以下內容:
using System.Collections;
using UnityEngine;
2.)您需要將 Light2D 腳本的 class 設置為“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.
(你會得到強度等選項)
這是我第一次回答問題。 原來我遇到了同樣的問題,很樂意提供幫助。
今天遇到了這個問題,要在頂部擴展 using 語句的解決方案(在我的情況下,只需添加它在 2020.2 上不起作用),您可以執行以下操作:
using Light2DE = UnityEngine.Experimental.Rendering.Universal.Light2D;
然后在腳本中使用 Light2DE 作為 class 而不是 Light2D。
light = transform.GetComponent<Light2DE>();
我不太清楚這個問題,但您可能只是在尋找“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 查找附加到同一個游戲 object 的“事物”(例如“light2D”)。
• 你有一些游戲 object
• 腳本 Script.cs 附加到該游戲 object
• 其他一些東西(比如 Light2D)附加到同一個游戲 object
在腳本 Script.cs 中,您可以使用 GetComponent 找到其他內容。
理想情況下,不要重復調用 GetComponent,只需在 Awake 中調用一次即可。 (但你離擔心性能還有一英里遠,所以不用擔心。)
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>();
}
}
統一2019.3.0f1
以下是訪問 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);// 這里我正在訪問顏色並設置一個新的顏色值。
在較新版本的統一(我使用的是 2021.2.7f1)中使用:
using UnityEngine.Rendering.Universal;
然后在代碼中:
Light2D light = gameObject.GetComponent<Light2D>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.