1
2
3
4
5
6
7
8
9
10
11
12
13
|
// bad
// 这里调用了 n的2次方 Input.touches ,n = Input.touches.Length;
for
(
int
i = 0; i < Input.touches.Length; ++i)
{
Touch touch = Input.touches[i];
}
// good
Touch[] touchArr = Input.touches;
int
len = touchArr.Length;
for
(
int
i = 0; i < len; ++i)
{
Touch touch = touchArr[i];
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public
class
Player
{
private
int
m_playerId;
public
int
playerId
{
get
{
return
m_playerId; }
set
{ m_playerId = value; }
}
private
void
Update(
int
id)
{
// bad
playerId = id;
// 这里会调用 get 函数
// good
m_playerId = id;
// 直接访问成员变量
}
}
|
1
2
3
4
5
6
7
8
9
10
|
private
Transform m_transform;
void
Start()
{
m_transform = transform;
}
void
Update()
{
m_transform.Translate(
new
Vector3(1, 1, 0), Space.World);
// 大量调用时,减少调用链
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
static
class
MessageMgr
{
public
static
void
RegisterMsg(
string
msgType, System.Action callBack)
{
}
public
static
void
DispatchMsg(
string
msgType)
{
}
}
void
SendMsgToPlayer()
{
// bad
SendMessage(
"InitPlayer"
,
this
);
// 会遍历整个Object的子节点,判断是否有InitPlayer方法
MessageMgr.DispatchMsg(
"MsgInitPlayer"
);
// 自定义消息管理, 定向发送消息,效率更高
}
|
1
2
3
|
System.Type t = System.Type.GetType(
"Player"
);
Player p = (Player)System.Activator.CreateInstance(t);
p.playerId = 10001;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
void
LoadLevel()
{
int
level = 2;
StartCoroutine(LoadLevelCoroutine(level));
}
IEnumerator LoadLevelCoroutine(
int
level)
{
// load secene
yield
return
new
WaitForEndOfFrame();
// load ui
yield
return
new
WaitForEndOfFrame();
// load audio
yield
return
new
WaitForEndOfFrame();
// load actor
yield
return
new
WaitForSeconds(1.0f);
}
|
1
2
3
4
|
PlayerInfo litPlayerInfo = JsonMapper.ToObject(litJsonStr);
// LitJson
PlayerInfo unityJsonInfo = JsonUtility.FromJson(litJsonStr);
// Unity 自带的JsonUtility 效率差很多倍
|
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
|
[System.Serializable]
class
PlayerConfig
{
public
int
id;
public
string
name;
}
private
PlayerConfig m_config;
private
string
m_jsonStr;
private
PlayerConfig ParseConfig(
string
jsonStr)
{
return
JsonUtility.FromJson(jsonStr);
}
private
void
InitConfig()
{
m_jsonStr =
"{\"id\":1001, \"name\":\"lfwu\"}"
;
m_config = ParseConfig(m_jsonStr);
}
private
void
Update()
{
// bad
int
id = ParseConfig(m_jsonStr).id;
// good
id = m_config.id;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
string
str1 =
"hello, world"
;
string
str2 =
"hell0, wor1d"
;
// bad
int
ret = 0;
ret =
string
.Compare(str1, str2);
// 语言相关,速度慢
ret = str1.CompareTo(str2);
// 语言相关,速度慢
// good
bool
isSame =
false
;
isSame = str1.Equals(str2);
isSame = str1.Equals(str2, System.StringComparison.Ordinal);
// 速度快,按字符2进制比较
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
Dictionary<
int
,
string
=
""
> playerDic =
new
Dictionary<
int
,
string
=
""
>
{
{ 10001,
"lfwu"
},
{ 10002,
"xiaoy"
}
};
int
id = 10001;
if
(playerDic.ContainsKey(id))
{
return
playerDic[id];
}
else
{
return
string
.Empty;
}
|
1
2
3
4
5
6
7
8
9
10
11
|
Dictionary<
int
,
string
=
""
> playerDic =
new
Dictionary<
int
,
string
=
""
>
{
{ 10001,
"lfwu"
},
{ 10002,
"xiaoy"
}
};
int
id = 10001;
string
ret =
string
.Empty;
playerDic.TryGetValue(id,
out
ret);
return
ret;
|
1
2
3
|
void
Awake() {}
void
Start() {}
void
Update() {}
|
1
2
3
4
5
6
7
8
9
|
private
List m_updateList =
new
List();
private
void
Update()
{
int
len = m_updateList.Count;
for
(
int
i = 0; i < len; ++i)
{
m_updateList[i]();
}
}
|
1
|
Application.targetFrameRate = Config.kFrameRate;
|
1
2
3
4
5
|
// bad
float
ret = dis / 2.0f;
// good
float
ret = dis * 0.5f;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Vector3 posA =
new
Vector3(1, 1, 1);
Vector3 posB =
new
Vector3(2, 2, 2);
// bad
float
minDis = 10.0f;
float
dis = Vector3.Magnitude(posA - posB);
// 这里调用了开平方,效率比较低
if
(dis < minDis)
{
}
// good
float
sqrMinDis = 10.0f * 10.0f;
float
sqrDis = Vector3.SqrMagnitude(posA - posB);
// 平方和,速度很快
if
(sqrDis < sqrMinDis)
{
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
float
Q_rsqrt(
float
number )
{
long
i;
float
x2, y;
const
float
threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * (
long
* ) &y;
// evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 );
// what the fuck?
y = * (
float
* ) &i;
y = y * ( threehalfs - ( x2 * y * y ) );
// 1st iteration
return
y;
}
|