屏幕团结的2-D游戏裁剪问题 [英] Screen cropped issue in unity 2-D game

查看:204
本文介绍了屏幕团结的2-D游戏裁剪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公司开发的统一1个2-D游戏。首先,我已经在网络播放器上运行的游戏我的屏幕是从两端切割。现在,我创建从它的Andr​​oid APK。然后在Android设备自身也从两端切割。我们使用的是游离的宽高比。我不知道很多关于屏幕尺寸。因此,如何从这个问题的解决?

CameraFollow.cs脚本 -

 使用UnityEngine;
System.Collections中使用;

公共类CameraFollow:MonoBehaviour
{
    公众持股量xMargin = 1F;在x轴的玩家//距离可以移动相机如下之前。
    公众持股量yMargin = 1F;在y轴的玩家//距离可以移动相机如下之前。
    公众持股量xSmooth = 8F; //如何顺利相机赶上在x轴它的目标移动。
    公众持股量ySmooth = 8F; //如何顺利相机赶上在y轴它的目标移动。
    公共Vector2 maxXAndY; //最大x和y坐标的相机可具有。
    公共Vector2 minXAndY; //最小x和y坐标的相机可具有。


    私人变换播放器; //参考玩家的变换。


    清醒无效()
    {
        //设置的参考。
        球员= GameObject.FindGameObjectWithTag(玩家)转换。
        //camera.orthographicSize = 640 /屏幕宽度* screenheight / 2;
    }


    布尔CheckXMargin()
    {
        //返回true如果相机和玩家在x轴之间的距离大于在x余量。
        返回Mathf.Abs(transform.position.x  -  player.position.x)> xMargin;
    }


    布尔CheckYMargin()
    {
        //返回true如果相机和玩家在y轴之间的距离大于在y余量。
        返回Mathf.Abs(transform.position.y  -  player.position.y)> yMargin;
    }


    无效FixedUpdate()
    {
        TrackPlayer();
    }


    无效TrackPlayer()
    {
        //默认摄像机的目标的x和y坐标是它的当前x和y坐标。
        浮targetX = transform.position.x;
        浮targetY = transform.position.y;

        //如果玩家已经超越的X裕...
        如果(CheckXMargin())
            // ...目标x坐标应该是相机的当前x位置和玩家的当前x位置之间的线性插值。
            targetX = Mathf.Lerp(transform.position.x,player.position.x,xSmooth * Time.deltaTime);

        //如果玩家已经超越了在y缘...
        如果(CheckYMargin())
            // ... y坐标的目标应该是相机的当前y位置和玩家的当前y位置之间的线性插值。
            targetY = Mathf.Lerp(transform.position.y,player.position.y,ySmooth * Time.deltaTime);

        //目标x和y坐标不应超过最大比最小更大或更小。
        targetX = Mathf.Clamp(targetX,minXAndY.x,maxXAndY.x);
        targetY = Mathf.Clamp(targetY,minXAndY.y,maxXAndY.y);

        //设置摄像机的位置以相同的Z分量目标位置。
        transform.position =新的Vector3类型(targetX,targetY,transform.position.z);
    }
}
 

解决方案

既然你是一个2D游戏,我使用的是正交相机presume 。不同于透视相机,正交相机在屏幕上显示更多的东西更大的分辨率,你有。

您需要规范化正交大小以通缉分辨率:

  camera.orthographicSize = 640 /屏幕宽度* screenheight / 2
 

在C中的垂直尺寸上$ C $标准化为640像素宽度。 (链接

I developed one 2-D game in unity. First I have run game on web player my screen is cutting from both ends. Now I am creating android apk from it. Then on android devices its also cutting from both ends. We are using free aspect ratio. I dont know much about screen size. So how to overcome from this problem?

CameraFollow.cs script-

using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour 
{
    public float xMargin = 1f;      // Distance in the x axis the player can move before the camera follows.
    public float yMargin = 1f;      // Distance in the y axis the player can move before the camera follows.
    public float xSmooth = 8f;      // How smoothly the camera catches up with it's target movement in the x axis.
    public float ySmooth = 8f;      // How smoothly the camera catches up with it's target movement in the y axis.
    public Vector2 maxXAndY;        // The maximum x and y coordinates the camera can have.
    public Vector2 minXAndY;        // The minimum x and y coordinates the camera can have.


    private Transform player;       // Reference to the player's transform.


    void Awake ()
    {
        // Setting up the reference.
        player = GameObject.FindGameObjectWithTag("Player").transform;
        //camera.orthographicSize = 640 / screenwidth * screenheight / 2;
    }


    bool CheckXMargin()
    {
        // Returns true if the distance between the camera and the player in the x axis is greater than the x margin.
        return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
    }


    bool CheckYMargin()
    {
        // Returns true if the distance between the camera and the player in the y axis is greater than the y margin.
        return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
    }


    void FixedUpdate ()
    {
        TrackPlayer();
    }


    void TrackPlayer ()
    {
        // By default the target x and y coordinates of the camera are it's current x and y coordinates.
        float targetX = transform.position.x;
        float targetY = transform.position.y;

        // If the player has moved beyond the x margin...
        if(CheckXMargin())
            // ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
            targetX = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.deltaTime);

        // If the player has moved beyond the y margin...
        if(CheckYMargin())
            // ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
            targetY = Mathf.Lerp(transform.position.y, player.position.y, ySmooth * Time.deltaTime);

        // The target x and y coordinates should not be larger than the maximum or smaller than the minimum.
        targetX = Mathf.Clamp(targetX, minXAndY.x, maxXAndY.x);
        targetY = Mathf.Clamp(targetY, minXAndY.y, maxXAndY.y);

        // Set the camera's position to the target position with the same z component.
        transform.position = new Vector3(targetX, targetY, transform.position.z);
    }
}

解决方案

Since you are making a 2D game, I presume you are using the orthogonal camera. Unlike perspective camera, the orthogonal camera shows more stuff on the screen the bigger resolution you have.

You need to normalize the orthogonal size to a wanted resolution:

 camera.orthographicSize = 640/screenwidth * screenheight/2

In the above code the orthogonal size is normalized to 640 pixel width. (Link)

这篇关于屏幕团结的2-D游戏裁剪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