首页 > 解决方案 > 如何使用 Unity 创建和访问小型服务器?

问题描述

我最初打算写一篇看起来像四段的文章来解释我正在做的事情,但这并不重要。总而言之,我是 Unity 的新手,对如何创建一个可以在“云”中存储(从其他客户端发送和检索)数据的平台知之甚少。然而,我需要能够为我的项目这样做。

using System.Collections.Generic;
using UnityEngine;

public class MultiplayerMoveMe : MonoBehaviour
{

    //Note: I am making a retro-style game where all sprites face the camera, so the rotation of the players is not a factor that needs considering here; just the position.
    //I have a fleshed-out idea on how I will do all of this, however I am completely foreign in all things server-related on this scale so I need some assistance(not the most prideful circumstances).
    public GameObject p2Obj;

    void Start()
    {
        //Anything that I might need to add that the serverGet() function might require
    }

    void serverGet(int playerNum, string reqType)
    {
        //On their side, every frame, the second player's X, Y, and Z pos should be packed into a string with seperator char '|' and then filed on the server under playerdata/2/pos/
        //Then, this script(on the side of player 1) would(every frame, displaced by +1 frame initially) take the player number and the reqType to find said directory online with THIS function to return the value.
        //Funny thing is; I have no idea what I'm doing.


        //And no, I haven't connected to a server yet. I also want to stay away from any third party apps for this since this is small-scale and I only wish to learn the ins and outs of all of this.


    }

    void Update()
    {
        String p2Position = serverGet(2,"pos");
        // String p2Position's value is currently "x|y|z"
        String[] sl = p2Position.Split('|');
        float xPos = float.parse(sl[0]);
        float yPos = float.parse(sl[1]);
        float zPos = float.parse(sl[2]);
        // Now that all values are floats, we can feed them into the thingamabobber to change the position of the other player from our side.
        p2Obj.transform.position = new Vector3(xPos, yPos, zPos);
    }
}

下面我有一个脚本,如果 serverGet() 函数的内容实际上是存在的(当然是功能性的),它将根据他们身边的实例提交的在线数据将第二个玩家的位置设置为他们的位置第一名(每帧也是,-1 帧初始位移,以便一切正常)。这样,如果我作为“玩家 2”在一台计算机上移动,我作为“玩家 1”在其中玩的计算机将显示玩家 2 在每一帧进行时的移动。换句话说,基本上所有的计算都是客户端的,但实际的通信(不可避免地)是服务器端的。服务器端是我一无所知的,如果这里有人能带领我朝着正确的方向迈出一步,我将不胜感激。

至于实际向服务器提交条款的脚本,这将伴随我对这一切的理解;再说一次,我现在还没有。

标签: databaseunity3dserver

解决方案


最近好像有很多问题:“所以我要从零开始写一个MP游戏引擎!” 示例。)

请尝试了解您的问题的规模。您即将开始从事博士水平的企业,这将至少需要数月的全职工作。首先,不花几天时间使用当前系统来获得一些基本原则,那将是疯狂的。

  • 同样,Photon 对于 mmp 系统非常受欢迎,https: //www.raywenderlich.com/1142814-introduction-to-multiplayer-games-with-unity-and-photon接下来花几天时间制作玩具 Photon/Unity 系统来学习更多的。

  • 最后,镜像网络是“像 Unity 的旧网络” https://assetstore.unity.com/packages/tools/network/mirror-129321所以你真的应该尝试一下。

  • 最后,从字面上回答你的问题,点击 AWS,启动一些 ubuntu EC2 实例,然后开始在“简单”游戏服务器上工作,所以几乎可以肯定你会使用 Node/Express/SQL 和可能是 websockets(刚开始,你最终必须转向原始 udp 通信)。在您熟悉一些基本的现有系统之前,开始这样做是不现实的。


推荐阅读