当前位置: 首页 > news >正文

凉鞋的 Unity 笔记 201. 第三轮循环:引入变量

201. 第三轮循环:引入变量

在这一篇,我们进行第三轮 编辑-测试 循环。

在之前我们编写了 输出 Hello Unity 的脚本,如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class FirstGameObject : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){print("Hello Unity"); // +}// Update is called once per framevoid Update(){}
}

如果我们要输出 10 次 Hello Unity 该怎么办呢?

答案很简单,就是复制十行 print(“Hello Unity”),代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class FirstGameObject : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){print("Hello Unity"); print("Hello Unity"); print("Hello Unity"); print("Hello Unity"); print("Hello Unity"); print("Hello Unity"); print("Hello Unity"); print("Hello Unity"); print("Hello Unity"); print("Hello Unity"); }// Update is called once per framevoid Update(){}
}

这样当我们运行场景后,结果如下:

image-20231003145220344

总共输出了十次 Hello Unity。

此时我突然想要把输出十次 Hello Unity 改成输出十次 Hello C#。

那么最简单的方式,就是直接修改代码,如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class FirstGameObject : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){print("Hello C#"); print("Hello C#"); print("Hello C#"); print("Hello C#"); print("Hello C#"); print("Hello C#"); print("Hello C#"); print("Hello C#"); print("Hello C#"); print("Hello C#"); }// Update is called once per framevoid Update(){}
}

运行之后,结果如下:

image-20231003145403916

但是这样太不优雅了,我们需要复制粘贴十次,如果我们有 100 个甚至 1000 个 Hello Unity,那么我们可能需要复制粘贴很多次,或者使用代码编辑器所提供的查找/替换功能完成。

比较优雅的方式就是引入一个变量,代码如下所示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class FirstGameObject : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){var textToPrint = "Hello C#";print(textToPrint); print(textToPrint); print(textToPrint); print(textToPrint); print(textToPrint);print(textToPrint); print(textToPrint); print(textToPrint); print(textToPrint); print(textToPrint); }// Update is called once per framevoid Update(){}
}

这样当我们想要输出十次 Hello World 那么我们只需要修改变量的值即可,如下所示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class FirstGameObject : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){var textToPrint = "Hello World";print(textToPrint); print(textToPrint); print(textToPrint); print(textToPrint); print(textToPrint);print(textToPrint); print(textToPrint); print(textToPrint); print(textToPrint); print(textToPrint); }// Update is called once per framevoid Update(){}
}

这样非常优雅。

我们在代码中新引入的 textToPrint 叫做变量。

变量可以存储一个值,然后再接下来可以通过这个变量来代替具体的值。

比如 textToPrint 是 “Hello World”,那么接下来的每一句 print(textToPrint) 其实都是 print(“Hello World”)。

变量给编程带来了巨大的便利。

当然,变量是每一个程序语言都有的,而每一个游戏引擎不管是提供专业脚本支持还是可视化脚本支持都会提供变量的使用,所以变量也是通识部分的内容,再接下来的篇幅里,笔者会好好介绍变量,以及 C# 中的变量使用。

这一篇内容就这些,我们下一篇再见,拜拜。

知识地图

image-20231003150833296
转载请注明 凉鞋的笔记

http://www.lryc.cn/news/197716.html

相关文章:

  • 小魔推短视频裂变工具,如何帮助实体行业降本增效?
  • VBA技术资料MF71:查找所有空格并替换为固定字符
  • c++小知识
  • C#上位机序列9: 批量读写+事件广播
  • ARM +FPGA GPIB IP核实现
  • 有消息称苹果Vision Pro会有廉价版
  • jenkins整合gerrit
  • PMP考完后应该考什么?
  • 科技资讯|苹果Vision Pro可通过手势ID检测不同用户
  • CUDA编程模型- 层次结构
  • 国际站阿里云服务器无法安装程序怎么办?
  • 基于Vue+webpack之H5打包资源优化
  • C#中DataAdapter对象
  • Nginx正向代理,反向代理,负载均衡
  • 安防视频监控平台EasyCVR出现视频流播放卡顿情况,如何优化?
  • VRRP基础
  • 虚实融合 智兴百业 | 赵捷副市长莅临拓世科技集团筹备展台指导,本月19号!拓世科技集团与您相约世界VR产业大会
  • 2000-2023年省市县人工智能企业数量数据
  • CSP模拟58联测20 牵着她的手
  • 电脑版便签软件下载用哪个?
  • 别再卷组件库了,Vue 拖拽库都断代了!
  • 利用服务器打造创新的在线社区
  • CSS动画实现节流
  • Apache Log4j Server (CVE-2017-5645) 反序列化命令执行漏洞
  • 视口 css
  • Puppeteer记录操作过程及优秀的开源插件(五)
  • 联邦学习+梯度+梯度剪枝
  • 提高研发效率还得看Apipost
  • Elasticsearch使用——结合MybatisPlus使用ES es和MySQL数据一致性 结合RabbitMQ实现解耦
  • 什么是CSGO大行动,2023年CSGO大行动时间预测