Beispiel #1
0
        private void Awake()
        {
            if (!WorldGen)
            {
                Debug.LogError(string.Format("{0}: WorldGen is not set.", name));
                return;
            }
            players        = new List <Player>(PlayerCount);
            playerCameras  = new List <PlayAreaCamera>(PlayerCount);
            playerCanvases = new List <MainCanvas>();

            for (var i = 0; i < PlayerCount; i++)
            {
                // Create player.
                var pos = new Vector3(Random.Range(-WorldGen.SizeInMeters / 2f + 1, WorldGen.SizeInMeters / 2f - 1),
                                      Random.Range(-WorldGen.SizeInMeters / 2f + 1, WorldGen.SizeInMeters / 2f - 1));
                var ply = (GameObject)Instantiate(PlayerPrefab, pos, Quaternion.identity);
                ply.name = "Player " + (i + 1);
                var plyScript = ply.GetComponent <Player>();
                plyScript.Index = i;
                players.Add(plyScript);
                ply.transform.SetParent(PlayersParent);

                // Create camera for player.
                var cam = (GameObject)Instantiate(PlayerCameraPrefab, pos + new Vector3(0, 0, CameraZOffset), Quaternion.identity);
                cam.name = ply.name + "'s camera";
                var camScript = cam.GetComponent <Camera>();
                camScript.SetViewportGrid(i, PlayerCount);
                var camAreaScript = cam.GetComponent <PlayAreaCamera>();
                camAreaScript.Target = ply.transform;
                playerCameras.Add(camAreaScript);
                cam.transform.SetParent(PlayerCamerasParent);

                // Create container on canvas for player.
                var can          = (GameObject)Instantiate(PlayerCanvasPrefab);
                var canRect      = can.GetComponent <RectTransform>();
                var canContainer = can.GetComponent <PlayerCanvasContainer>();
                can.transform.SetParent(PlayerCanvasParent, false);
                var canView = RectUtilities.GetGridRect(i, PlayerCount, Screen.width, Screen.height);
                canRect.sizeDelta     = canView.size;
                canRect.localPosition = canView.position;
                plyScript.Hud         = canContainer;
            }
        }
 /// <summary>
 ///     Calculates the viewport rect based on the current index and the desired totalFitAmount of viewports the screen
 ///     should fit.
 /// </summary>
 /// <param name="index">Index that determines the location of the current camera.</param>
 /// <param name="totalFitAmount">Amount of viewports that the screen should fit.</param>
 public static void SetViewportGrid(this Camera camera, int index, int totalFitAmount)
 {
     camera.pixelRect = RectUtilities.GetGridRect(index, totalFitAmount, Screen.width, Screen.height);
 }