This is the 24th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Recommended reading

  • CSDN home page
  • GitHub open source address
  • Unity3D plugin sharing
  • Jane’s address book
  • My personal blog
  • QQ group: 1040082875

Hello everyone, I am a Buddhist engineer ☆ quiet small magic dragon ☆, update Unity development skills from time to time, think useful remember one key three link oh.

One, foreword

The attributes and usage methods of each UGUI component, such as Text, Button, Image, Toggle, InputField, ScrollView, etc.

Then share some principles of UGUI, such as UGUI rendering mode, UGUI zoom calculation, UGUI tracing point positioning, UGUI automatic layout and so on.

I believe you will have a more comprehensive understanding of UGUI after reading.

Next, I’ll share an example of the UGUI UI component being applied.

2. Property configuration of Canvas and CanvasScaler

Three, code implementation

void Start ()   
{  
        float standard_width = 960f;        // The initial width
        float standard_height = 640f;       // Initial height
        float device_width = 0f;                // The current device width
        float device_height = 0f;               // Current device height
        float adjustor = 0f;         // Screen correction ratio
        // Get the device width and height
        device_width = Screen.width;  
        device_height = Screen.height;  
        // Calculate the width to height ratio
        float standard_aspect = standard_width / standard_height;  
        float device_aspect = device_width / device_height;  
        // Calculate the correction ratio
        if (device_aspect < standard_aspect)  
        {  
            adjustor = standard_aspect / device_aspect;  
        }  
  
        CanvasScaler canvasScalerTemp = transform.GetComponent<CanvasScaler>();  
        if (adjustor == 0)  
        {  
            canvasScalerTemp.matchWidthOrHeight = 1;  
        }  
        else  
        {  
            canvasScalerTemp.matchWidthOrHeight = 0; }}Copy the code