代码控制 UGUI相关

设置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)

设置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);

代码控制toggle

(怀疑是Bug导致要这么麻烦,目前Unity版本2018.3.11f1)

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

Tab键切换InputField光标位置

这个网上有好多文章,但是都是点击按钮时再去识别下一个,我觉得没必要,毕竟很少见InputField会改变。

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实现多选

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

将脚本放到Toggle的父物体上,然后Toggle各自在OnValueChanged处引用表明自身。支持多个MultipleChoiceToggle同时使用。
表明自身这个动作可以减少很多代码步骤
上述方法没有将Toggle的Interactive disable掉,如需则用下述脚本

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

自动拉伸的聊天气泡

转载自游戏蛮牛
代码控制 UGUI相关_第1张图片代码控制 UGUI相关_第2张图片
原理待补

承上,自动拉伸ScrollContent

代码控制 UGUI相关_第3张图片

双击退出

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

短暂提示

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

你可能感兴趣的:(U3D)