Code control UGUI related

Set RGB value

Color nameColor = Color.gray;								//直接指定顏色
Color topicColor= new color32(80, 80, 80, 255);				//RGB (0-255)
Color bodyColor = new color(0.313f, 0.313f, 0.313f, 1); 	//RGB (0-1.0)

Set RectTransform

//改变RectTransform的四角
GetComponent<RectTransform>().offsetMax = new Vector2(-left, -top);
GetComponent<RectTransform>().offsetMin = new Vector2(right, bottom);

//改变RectTransform的大小
GetComponent<RectTransform>().sizeDelta = new Vector2(width, height);

//改变RectTransform的坐标
GetComponent<RectTransform>().anchoredPosition3D = new Vector3(x, y, z);
GetComponent<RectTransform>().anchoredPosition = new Vector2(x, y);

Code control toggle

(It is suspected that a bug is causing such trouble, the current Unity version 2018.3.11f1)

toggle.isOn = false;		
toggle.TrySetEnable(false);	//如果从代码改了isOn,但显示没有改变
toggle.TrySetEnable(true);	//如果从代码改了isOn,但显示没有改变

By the way, if you want to monitor the Toggle and get its value directly, you can use

void Start()
{
    
    
	toggle.onValueChanged.AddListener((bool value) => OnToggleSwith(value));
}

void OnToggleSwith(bool value)
{
    
    
	//...
}

Tab key to switch InputField cursor position

There are a lot of articles on the Internet, but I don’t think it is necessary to identify the next one when I click the button. After all, it is rare that InputField will change.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UIInputNavigator : MonoBehaviour, ISelectHandler, IDeselectHandler
{
    
    
    public Selectable nextInputField;
    private EventSystem system;
    private bool isSelect = false;
    
    void Start()
    {
    
    
        system = EventSystem.current;
    }

    void Update()
    {
    
    
        if (Input.GetKeyDown(KeyCode.Tab) && isSelect)
        {
    
    
            StartCoroutine(Wait(nextInputField));
        }
    }

    IEnumerator Wait(Selectable next)
    {
    
    
        yield return new WaitForEndOfFrame();
        system.SetSelectedGameObject(next.gameObject, new BaseEventData(system));
    }

    public void OnSelect(BaseEventData eventData)
    {
    
    
        isSelect = true;
    }

    public void OnDeselect(BaseEventData eventData)
    {
    
    
        isSelect = false;
    }
}

ToggleGroup realizes multiple selection

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

public class MultipleChoiceToggle : MonoBehaviour
{
    
    
    public float maxChoiceNum;
    float curChoiceNum;

    public void OnClickToggle(Toggle thisToggle)
    {
    
    
        if (thisToggle.isOn)
        {
    
    
            curChoiceNum += 1;
            if (curChoiceNum > maxChoiceNum)
            {
    
    
                thisToggle.isOn = false;
            }
        }
        else
        {
    
    
            curChoiceNum -= 1;
            thisToggle.isOn = false;
        }
    }
}

Put the script on the parent object of Toggle, and then each Toggle refers to itself at OnValueChanged. Support multiple MultipleChoiceToggle to use simultaneously.
Show that this action can reduce a lot of code steps
The above method does not disable Toggle's Interactive disable. If necessary, use the following script

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

public class MultipleChoiceToggle : MonoBehaviour
{
    
    
    public Toggle[] toggleInGroup;
    public float maxChoiceNum;
    float curChoiceNum;

    public void OnClickToggle(Toggle thisToggle)
    {
    
    
        if (thisToggle.isOn)
        {
    
    
            curChoiceNum += 1;
            if (curChoiceNum > maxChoiceNum)
            {
    
    
                thisToggle.isOn = false;
            }
            if (curChoiceNum == maxChoiceNum)
            {
    
    
                ToggleInteraction(false);
            }
        }
        else
        {
    
    
            curChoiceNum -= 1;
            ToggleInteraction(true);
            thisToggle.isOn = false;
        }
    }

    void ToggleInteraction(bool value)
    {
    
    
        for (int i = 0; i < toggleInGroup.Length; i++)
        {
    
    
            if (!toggleInGroup[i].isOn)
            {
    
    
                toggleInGroup[i].interactable = value;
            }
        }
    }
}

Automatically stretched chat bubbles

Reprinted from the game Manniu, the
Insert picture description hereInsert picture description here
principle to be added

Continuing, automatically stretch ScrollContent

Insert picture description here

Double click to exit

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

public class quitBtn: MonoBehaviour
{
    
    
    private float timer = 0;
    private readonly float waitTime = 2f;

    private bool btnDown = false;

    // Use this for initialization
    void Start()
    {
    
    

    }

    // Update is called once per frame
    void Update()
    {
    
    
        if (Input.GetKeyDown(KeyCode.Escape))
        {
    
    
            OnClickQuitButton();
        }

        if (btnDown == true)
        {
    
    
            timer += Time.deltaTime;
            if (timer > waitTime)
            {
    
    
                btnDown = false;
                timer = 0;
            }
        }
    }

    public void OnClickQuitButton()
    {
    
    
        if (btnDown == false)
        {
    
    
            btnDown = true;
            timer = 0;
        }
        else
        {
    
    
            Application.Quit();
        }
    }
}

Short reminder

    IEnumerator playFrame(GameObject frame, float waitTime)
    {
    
    
        frame.SetActive(true);
        yield return new WaitForSeconds(waitTime);
        frame.SetActive(false);
    }

    void showFrame(GameObject frame, string str)
    {
    
    
        frame.GetChild(0).GetComponent<Text>().text = str;
        StartCoroutine(playFrame(frame, 1.0f));
    }

Guess you like

Origin blog.csdn.net/MikeW138/article/details/91872444