Wajid Ali
Wajid Ali

Reputation: 39

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?

I want to maintain the aspect ratio of the 3D object in the scene. I just have to the pass the enum then the aspect ratio can be maintained either by width or height it will depend on the check.

The check will decide if it is handled by the width or height. The aspect ratio will only change the X, Y of the GameObject scale ignoring the z.

The script needs to be executed in the editor as well as runtime. Here is the enum that I want to use for the GameObject.

The user select the aspect ratio type from the inspector and the 3D GameObject is automatically adjusted to the size of it in the editor and also in runtime.

This is the sample of the Enum that will be used to keep the aspect ratio of the GameObject

public enum ImmersiveAdAspectRatioSize
{
    EighteenBySix = 0,
    SixteenByNine = 1,
    FourByThree = 2,
    ThreeByTwo = 3,
    OneByOne = 4,
}

Remember the localScale.z needs to be ignored in this case as it was not needed.

Upvotes: 0

Views: 1197

Answers (1)

Wajid Ali
Wajid Ali

Reputation: 39

In my case the requirement is billboard and the billboard (3D Cube object) has to be maintained or scaled up while following the aspect ratio and its color would be green if it matches the select aspect ratio type like in the enum below. I am ignoring the z-axis over here so everything is working as it should. If you change the aspect ratio from the inspector you can see the action of this code rightaway. I have tested it and it is working as it should in my case. There is a checkbox which controls whether the size of the billboard (3D Cube object) has to be maintained by height or width you can use whichever you like and take control of the 3D object on which the script is attached.

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();
    }
}

Upvotes: -1

Related Questions