using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestLayer : MonoBehaviour
{
void Start()
{
string layerName = "TestLayer";
TagManagerAutoAdd.AutoAddLayer(layerName);
this.gameObject.layer = LayerMask.NameToLayer(layerName);
Debug.Log(LayerMask.NameToLayer(layerName));
Debug.Log(this.gameObject.layer);
//print(SortingLayer.GetLayerValueFromName(layerName));
Camera.main.cullingMask &= ~(1 << this.gameObject.layer);
}
}
参考资料1,2里面的
using UnityEditor;
using UnityEngine;
...
public class TagManagerAutoAdd//类的名字 与脚本的文件名称一致
...
public static void AutoAddLayer(string layer)//改为static void AutoAddLayer(string layer)
...
运行结束的时候,添加的Layer的名字会清空。
貌似只能在 Unity编辑器里面使用,不能在打包后进行使用。

这篇博客探讨了在Unity中如何通过代码动态为GameObject设置Layer,并且解决打包后Layer名丢失的问题。文章提到,Unity允许设置24个自定义Layer,尽管在编辑器中设置的Layer名可能在打包后不保留,但仍然可以通过序号进行操作。此外,作者建议利用Dictionary管理Layer,并提醒读者注意类名变化和确保场景中已添加相关设置。
最低0.47元/天 解锁文章
1092

被折叠的 条评论
为什么被折叠?



