buttonを点滅させるコードです.
Buttonに何も操作していない間の色のみ点滅します.
使用方法:点滅させたいbuttonに以下のコンポーネントを追加してください.
c#
1using System.Collections;
2using System.Collections.Generic;
3using UnityEngine;
4using UnityEngine.UI; //Buttonを使用するため追加
5
6public class buttonblink : MonoBehaviour
7{
8
9 // ボタンのコンポーネント
10 Button button;
11
12 // カウンタ
13 int cnt;
14
15 // 点滅の速さを設定(60の場合,30フレームごとに色が変わる)
16 public int MAX_COUNT = 60;
17
18 // 点滅色の設定
19 public List<Color> colors = new List<Color>() {new Color(1,1,1,1),new Color(1,1,1,0)};
20
21
22 // Start is called before the first frame update
23 void Start()
24 {
25 //ボタンのコンポーネントを設定
26 button = GetComponent<Button>();
27 //カウンタの初期値を0に設定
28 cnt = 0;
29 }
30
31 // Update is called once per frame
32 void Update()
33 {
34 cnt++;
35 cnt%=MAX_COUNT;
36 var cls = button.colors;
37 cls.normalColor = colors[cnt/(MAX_COUNT/colors.Count)];
38 button.colors = cls;
39 }
40}
41
応用:Inspectorから次の操作が可能です.
- MAX_COUNTの値により点滅速度の変化
- colorsの色を変えて点滅色の変化
- colorsの要素数(size)を増やして点滅色数の追加
※colorsの要素数は0にするとエラーになります
※MAX_COUNTは0にするとエラーになります.