分辨率适配:一个简单的刘海屏适配,ui适配位移,以及正交相机根据分辨率自适应宽度其中ui根据锚点自适配位置

ChangeCamera脚本挂在相机上,ScreenMatchEditor脚本放在editor文件夹下,ScreenMatch脚本挂在需要适配的ui上。刘海屏适配的话要简单的感觉就只能做到目前适配机型的最大刘海长度,如果要完美一点的话感觉就要根据机型进行对应适配了。
ChangeCamera:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeCamera : MonoBehaviour {
    public float camInitSize;// 初始化视口大小
    public float originalWidth;// 初始化宽
    public float originalHeight;//初始化高


    private float size;//实际视口大小
    private float width;// 实际宽
    private float height;//实际高   

    void Awake()
    {
        width = Screen.width;
        height = Screen.height;
        size = (camInitSize * (originalWidth / originalHeight)) / (width / height);
        GetComponent<Camera>().orthographicSize = size;
    }
}

ScreenMatchEditor:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;


/// 
/// 按类型显示需要的变量
/// 
[CustomEditor(typeof(ScreenMatch))]
public class ScreenMatchEditor : Editor {
    private SerializedObject obj;
    private ScreenMatch match;
    private SerializedProperty stretchOffset;
    private SerializedProperty moveOffset;
    void OnEnable()
    {
        obj = new SerializedObject(target);
        stretchOffset = obj.FindProperty("stretchOffset");
        moveOffset = obj.FindProperty("moveOffset");
    }
    public override void OnInspectorGUI()
    {
        match = (ScreenMatch)target;
        match.uitype = (ScreenMatch.UIType)EditorGUILayout.EnumPopup("Match Type", match.uitype);
        if (match.uitype == ScreenMatch.UIType.Stretch)
        {
            EditorGUILayout.PropertyField(stretchOffset);
        }
        if (match.uitype == ScreenMatch.UIType.CenterMove)
        {
            EditorGUILayout.PropertyField(moveOffset);
            match.location = (ScreenMatch.Location)EditorGUILayout.EnumPopup("Location",match.location);
        }
    }
}  

ScreenMatch:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// 
/// 刘海屏适配
/// 
public class ScreenMatch : MonoBehaviour
{
    private Camera mainCamera;
    private float originalCameraSize = 6.4f;
    /// 
    /// UI需要的变化
    /// 
    public enum UIType{
        DownMove,//下移
        UpMove,//上移
        Stretch,//拉伸
        LeftMove,//左移
        RightMove,//右移
        CenterMove,//中间靠拢,适用于正交相机
    }
    /// 
    /// 锚点位置
    /// 
    public enum Location {
        top,
        bottom
    }

    public enum GameType {
        Vertical,//竖屏游戏
        Horizontal//横屏游戏
    }
    public Location location;
    public GameType gameType;
    public UIType uitype;
    public float stretchOffset;
    public float moveOffset;
    private float width;
    private float height;
    private float maxMoveDistance = 90;//最大刘海长度
    private float originalZ = 1280f / 720f;//基础分辨率
    private float tempy, tempx, originaly, originalx, originalHeight, originalWidth, tempz, newHeight, newy, newWidth, newx, cameraOffset;
    private Vector2 tempv;
    private void Start()
    {
        width = Screen.width;
        height = Screen.height;
        mainCamera = Camera.main;
        Match();
    }


