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);
コード制御トグル
(バグがそのような問題を引き起こしていると思われます、現在のUnityバージョン2018.3.11f1)
toggle.isOn = false;
toggle.TrySetEnable(false); //如果从代码改了isOn,但显示没有改变
toggle.TrySetEnable(true); //如果从代码改了isOn,但显示没有改变
ちなみに、トグルを監視してその値を直接取得したい場合は、
void Start()
{
toggle.onValueChanged.AddListener((bool value) => OnToggleSwith(value));
}
void OnToggleSwith(bool value)
{
//...
}
InputFieldカーソル位置を切り替えるTabキー
インターネット上にはたくさんの記事がありますが、ボタンをクリックしたときに次の記事を特定する必要はないと思います。結局のところ、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のインタラクティブな無効化は無効になりません。必要に応じて、次のスクリプトを使用してください。
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;
}
}
}
}
自動的に引き伸ばされたチャットバブル
ゲームManniuから転載、
追加される原則
続行すると、ScrollContentが自動的に引き伸ばされます
ダブルクリックして終了します
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));
}