Unity不熟悉API踩过的坑

Camera设置渲染层级问题

如果想动态设置Camera的渲染层级,在网上搜索大概都有这样几个示例:

camera.cullingMask = 1 << x; // 渲染x层

camera.cullingMask = ~(1 << x); // 渲染除去层x的所有层

camera.cullingMask &= ~(1 << x); // 关闭层x

camera.cullingMask |= (1 << x); // 打开层x

camera.cullingMask = 1 << x + 1 << y + 1 << z; // 摄像机只显示第x层,y层,z层.(最开始以为运算顺序是1 <<( x + 1 )<<(y + 1) << z)

但是最后一个这样设置,如果想渲染多个层级真的是这样的吗?我Unity5.6代码这样写之后没有得到我想要的结果。然后看了一下cullingMask 设置参数的原理,是用的位运算来确定哪些层级被选中,记得大学老师讲C++的时候讲过这种方法。想了一下,觉得参数应该这样设置:

camera.cullingMask = (1 << x )+ (1<< y )+ (1 << z); // 摄像机只显示第x层,y层,z层.

试了一下,是我想要的效果。

Transform.SetParent第二个参数的坑

可能是当时学习Unity的时候没有仔细,后来才发现这样的问题:

Unity中Transform.SetParent第二个参数的使用。

Unity编辑器下某些方法执行结果与预期不同

发现这个问题是想学习一下编辑器的东西:

原文: Unity编辑器下某些方法执行结果与预期不同

​ Unity的transform中有一个GetComponentsInChildren方法,可以获取到GameObject下所有子物体上需要查找的组件,但是不知道为什么在编辑器下获取不到。

​ 在Unity编辑器模式下,用一个Object接收物体,OnGUI()中添加

obj = EditorGUILayout.ObjectField(“需要查看的预制”, obj, typeof(Object), false);代码。

然后在Unity中拖拽prefab到编辑器窗口中。把obj转换成GameObject后,使用如下代码查找子物体上的组件,

1
_gameObject.transform.GetComponentsInChildren<Transform>();

点击并拖拽以移动

​ 结果组件找不到,并且发现GameObject下的GetComponentInParent也查找不到,其他的没有一一验证。(之前使用这个API的时候是用的Unity4.7.2,今天使用Unity5.6没有这个问题,如果遇到这个问题,在方法里面添加一个TRUE参数即可。如

1
_gameObject.transform.GetComponentsInChildren<Transform>(true);

点击并拖拽以移动

–2018.05.02)

所以先写一个方法在编辑器模式下获取子物体的组件。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/// <summary>
/// 获取子物体的某个组件
/// </summary>
/// <typeparam name="T">组件泛型</typeparam>
/// <param name="obj">父物体</param>
/// <returns></returns>
private T[] GetComponentsInChildren<T>(GameObject obj) where T : Component
{
if (obj == null)
return null;
Transform _tran = obj.transform;
List<T> _listT = new List<T>();
for (int i = 0; i < _tran.childCount; i++)
{
T _t = _tran.GetChild(i).GetComponent<T>();
if (_t != null)
{
_listT.Add(_t);
}
if (_tran.childCount > 0)
{
T[] _childrenT = GetComponentsInChildren<T>(_tran.GetChild(i).gameObject);
for (int j = 0; j < _childrenT.Length; j++)
{
_listT.Add(_childrenT[j]);
}
}
}
return _listT.ToArray();
}

点击并拖拽以移动

C#中Split的使用

原文: C#中Split的使用

String.Split有六个重载方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
private string _testStr = "(AAA)123(BBB)";
void Start()
{
string[] _temp;
//public String[] Split(params char[] separator);
//按参数字符拆分字符串,保留空字符
_temp = _testStr.Split('(');
LogSplitStr(_temp);//[],[AAA)123],[BBB)],
_temp = _testStr.Split('(', ')');
LogSplitStr(_temp);//[],[AAA],[123],[BBB],[],
//public String[] Split(char[] separator, StringSplitOptions options);
//按字符数组拆分字符串,不保留空字符
_temp = _testStr.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
LogSplitStr(_temp);//[AAA],[123],[BBB],
//public String[] Split(String[] separator, StringSplitOptions options);
//按字符串数组拆分字符串,不保留空字符
_temp = _testStr.Split(new string[] { "AA", "BB" }, StringSplitOptions.RemoveEmptyEntries);
LogSplitStr(_temp);//[(],[A)123(],[B)],
//public String[] Split(char[] separator, int count);
//按字符数组拆分字符串,返回字符串数组最大长度为Count
_temp = _testStr.Split(new char[] { '(', ')' }, 5);
LogSplitStr(_temp);//[],[AAA],[123],[BBB],[],
//public String[] Split(char[] separator, int count, StringSplitOptions options);
//按字符数组拆分字符串,返回字符串数组最大长度为Count 不保留空字符
_temp = _testStr.Split(new char[] { '(', ')' },6, StringSplitOptions.RemoveEmptyEntries);
LogSplitStr(_temp);//[AAA],[123],[BBB],
//public String[] Split(String[] separator, int count, StringSplitOptions options);
//按字符串数组拆分字符串,返回字符串数组最大长度为Count 不保留空字符
_temp = _testStr.Split(new string[] { "AA", "BB" }, 6,StringSplitOptions.RemoveEmptyEntries);
LogSplitStr(_temp);//[(],[A)123(],[B)],
}
private void LogSplitStr(string[]_temp)
{
string _tempStr = "";
for (int i = 0; i < _temp.Length; i++)
{
_tempStr = _tempStr +"["+ _temp[i] + "],";
}
Debug.Log(_tempStr);
}

点击并拖拽以移动

正常的字符串分割就是传一个参数。

如果要分割的字符串里面有多个需要分割的字符时,可以传入多个分隔符。

特殊情况,如果头尾有分隔符,或者两个分隔符中间没有其他字符时,默认分割之后是保留空字符的,如果不需要空字符的话,可以传入参数StringSplitOptions.RemoveEmptyEntries移除掉空字符。

来源:CSDN
原文:https://blog.csdn.net/qq_33461689/article/details/85011689
版权声明:本文为博主原创文章,转载请附上博文链接!

-------------    本文结束  感谢您的阅读    -------------
打赏两块钱买糖吃!
0%