我似乎无法在intellisense中找到Draw Halo属性。有没有办法以编程方式设置其值?谢谢!
答案 0 :(得分:3)
<强>更新强>:
public Component AddComponent(string className);
现已弃用并已删除,因此无法再使用它来执行此操作。看一下我所做的扩展方法,名为AddComponentExt
,可用于执行此操作here。
答案是使用:gameObject.AddComponentExt("Halo");
OLD ANSWER :
尽管已经回答了这个问题,但我认为其他会遇到这种情况的人会这很有用。
除了rutter's回答,
Halo
类无法像其他组件一样直接访问。
GetComponent
函数有两个重载:
public Component GetComponent(Type type);
public Component GetComponent(string type);
AddComponent
函数有两个重载:
public Component AddComponent(Type componentType);
public Component AddComponent(string className);
您必须将GetComponent
和AddComponent
与string
参数一起使用,而不是Type
参数。
GetComponent("Halo");
和AddComponent("Halo");
将编译。
GetComponent<Halo>();
和AddComponent<Halo>();
将不编译。
此外,您需要使用反射通过启用/禁用它来打开和关闭Halo
。
使用反射打开/关闭Halo的扩展方法:
public static class ExtensionMethod
{
public static void drawHalo(this Light light, bool value)
{
//Get Halo Component
object halo = light.GetComponent("Halo");
//Get Enable Halo property
var haloInfo = halo.GetType().GetProperty("enabled");
//Enable/Disable Halo
haloInfo.SetValue(halo, value, null);
}
}
<强>用法强>:
Light light = GameObject.Find("Point light").GetComponent<Light>();
light.drawHalo(true); //Extension function. pass true or false
注意:
在使用此功能之前,请务必将Halo
附加到Light
。
选择你的灯,然后转到组件 - &gt; 效果 - &gt; 晕。您也可以使用yourLight.AddComponent("Halo");
脚本执行此操作。
答案 1 :(得分:1)
Halo
是一个单独的组件。
为灯光添加光环:AddComponent<Halo>()
要访问附加到灯光的光晕:GetComponent<Halo>()
&#34; Draw Halo&#34;检查器中的复选框有点像红色鲱鱼 - 它会创建一个Halo
组件,然后从层次结构视图中隐藏该组件,该组件视图是笨拙的,但它是从旧版本的Unity中保留的。