c# - (Unity) 更改图层蒙版时向游戏对象添加脚本
问题描述
我希望能够在更改其 LayerMask 时向游戏对象添加脚本。我希望在编辑器中更改它而不是仅在运行时发生这种情况。
我该怎么做?我无法在 Google 上提出问题以找到任何可能对我有帮助的必要信息。
先感谢您。
解决方案
我认为没有任何内置功能,但您可以使用GameObject.layer,存储它并进行比较。
使用[ExecuteInEditMode]
您可以使组件在编辑器中执行一些方法:
[ExecuteInEditMode]
public class LayerChecker : MonoBehaviour
{
private int lastLayer;
// could e.g be a UnityEvent (like onClick of buttons)
public UnityEvent OnLayerChanged;
private void Update()
{
// if layer didn't change do nothing
if(lastLayer == gameObject.layer) return;
// changed! -> what ever you want to happen
// e.g. invoke the event or call another method
OnLayerChanged.Invoke();
// store the new layer
lastLayer = gameObject.layer;
}
}
如果您想限制发生这种情况的位置,您还可以检查Application.isEditor和/或Application.isPlaying
推荐阅读
- javascript - 如何在重定向之前发出请求而不等待响应?
- model-view-controller - MVC 如何枚举第二个模型的数据
- google-cloud-platform - 如何在谷歌云中的自定义端口 8080 上允许出站流量
- node.js - TSLint 在 Jenkins 作业中缺少空格
- java - 使用 WebElement 列表时的陈旧元素,但与 Selenium 中的单个元素一起工作正常
- python - 如何删除 m² 并将 ha 转换为 m²?
- javascript - Node.js 如何获取类方法参数并使用它来调用“this.argument”
- python - Pandas:对于组中的每一行,为一列分配一个值
- java - 无法在 aws 中连接到 Dax 集群
- css - SVG – :hover 和 links 不能与 clip-path 和 Firefox 结合使用