    public void Match()
    {
        float z = height / width;
        RectTransform t = gameObject.GetComponent<RectTransform>();
        if (z > 2)
        {
            if (gameType == GameType.Vertical)
            {
                switch (uitype)
                {
                    case UIType.DownMove:
                        tempy = t.anchoredPosition.y;
                        tempx = t.anchoredPosition.x;
                        tempv = new Vector2(tempx, tempy - maxMoveDistance);
                        gameObject.GetComponent<RectTransform>().anchoredPosition = tempv;
                        break;
                    case UIType.Stretch:
                        originaly = t.anchoredPosition.y;
                        originalx = t.anchoredPosition.x;
                        originalHeight = t.rect.height;
                        originalWidth = t.rect.width;
                        tempz = Math.Abs(z - originalZ);
                        if (originalHeight > maxMoveDistance)
                        {
                            newHeight = originalHeight + (originalHeight / originalZ) * tempz - maxMoveDistance + stretchOffset;
                            newy = originaly + (originaly / originalZ) * tempz - maxMoveDistance / 2 + stretchOffset / 2;
                        }
                        else
                        {
                            newHeight = originalHeight + (originalHeight / originalZ) * tempz + stretchOffset;
                            newy = originaly + (originaly / originalZ) * tempz + stretchOffset / 2;
                        }
                        t.sizeDelta = new Vector2(originalWidth, newHeight);
                        t.anchoredPosition = new Vector2(originalx, newy);
                        break;
                    case UIType.UpMove:
                        tempy = t.anchoredPosition.y;
                        tempx = t.anchoredPosition.x;
                        tempv = new Vector2(tempx, tempy + maxMoveDistance);
                        gameObject.GetComponent<RectTransform>().anchoredPosition = tempv;
                        break;
                    case UIType.LeftMove:
                        tempy = t.anchoredPosition.y;
                        tempx = t.anchoredPosition.x;
                        tempv = new Vector2(tempx - maxMoveDistance, tempy);
                        gameObject.GetComponent<RectTransform>().anchoredPosition = tempv;
                        break;
                    case UIType.RightMove:
                        tempy = t.anchoredPosition.y;
                        tempx = t.anchoredPosition.x;
                        tempv = new Vector2(tempx + maxMoveDistance, tempy);
                        gameObject.GetComponent<RectTransform>().anchoredPosition = tempv;
                        break;
                    case UIType.CenterMove:
                        if (location == Location.top)
                        {
                            
                            cameraOffset = 100 * (mainCamera.orthographicSize - originalCameraSize);
                            tempy = t.anchoredPosition.y;
                            tempx = t.anchoredPosition.x;
                            tempv = new Vector2(tempx, tempy - cameraOffset);
                            gameObject.GetComponent<RectTransform>().anchoredPosition = tempv;
                        }
                        else if (location == Location.bottom)
                        {
                            cameraOffset = 100 * (mainCamera.orthographicSize - originalCameraSize);
                            tempy = t.anchoredPosition.y;
                            tempx = t.anchoredPosition.x;
                            tempv = new Vector2(tempx, tempy + cameraOffset);
                            gameObject.GetComponent<RectTransform>().anchoredPosition = tempv;
                        }
                        break;
                }
            }
            else if(gameType == GameType.Horizontal)
            {
                switch (uitype)
                {                    
                    case UIType.Stretch:
                        originaly = t.anchoredPosition.y;
                        originalx = t.anchoredPosition.x;
                        originalHeight = t.rect.height;
                        originalWidth = t.rect.width;
                        tempz = Math.Abs(z - originalZ);
                        if (originalHeight > maxMoveDistance)
                        {
                            newWidth = originalWidth + (originalWidth / originalZ) * tempz - maxMoveDistance + stretchOffset;
                            newx = originalx + (originalx / originalZ) * tempz - maxMoveDistance / 2 + stretchOffset / 2;
                        }
                        else
                        {
                            newWidth = originalWidth + (originalWidth / originalZ) * tempz + stretchOffset;
                            newx = originalx + (originalx / originalZ) * tempz + stretchOffset / 2;
                        }
                        t.sizeDelta = new Vector2(newWidth, originalHeight);
                        t.anchoredPosition = new Vector2(newx, originaly);
                        break;
                    case UIType.LeftMove:
                        tempy = t.anchoredPosition.y;
                        tempx = t.anchoredPosition.x;
                        tempv = new Vector2(tempx - maxMoveDistance, tempy);
                        gameObject.GetComponent<RectTransform>().anchoredPosition = tempv;
                        break;
                    case UIType.RightMove:
                        tempy = t.anchoredPosition.y;
                        tempx = t.anchoredPosition.x;
                        tempv = new Vector2(tempx + maxMoveDistance, tempy);
                        gameObject.GetComponent<RectTransform>().anchoredPosition = tempv;
                        break;
                    case UIType.CenterMove:
                        if(location == Location.top)
                        {
                            cameraOffset = 100 * (mainCamera.orthographicSize - originalCameraSize);
                            tempy = t.anchoredPosition.y;
                            tempx = t.anchoredPosition.x;
                            tempv = new Vector2(tempx - cameraOffset, tempy);
                            gameObject.GetComponent<RectTransform>().anchoredPosition = tempv;
                        }
                        else if(location == Location.bottom)
                        {
                            cameraOffset = 100 * (mainCamera.orthographicSize - originalCameraSize);
                            tempy = t.anchoredPosition.y;
                            tempx = t.anchoredPosition.x;
                            tempv = new Vector2(tempx + cameraOffset, tempy);
                            gameObject.GetComponent<RectTransform>().anchoredPosition = tempv;
                        }
                        break;
                }
            }
        }
    }

 
}

你可能感兴趣的:(游戏开发中~~~)