Control de código relacionado con UGUI

Establecer valor RGB

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)

Establecer 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);

Alternar control de código

(Se sospecha que un error está causando tal problema, la versión actual de Unity 2018.3.11f1)

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

Por cierto, si desea monitorear el Toggle y obtener su valor directamente, puede usar

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

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

Tecla de tabulación para cambiar la posición del cursor InputField

Hay muchos artículos en Internet, pero no creo que sea necesario identificar el siguiente cuando hago clic en el botón. Después de todo, es raro que InputField cambie.

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 realiza una selección múltiple

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

Coloque el script en el objeto principal de Toggle, y luego cada Toggle se refiere a sí mismo en OnValueChanged. Admite múltiples MultipleChoiceToggle para usar simultáneamente.
Muestre que esta acción puede reducir muchos pasos de código
El método anterior no desactiva la desactivación interactiva de Toggle. Si es necesario, utilice la siguiente secuencia de comandos

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

Burbujas de chat estiradas automáticamente

Reimpreso del juego Manniu, el
Inserte la descripción de la imagen aquíInserte la descripción de la imagen aquí
principio que se agregará

Continuando, estire automáticamente ScrollContent

Inserte la descripción de la imagen aquí

Haga doble clic para salir

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

Recordatorio breve

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

Supongo que te gusta

Origin blog.csdn.net/MikeW138/article/details/91872444
Recomendado
Clasificación