设置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);
offsetMin (1,2)
offsetMax (3,4)
Button Onclick 传参
m_btn.onClick.AddListener(delegate { this.OnClickDoWhat(temp); });
代码控制toggle
(怀疑是Bug导致要这么麻烦,目前Unity版本2018.3.11f1)
toggle.isOn = false;
toggle.TrySetEnable(false); //如果从代码改了isOn,但显示没有改变
toggle.TrySetEnable(true); //如果从代码改了isOn,但显示没有改变
顺带一提,如果想对Toggle使用监听并直接获得其value可以使用
void Start()
{
toggle.onValueChanged.AddListener((bool value) => OnToggleSwith(value));
}
void OnToggleSwith(bool value)
{
//...
}
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;
}
}
}
}
循环播放文字
Text showtxt;
string[] loopText;
void OnEnable()
{
StartCoroutine(SetText());
}
void OnDisable()
{
StopAllCoroutines();
}
IEnumerator SetText()
{
int sec=0;
while(true)
{
showtxt.text = loopText[sec++ % loopText.Length];
yield return new WaitForSecondsRealtime(0.3f);
}
}
自动拉伸的聊天气泡
原理待补
承上,自动拉伸ScrollContent
紧贴动态长度文字的单位/图标
比如UI上需要显示100米,这个100可能会变成1000000,而这个米是一个图标,要紧贴在0后,这个时候就要用ContentSizeFitter绑定。
长度会变动的文字上加一个ContentSizeFitter,懒得调的话都设成PreferredSize就行,这个情况下文字只能置中,如果需要靠左或右(而图标放在另一侧),就需要自己写脚本用RectTransform的offset替代。
将图标放在文字的子物体里面,然后如果单位在右边的话,图标设置成右对齐,反之亦然,然后把它的位置拖拉到文字框框外面。
双击退出
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));
}