Files
TouchSocket/handbook/docs/troubleshootunity3d.mdx
若汝棋茗 6c75d6999d 更新文档
2023-10-17 17:51:59 +08:00

72 lines
2.9 KiB
Plaintext
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.

---
id: troubleshootunity3d
title: Unity3D相关
---
## 【问】TouchSocket系列是否能用于Unity3D
TouchSocket系列是否能用于Unity3D
### 【解决】
可以的TouchSocket是基于Net45和.netstandard2.0的且没有任何其他运行时是100%基于C#开发的所以可以直接用于unity。
但是由于TouchSocket全系都是基于Socket构建的所以目前无法用于WebGL。除此之外支持window、android、ios、linux等平台。
目前**实测**支持Il2cpp编译支持HybridCLR热更新。
TouchSocket提供一个简单的示例demo包含一个服务器和unity包。以供大家下载试用。 [TouchSocket For Unity](https://gitee.com/RRQM_Home/TouchSocket/tree/master/examples/Unity3d)
## 【问】用于Unity 3D时显示有dll不兼容或者找不到
【描述】
unity提示由于以下错误将不会加载程序集“Assets/Plugins/TouchSocket.dl”无法解析引用“TouchSocket.Core”。程序集是否丢失或与当前平台不兼容?可以在插件检查器中禁用引用验证.无法解析引用“TouchSocket”。程序集是否丢失或与当前平台不兼容?可以在插件检查器中禁用引用验证
### 【解决】
这是因为TouchSocket系列程序集有依赖关系。例如TouchSocket依赖于TouchSocket.Core。所以在下载TouchSocket.dll时应该也下载TouchSocket.Core.dll.
当然有时候我们对具体依赖不了解时可以直接在vs看依赖关系。
## 【问】程序集用于Unity 3D时有莫名其妙的异常
【描述】
我将TouchSocket程序集引入到U3D中后使用了相关功能或者其他组件功能在编辑器界面正常但是发布到PC、Android等平台时无法使用
### 【解决1】
首先查看项目是否设置了`IL2CPP`,如果设置了的话,可以考虑是否能设置为`Mono`如果能则OK。
<img src={require('@site/static/img/docs/troubleshootunity3d-1.png').default} width="300" />
### 【解决2】
需要unity内link.xml设置(放置在Assets文件夹内)。 [unity官方文档 托管代码剥离](https://docs.unity3d.com/cn/current/Manual/ManagedCodeStripping.html#LinkXML)
下列仅示例部分,如果是其他组件,则添加相应程序集名称。
```csharp xml
<linker>
<assembly fullname="TouchSocket" />
</linker>
```
然后在程序运作最开始时调用全局设置
```csharp
GlobalEnvironment.OptimizedPlatforms = OptimizedPlatforms.Unity;
```
## 【问】程序集用于Unity 3D时Json序列化有问题
### 【解决】
这是因为unity中不支持代码生成的Json序列化所以必须用IL2cpp版的json。下面提供参考博客。
在git上下载Newtonsoft.Json-for-Unity-master的压缩文件.zip解压之后复制到unity3D工程的Asset/Plugins文件夹下就可以用了
[Unity版Newtonsoft.Json](https://github.com/jilleJr/Newtonsoft.Json-for-Unity)