简体   繁体   English

Unity3D - 如何在客户端加入时生成对象?

[英]Unity3D - How to get an objects spawned when a client joined?

I'm making a simple network game and I have a problem.我正在制作一个简单的网络游戏,但遇到了问题。 Basilicaty, when any client joins the game a square is spawned (by NetworkManager component) and the client can control only that one square. Basilicaty,当任何客户端加入游戏时,会生成一个方格(通过 NetworkManager 组件)并且客户端只能控制该方格。 My problem is: how do I get a gameObject with the square from server view?我的问题是:如何从服务器视图中获取带有正方形的游戏对象? I mean I want to change a square's color to eg blue, so every connected client sees that particular square as a blue square.我的意思是我想将正方形的颜色更改为例如蓝色,因此每个连接的客户端都会将该特定正方形视为蓝色正方形。 Any ideas guys?有什么想法吗? (a gameobject with a square has NetworkIdentity component) (带有正方形的游戏对象具有 NetworkIdentity 组件)

I tried that:我试过了:

var a = NetworkServer.connections[NetworkServer.connections.Count() - 1].clientOwnedObjects; GameObject go = NetworkServer.FindLocalObject(a.ElementAt(previousPositions.Length));

but I got exception which says that GameObject go is a null但我得到了一个异常,它说 GameObject go 是一个空值

For a simple variation like color you can probably do it with scripting, just a property on the object set by the server.对于像颜色这样的简单变化,您可能可以通过脚本来完成,只需服务器设置的对象上的属性。 If you need something more complex, an entire asset bundle, try looking at these two options:如果您需要更复杂的东西,整个资产包,请尝试查看以下两个选项:

WWW.LoadFromCacheOrDownload WWW.LoadFromCacheOrDownload

UnityWebRequest 统一网络请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM