[英]How should I Maintain aspect ratio in Unity c# and set the 3D object to scale within the aspect ratio of the selected aspect ratio?
在我的情況下,要求是廣告牌,廣告牌(3D 立方體對象)必須在遵循縱橫比的同時保持或放大,如果它與下面的枚舉中的選擇縱橫比類型匹配,則其顏色將為綠色。 我在這里忽略了 z 軸,所以一切正常。 如果您從檢查器更改縱橫比,您可以立即看到此代碼的操作。 我已經對其進行了測試,並且在我的情況下它可以正常工作。 有一個復選框可以控制廣告牌(3D 立方體對象)的大小是否必須按高度或寬度保持,您可以使用任何您喜歡的方式並控制附加腳本的 3D 對象。
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
public class AspectRatioHandler : MonoBehaviour
{
public ImmersiveAdAspectRatioSize AdAspectRatio = ImmersiveAdAspectRatioSize.OneByOne;
public bool aspectRatioByWidth = false;
private bool isAllowedChanges = true;
public bool IsAllowedChanges
{
get { return isAllowedChanges; }
set { isAllowedChanges = value; }
}
private void ChangeAspectRatioAccordingly()
{
if (IsAllowedChanges && transform.localScale.x > 0f && transform.localScale.y > 0f)
{
float width = transform.localScale.x;
float height = transform.localScale.y;
//float gcdNumber = GCD(width, height);
//Debug.LogError("Changing accordingly the Aspect Ratio");
if (AdAspectRatio == ImmersiveAdAspectRatioSize.OneByOne)
{
height = width; //Setting the height and width same
GetComponent<Renderer>().sharedMaterial.color = Color.green;
transform.localScale = new Vector3(width, height, transform.localScale.z);
}
else if (AdAspectRatio == ImmersiveAdAspectRatioSize.ThreeByTwo)
{
if (aspectRatioByWidth)
{
float constant = Mathf.Round(3f / 2f);
var height_1 = Mathf.Round((width / 3) * 2);
transform.localScale = new Vector3(transform.localScale.x, height_1, transform.localScale.z);
float aspectDivision = Mathf.Round(transform.localScale.x / transform.localScale.y);
if (aspectDivision.Equals(constant))
{
GetComponent<Renderer>().sharedMaterial.color = Color.green;
}
else
{
GetComponent<Renderer>().sharedMaterial.color = Color.red;
}
}
else
{
float constant = Mathf.Round(3f / 2f);
var width_1 = Mathf.Round((height / 3) * 2);
transform.localScale = new Vector3(width_1, transform.localScale.y, transform.localScale.z);
float aspectDivision = Mathf.Round(transform.localScale.x / transform.localScale.y);
if (aspectDivision.Equals(constant))
{
GetComponent<Renderer>().sharedMaterial.color = Color.green;
}
else
{
GetComponent<Renderer>().sharedMaterial.color = Color.red;
}
}
}
else if (AdAspectRatio == ImmersiveAdAspectRatioSize.FourByThree)
{
if (aspectRatioByWidth)
{
float constant = Mathf.Round(4f / 3f);
var height_1 = Mathf.Round((width / 4) * 3);
transform.localScale = new Vector3(transform.localScale.x, height_1, transform.localScale.z);
float aspectDivision = Mathf.Round(transform.localScale.x / transform.localScale.y);
if (aspectDivision.Equals(constant))
{
GetComponent<Renderer>().sharedMaterial.color = Color.green;
}
else
{
GetComponent<Renderer>().sharedMaterial.color = Color.red;
}
}
else
{
float constant = Mathf.Round(4f / 3f);
var width_1 = Mathf.Round((height / 4) * 3);
transform.localScale = new Vector3(width_1, transform.localScale.y, transform.localScale.z);
float aspectDivision = Mathf.Round(transform.localScale.x / transform.localScale.y);
if (aspectDivision.Equals(constant))
{
GetComponent<Renderer>().sharedMaterial.color = Color.green;
}
else
{
GetComponent<Renderer>().sharedMaterial.color = Color.red;
}
}
}
else if (AdAspectRatio == ImmersiveAdAspectRatioSize.SixteenByNine)
{
if (aspectRatioByWidth)
{
float constant = Mathf.Round(16f / 9f);
var height_1 = Mathf.Round((width / 16) * 9);
transform.localScale = new Vector3(transform.localScale.x, height_1, transform.localScale.z);
float aspectDivision = Mathf.Round(transform.localScale.x / transform.localScale.y);
if (aspectDivision.Equals(constant))
{
GetComponent<Renderer>().sharedMaterial.color = Color.green;
}
else
{
GetComponent<Renderer>().sharedMaterial.color = Color.red;
}
}
else
{
float constant = Mathf.Round(16f / 9f);
var width_1 = Mathf.Round((height / 16) * 9);
transform.localScale = new Vector3(width_1, transform.localScale.y, transform.localScale.z);
float aspectDivision = Mathf.Round(transform.localScale.x / transform.localScale.y);
if (aspectDivision.Equals(constant))
{
GetComponent<Renderer>().sharedMaterial.color = Color.green;
}
else
{
GetComponent<Renderer>().sharedMaterial.color = Color.red;
}
}
}
else if (AdAspectRatio == ImmersiveAdAspectRatioSize.EighteenBySix)
{
if (aspectRatioByWidth)
{
float constant = Mathf.Round(18f / 6f);
var height_1 = Mathf.Round((width / 18) * 6);
transform.localScale = new Vector3(transform.localScale.x, height_1, transform.localScale.z);
float aspectDivision = Mathf.Round(transform.localScale.x / transform.localScale.y);
if (aspectDivision.Equals(constant))
{
GetComponent<Renderer>().sharedMaterial.color = Color.green;
}
else
{
GetComponent<Renderer>().sharedMaterial.color = Color.red;
}
}
else
{
float constant = Mathf.Round(18f / 6f);
var width_1 = Mathf.Round((height / 18) * 6);
transform.localScale = new Vector3(width_1, transform.localScale.y, transform.localScale.z);
float aspectDivision = Mathf.Round(transform.localScale.x / transform.localScale.y);
if (aspectDivision.Equals(constant))
{
GetComponent<Renderer>().sharedMaterial.color = Color.green;
}
else
{
GetComponent<Renderer>().sharedMaterial.color = Color.red;
}
}
}
}
else
{
GetComponent<Renderer>().sharedMaterial.color = Color.red;
}
}
private void Reset()
{
ChangeAspectRatioAccordingly();
}
// Update is called once per frame
void Update()
{
ChangeAspectRatioAccordingly();
}
}
//請記住,在這種情況下,我忽略了 localScale.z,因為不需要像大小一樣將其保留在廣告牌中。
//這是要保持的縱橫比的枚舉
//用戶從檢查器中選擇縱橫比類型,3D游戲對象會自動調整到它的大小。
//這是我用來保持給定bolow的縱橫比的代碼示例
public enum ImmersiveAdAspectRatioSize
{
EighteenBySix = 0,
SixteenByNine = 1,
FourByThree = 2,
ThreeByTwo = 3,
OneByOne = 4,
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.