C# 如何控制点光源';什么是统一的颜色?
我已经成功地将材质应用于网格,并随着时间的推移不断更改颜色(从一组颜色中拾取),我希望对点光源也这样做。我怎样才能做到这一点 这是控制材质颜色的脚本,我想进行调整,这样它也可以控制灯光的颜色C# 如何控制点光源';什么是统一的颜色?,c#,unity3d,C#,Unity3d,我已经成功地将材质应用于网格,并随着时间的推移不断更改颜色(从一组颜色中拾取),我希望对点光源也这样做。我怎样才能做到这一点 这是控制材质颜色的脚本,我想进行调整,这样它也可以控制灯光的颜色 using System.Collections; using System.Collections.Generic; using UnityEngine; public class ChangeColors : MonoBehaviour { public Color[] colors;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColors : MonoBehaviour
{
public Color[] colors;
public int currentIndex = 0;
private int nextIndex;
public float changeColourTime = 2.0f;
private float lastChange = 0.0f;
private float timer = 0.0f;
void Start()
{
if (colors == null || colors.Length < 2)
Debug.Log("Need to setup colors array in inspector");
nextIndex = (currentIndex + 1) % colors.Length;
}
void Update()
{
timer += Time.deltaTime;
if (timer > changeColourTime)
{
currentIndex = (currentIndex + 1) % colors.Length;
nextIndex = (currentIndex + 1) % colors.Length;
timer = 0.0f;
}
GetComponent<Renderer>().material.color = Color.Lerp(colors[currentIndex], colors[nextIndex], timer / changeColourTime);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类颜色:单一行为
{
公共颜色[]颜色;
公共int currentIndex=0;
私有int nextIndex;
公共浮动时间=2.0f;
私有浮动lastChange=0.0f;
专用浮点计时器=0.0f;
void Start()
{
if(colors==null | | colors.Length<2)
Log(“需要在inspector中设置颜色数组”);
nextIndex=(currentIndex+1)%colors.Length;
}
无效更新()
{
timer+=Time.deltaTime;
如果(计时器>更改颜色时间)
{
currentIndex=(currentIndex+1)%colors.Length;
nextIndex=(currentIndex+1)%colors.Length;
定时器=0.0f;
}
GetComponent().material.color=color.Lerp(颜色[currentIndex]、颜色[nextIndex]、计时器/ChangeColor时间);
}
}
您只需在脚本中获取对点光源的引用,并像对渲染器材质所做的那样更改其颜色
您的脚本可以得到很大改进,但只需进行少量更改,如下所示:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeColors : MonoBehaviour
{
public Color[] colors;
public Light pLight;
public int currentIndex = 0;
private int nextIndex;
public float changeColourTime = 2.0f;
private float lastChange = 0.0f;
private float timer = 0.0f;
void Start()
{
if (colors == null || colors.Length < 2)
Debug.Log("Need to setup colors array in inspector");
nextIndex = (currentIndex + 1) % colors.Length;
}
void Update()
{
timer += Time.deltaTime;
if (timer > changeColourTime)
{
currentIndex = (currentIndex + 1) % colors.Length;
nextIndex = (currentIndex + 1) % colors.Length;
timer = 0.0f;
}
Color c = Color.Lerp(colors[currentIndex], colors[nextIndex], timer / changeColourTime);
GetComponent<Renderer>().material.color = c; // MIND! you should store the Renderer reference in a variable rather than getting it once per frame!!!
pLight.color = c;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类颜色:单一行为
{
公共颜色[]颜色;
公共照明困境;
公共int currentIndex=0;
私有int nextIndex;
公共浮动时间=2.0f;
私有浮动lastChange=0.0f;
专用浮点计时器=0.0f;
void Start()
{
if(colors==null | | colors.Length<2)
Log(“需要在inspector中设置颜色数组”);
nextIndex=(currentIndex+1)%colors.Length;
}
无效更新()
{
timer+=Time.deltaTime;
如果(计时器>更改颜色时间)
{
currentIndex=(currentIndex+1)%colors.Length;
nextIndex=(currentIndex+1)%colors.Length;
定时器=0.0f;
}
Color c=Color.Lerp(颜色[currentIndex]、颜色[nextIndex]、计时器/更改颜色时间);
GetComponent().material.color=c;//注意!应该将渲染器引用存储在变量中,而不是每帧获取一次!!!
颜色=c;
}
}
这是旧的,但您只需要访问灯光组件
GetComponent Light.color
而不是Renderer.material.color异常:缺少组件异常:没有“Renderer”附加到“Light”游戏对象,但脚本正在尝试访问它。联机:GetComponent().material.color=c;您可能将脚本附加到了错误的对象。编写脚本的方式要求您将其附加到具有渲染器的对象,例如立方体或球体。查看附件中的层次结构和检查器screenshot@atline嘿,谢谢你昨天刚注册的编辑我是新来的--也许你可以把它做得更好,它实际上是GetComponent“GreaterThanSign”Light“LessThanSign”()color,但当我昨晚试图键入它时,它删除了这些标志以及它们之间的所有内容