This is the 23rd 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.

Ii. Introduction and effect drawing

Sometimes ICONS don’t do a good job of explaining the feature, so you need some descriptive text. For example, text can be displayed when the mouse moves over the UI. So how do you move the mouse over to display text on UGUI? As we all know, when the mouse moves over the button button, there will be changes, mainly controlled by the button componentIf you can control the color, there must be some enumeration of state capture and you found thisThe next step is to rewrite the Button class.

Effect:

Three, implementation,

1. Create a button and remove the original button component2. Create the testButton. cs script and write the script

using UnityEngine;
using UnityEngine.UI;

public class TestButton : Button
{
    enum Selection
    {
        Normal,
        Highlighted,
        Pressed,
        Disabled
    }
    Selection selection;

    protected override void DoStateTransition(SelectionState state, bool instant)
    {
        base.DoStateTransition(state, instant);
        switch (state)
        {
        	// Four states
            case SelectionState.Normal:
                selection = Selection.Normal;
                break;
            case SelectionState.Highlighted:
                selection = Selection.Highlighted;
                break;
            case SelectionState.Pressed:
                selection = Selection.Pressed;
                break;
            case SelectionState.Disabled:
                selection = Selection.Disabled;
                break;
            default:
                break; }}private void OnGUI()
    {
        GUI.skin.box.fontSize = 10;
        switch (selection)
        {
            case Selection.Highlighted:
                GUI.Box(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, 100.25), "Highlighted");
                break;
            case Selection.Pressed:
                GUI.Box(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, 100.25), "Pressed");
                break;
            default:
                break; }}}Copy the code

3. Mount it to the button buttonOK.