Unity C#实现对象的莫斯电码闪烁

『技巧 | 经验』 版权:禁止匿名转载;禁止商业使用。

1986 0 0 2019-04-17 举报

CG素材

未设置职业

Unity C#实现对象的莫斯电码闪烁


C#脚本设置对象在Morse Code的时间节奏下的Active / Inactive【闪烁】,如下所示:












[C#] 纯文本查看 复制代码
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;// © 2017 TheFlyingKeyboard// theflyingkeyboard.netpublic class FlashInMorseCode : MonoBehaviour { [SerializeField] private float dotTime; [SerializeField] private GameObject objectToUse; [SerializeField] private bool flashOnStart = false; [SerializeField] private bool repeat = false; [TextAreaAttribute(4, 15)] [SerializeField] private string textToShow; private float dashTime; private char[] letters = { ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' }; private string[] morseLetters = { " ", ". ___", "___ . . .", "___ . ___ .", "___ . .", ".", ". . ___ .", "___ ___ .", ". . . .", ". .", ". ___ ___ ___", "___ . ___", ". ___ . .", "___ ___", "___ .", "___ ___ ___", ". ___ ___ .", "___ ___ . ___", ". ___ .", ". . .", "_", ". . ___", ". . . ___", ". ___ ___", "___ . . ___", "___ . ___ ___", "___ ___ . .", ". ___ ___ ___ ___", ". . ___ ___ ___", ". . . ___ ___", ". . . . ___", ". . . . .", "___ . . . .", "___ ___ . . .", "___ ___ ___ . .", "___ ___ ___ ___ .", "___ ___ ___ ___ ___" }; private void Start() { if (flashOnStart) { StartCoroutine(Flash(objectToUse, textToShow, dotTime)); } } private IEnumerator Flash(GameObject objectToFlash, string textToConvert, float timeOfDot) { string textInMorse = ""; ConvertTextToMorseCode(textToConvert, out textInMorse); for (int i = 0; i < textInMorse.Length; i++) { if (textInMorse == ' ') { objectToFlash.SetActive(false); yield return 0; yield return new WaitForSeconds(timeOfDot); } else if (textInMorse == '.') { objectToFlash.SetActive(true); yield return 0; yield return new WaitForSeconds(timeOfDot); } else if(textInMorse == '_') { objectToFlash.SetActive(true); yield return 0; yield return new WaitForSeconds(timeOfDot * 3.0f); } if (repeat) { if(i == textInMorse.Length - 1) { i = 0; } } } } private void ConvertTextToMorseCode(string textToConvert, out string convertedText) { convertedText = ""; textToConvert = textToConvert.ToLower(); for (int i = 0; i < textToConvert.Length; i++) { for (short j = 0; j < 37; j++) { if (textToConvert == letters[j]) { convertedText += morseLetters[j]; convertedText += " "; break; } } } } public void WriteText(GameObject newObject = null, string newTextToShow = null, float newDotTime = -1.0f) { if (newObject != null && newTextToShow != null && newDotTime > 0.0f) { StartCoroutine(Flash(newObject, newTextToShow, newDotTime)); return; } if (newObject != null && newTextToShow != null) { StartCoroutine(Flash(newObject, newTextToShow, dotTime)); return; } if (newObject != null && newDotTime > 0.0f) { StartCoroutine(Flash(newObject, textToShow, newDotTime)); return; } if (newTextToShow != null && newDotTime > 0.0f) { StartCoroutine(Flash(objectToUse, newTextToShow, newDotTime)); return; } if (newTextToShow != null) { StartCoroutine(Flash(objectToUse, newTextToShow, dotTime)); return; } if (newDotTime > 0.0f) { StartCoroutine(Flash(newObject, textToShow, newDotTime)); return; } }}














原文标题:Unity C# Flashing Object In Morse Code
原文链接:https://www.unrealengine.com/en-US/blog/how-to-add-global-shaders-to-ue4

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

让资源更有价值

  • Archiver|
  • 手机版|
  • 小黑屋|
  • CG素材网
  • 蜀ICP备18003526号-3
  • Powered by Discuz! X3.4
  • © 2001-2017 Comsenz Inc.
  • GMT+8, 2024-4-20 05:53 , Processed in 0.315713 second(s), 34 queries .

 关注CG资源素材

快速回复 返回顶部 返回列表