Created
January 10, 2018 08:11
-
-
Save vend9520/cb028e386d0256f5b5940505273375a1 to your computer and use it in GitHub Desktop.
TextかImageを点滅させる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using UnityEngine; | |
| using UnityEngine.UI; | |
| public class Blink : MonoBehaviour { | |
| //public | |
| public float speed = 1.0f; | |
| //private | |
| private Text text; | |
| private Image image; | |
| private float time; | |
| private enum ObjType{ | |
| TEXT, | |
| IMAGE | |
| }; | |
| private ObjType thisObjType = ObjType.TEXT; | |
| void Start() { | |
| //アタッチしてるオブジェクトを判別 | |
| if (this.gameObject.GetComponent<Image>()) { | |
| thisObjType = ObjType.IMAGE; | |
| image = this.gameObject.GetComponent<Image>(); | |
| }else if (this.gameObject.GetComponent<Text>()) { | |
| thisObjType = ObjType.TEXT; | |
| text = this.gameObject.GetComponent<Text>(); | |
| } | |
| } | |
| void Update () { | |
| //オブジェクトのAlpha値を更新 | |
| if (thisObjType == ObjType.IMAGE) { | |
| image.color = GetAlphaColor(image.color); | |
| } | |
| else if (thisObjType == ObjType.TEXT) { | |
| text.color = GetAlphaColor(text.color); | |
| } | |
| } | |
| //Alpha値を更新してColorを返す | |
| Color GetAlphaColor(Color color) { | |
| time += Time.deltaTime * 5.0f * speed; | |
| color.a = Mathf.Sin(time) * 0.5f + 0.5f; | |
| return color; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment