Files
netease-modsdk-wiki/docs/mcguide/20-玩法开发/18-性能优化/组件与接口使用优化.md
2025-03-18 14:46:12 +08:00

140 lines
4.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
front: https://nie.res.netease.com/r/pic/20210728/5507b669-4c6f-4958-b5d0-b8556ab4cfb5.png
hard: 进阶
time: 20分钟
---
# 组件与接口使用优化
## 前言
MC的强大在于他的开放性为开发者提供了大量系统功能和Api做支持但很多功能如果使用不当容易出现性能问题。这里会列出一些需要注意的系统功能的使用。
## 角色特效数量控制
### 意义
因为实际游戏中会存在多个角色同时显示在屏幕内的情况,所以如果是绑定在角色身上的特效需要更为注重性能问题,避免往角色身上挂接会发射大量粒子的粒子特效,或者挂接大量序列帧或者文本。
### 粒子特效使用太多
- 错误写法:
(自定义的翅膀定义了每秒发射200到300个粒子太多了如果同屏有6个玩家就是1200到1800个粒子,粒子的更新与渲染都有一定性能消耗)
```json
# testWind.json:
{
"particleeffect": {
"emissionrate": {
"max": "300.0",
"min": "200.0"
},
...()
}
```
- 正确写法:
(以下几个方案可结合使用)
```python
1方案1, 严格控制粒子数量在100左右
# 某个翅膀的定义 testWind.json:
{
"particleeffect": {
"emissionrate": {
"max": "100.0",
"min": "80.0"
},
...(省略无关代码)
}
2方案2分档定义多个翅膀游戏内让玩家根据自己设备配置自行选择
# 高配翅膀
{
"particleeffect": {
"emissionrate": {
"max": "300.0",
"min": "280.0"
},
...(省略无关代码)
}
# 中配翅膀
{
"particleeffect": {
"emissionrate": {
"max": "200.0",
"min": "180.0"
},
...(省略无关代码)
}
# 低配翅膀
{
"particleeffect": {
"emissionrate": {
"max": "100.0",
"min": "80.0"
},
...(省略无关代码)
}
3方案3提供显示隐藏特效切换按钮提供显示隐藏其它玩家特效切换按钮玩家觉得卡顿时可随时自行切换
```
## 点对点的消息同步
### 意义
服务端消息通知客户端能点对点就不要广播,游戏中服务端经常需要下发消息给客户端,如果采用广播的话,全部客户端都会收到,只对某个玩家发的消息不应该用广播。
例子游戏中有A,B,C三位玩家服务端给A发消息
- 错误写法:
(广播给所有人,其实其它人根本不需要接收这个消息,会浪费这多余的流量与性能)
```python
# 客户端:
class MyClientSystem(ClientSystem):
def __init__(self, namespace, systemName):
ClientSystem.__init__(self, namespace, systemName)
# 注册监听
self.ListenForEvent(clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName(), 'OnServerSay', self, self.OnServerSay)
# 监听回调函数
def OnServerSay(self, data):
if data["playerId"] == clientApi.GetLocalPlayerId():
print("server talk to me, content:", data["content"])
# 服务端
class UIDemoServerSystem(ServerSystem):
def talkToClient(self, playerId):
# 广播方式
self.BroadcastToAllClient("OnServerSay", {
"playerId": playerId,
"content": "hello!"
})
def func(self):
self.talkToClient(playerAId)
```
- 正确写法:
```python
# 客户端:
class MyClientSystem(ClientSystem):
def __init__(self, namespace, systemName):
ClientSystem.__init__(self, namespace, systemName)
# 注册监听
self.ListenForEvent(clientApi.GetEngineNamespace(), clientApi.GetEngineSystemName(), 'OnServerSay', self, self.OnServerSay)
# 监听回调函数
def OnServerSay(self, data):
print("server talk to me, content:", data["content"])
# 服务端
class UIDemoServerSystem(ServerSystem):
def talkToClient(self, playerId):
# 点对点发送的方式
self.NotifyToClient(playerId, "OnServerSay", {
"content": "hello!"
})
def func(self):
self.talkToClient(playerAId)
```