unity游戏适配脚本【改变大小】

适配脚本。可基于高,或宽。或等比适配。

using UnityEngine;
using System.Collections;
public class PAutoFixScale : MonoBehaviour
{
    public enum FIXTYPE
    {
        OnlyWidth,
        OnlyHeight,
        Both,
    }
    public PUIStretch.Style mStyle = PUIStretch.Style.BasedOnWidth;
    public float mWidth = 1136.0f;
    public float mHeight = 768.0f;
    public FIXTYPE mFixType = FIXTYPE.Both;
    protected bool mDealFlag = false;
    void Awake()
    {
        AutoFixScale();
    }
 void Start ()
    {
        AutoFixScale();
 }
    void AutoFixScale()
    {
        if (mDealFlag == true)
        {
            return;
        }
        mDealFlag = true;
        if (mStyle == PUIStretch.Style.BasedOnHeight)
        {
            float mCameraWidth = HRSceneManager.GetCurrentScreenSize().x;//(float)Screen.width;
            float mCameraHeight = HRSceneManager.GetCurrentScreenSize().y;// (float)Screen.height;
            float w1 = mWidth * mCameraHeight / mHeight;
            if (w1 < mCameraWidth)
            {
                float mScale = mCameraWidth / w1;
                //this.transform.localScale *= mScale;
                if (mFixType == FIXTYPE.OnlyWidth)
                {
                    this.transform.localScale = new Vector3(mScale, 1.0f, 1.0f);
                }
                else if (mFixType == FIXTYPE.OnlyHeight)
                {
                    this.transform.localScale = new Vector3(1.0f, mScale, 1.0f);
                }
                else
                {
                    this.transform.localScale = new Vector3(mScale, mScale, mScale);
                }
            }
        }
        else if (mStyle == PUIStretch.Style.BasedOnWidth)
        {
            float mCameraWidth = HRSceneManager.GetCurrentScreenSize().x;//(float)Screen.width;
            float mCameraHeight = HRSceneManager.GetCurrentScreenSize().y;//(float)Screen.height;
            float h1 = mHeight * mCameraWidth / mWidth;
            if (h1 < mCameraHeight)
            {
                float mScale = mCameraHeight / h1;
                //this.transform.localScale *= mScale;
                if (mFixType == FIXTYPE.OnlyWidth)
                {
                    this.transform.localScale = new Vector3(mScale, 1.0f, 1.0f);
                }
                else if (mFixType == FIXTYPE.OnlyHeight)
                {
                    this.transform.localScale = new Vector3(1.0f, mScale, 1.0f);
                }
                else
                {
                    this.transform.localScale = new Vector3(mScale, mScale, mScale);
                }
            }
        }
        Destroy(this);
    }
}

 

本文链接:

https://bobsong.cn/138.html
1 + 1 =
快来做第一个评论的人吧~