当前位置:网站首页>Essential C# scripting skills for Unity developers
Essential C# scripting skills for Unity developers
2022-08-11 08:11:00 【is a small A】
- 注释代码
Ctrl+K+C组合键可快速注释选中代码块或光标所在处代码行,Ctrl+K+U取消注释
- 对齐代码
按下Ctrl+K+F组合键可快速将选中代码块或光标所在处代码与上下文对齐
- 比较距离
将两点直接的距离与一个固定距离进行比较时,可使两点相减后取平方(即sqrMagnitude),然后用该值与某个距离值的平方进行比较.不建议使用Vector3.Distance方法获取两点之间的距离,然后与给定的距离值进行比较.因为Vector3.Distance(a,b)相当于(a-b).magnitude,即求平方根,而sqrMagnitude方法省去了求平方根的操作,所以比magnitude执行快,代码如下:
//建议使用:
Vector3 a = new Vector3(1, 1, 0);
Vector3 b = new Vector3(0, 0, 0);
float value=1.0f;
if((a-b).sqrMagnitude<value*value)
{
}
//不建议:
if(Vector3.Distance(a,b)<value)
{
}
- 移动代码行
按下Alt+键盘上/下键,即可使鼠标光标所在代码行上移/下移
- 自定义脚本模板
在你Unity版本安装目录下依次打开:Editor/Data/Resources/ScriptTemplates即可看到一堆脚本模板,选择你常用的脚本模板进行修改
- 随机获取布尔值
Unity里面的随机类Random中使用Random.value可以返回0~1之间的随机数,所以可以让其返回值与0.5f进行比较,就可以获取一个随机布尔值True或false,有点类似于JAVArandom class in
- 字符串性能优化
Certain strings do not change throughout the project and are frequently used,可将其存储在静态只读变量中,从而节省内存分配
static readonly string Fire1="Fire1";
void Update(){
Input.GetAxis(Fire1);
//不建议直接使用字符串
Input.GetAxis("Fire1");
}
- Initialize the cached component reference
When a component needs to be accessed frequently,可以在程序初始化时获取到该组件的引用,而不是在需要时才去获取,也就是减少使用GetComponent< T>(),从而避免由于重复获取引起的性能开销
private MeshRenderer mesh;
private void Awake()
{
mesh = GetComponent<MeshRenderer>();//初始化时提前缓存组件引用
}
void Update()
{
mesh.material.color = Color.red;
}
- Turn on Find and Replace mode
按下Ctr+F组合键可快速打开查找模式:(可选择当前文档或整个项目等查找匹配字符串)
- Navigate to a function or variable
鼠标光标放置在变量或函数方法处,按下F12可快速定位到其定义的代码行
- Visual StudioStatement auto-completion
使用VS写脚本时,输入 if、for、switch、foreach、try、while等语句后可双击Tab键完成语句补全
- 风格化Debug.Log的输出信息
使用debug.log输出字符串信息时,可以用富文本标记来强调内容
Debug.Log("<color=green>程序初始化加载资源</color>"+"bundle is loading");
- Find all references to a function or variable
鼠标光标放置在变量或函数处,按下Shift+F12组合键可快速查看其所有引用,单击某一引用即可定位到其代码行
- Scripts can be executed without being attached to a game object
一般来说,脚本都是要挂载到游戏对象上才能执行,但Unity提供了一个方法可以不用挂载脚本到游戏对象上也可以在程序运行时执行某个方法的语句.只需要在脚本的方法声明[RuntimeInitializeOnLoad(RuntimeInitializeLoadType.BeforeSceneLoad)] 即可,常用于程序初始化前做一些额外的初始化工作,代码如下:
(注意:必须是静态方法上声明才会生效)
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
private static void AfterSceneInit()
{
Debug.Log("场景加载之后执行");
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void BeforeSceneInit()
{
Debug.Log("场景加载之前执行");
}
- Compare Tagmethod to compare game objects
当使用到游戏对象的标签Tag进行比较时,从性能上考虑,建议使用CompareTag方法,而不用=去进行判断
if(gameObject.CompareTag("Player")){}
//等效于
if(gameObject.tag=="Player"){}
- 数据类型struct和class的序列化
The data type needs to be givenstruct和Class在面板上赋值,可以在struct和Class声明前添加[System.Serializable]
[System.Serializable]
public class UIData
{
public Text txt;
public Image IMG;
}
[System.Serializable]
public struct ObjData
{
public Transform pos;
public string name;
}
public class NewBehaviourScript : MonoBehaviour
{
public UIData mgUIData;
public ObjData myObjData;
}
- 高亮显示Debug.LogCorresponds to the game object
使用Debug.Log方法输出信息时,可将gameObject作为此方法的第二个参数,当程序运行时,单击Console面板中对应的输出信息,可在Hierarchy面板中高亮显示挂载了此脚本的游戏物体
private void Awake()
{
Debug.Log("程序初始化", gameObject);
}
- 检查脚本中空的Start和Updata函数
检查空的MonoBehavior Start和Update方法,无论你有无写逻辑,它Start函数都会执行一次,Update函数也是会每帧执行一次.如果没有逻辑实现就将其删掉,因为跑空函数多少会损耗效率
- 养成写成#region和#endregion的好习惯
写代码除了养成写注释的好习惯外,也要养成写#region和#endregion 的好习惯,当你的代码有成百上千,甚至更多行时,使用该语句可将两者之间包含的代码块折叠,方便阅读
以上就是我总结的UnityDevelop essential scripting skills,如果还想了解UnityFor compilation skills, see my previous article,If you have any other questions, please feel free to message me privately
边栏推荐
- C Primer Plus(6) 中文版 第1章 初识C语言 1.1 C语言的起源 1.2 选择C语言的理由 1.3 C语言的应用范围
- 经典论文-MobileNet V1论文及实践
- Square, multi-power, square root calculation in Tf
- 2022-08-10:为了给刷题的同学一些奖励,力扣团队引入了一个弹簧游戏机, 游戏机由 N 个特殊弹簧排成一排,编号为 0 到 N-1, 初始有一个小球在编号 0 的弹簧处。若小球在编号为 i 的弹
- Kotlin算法入门计算质因数
- Interaction of Pico neo3 in Unity
- There may be fields that cannot be serialized in the abnormal object of cdc and sqlserver. Is there anyone who can understand it? Help me to answer
- 【云原生】云原生在网络安全领域的应用
- opengauss创建用户权限问题
- 基于微信小程序的租房小程序
猜你喜欢

一根网线两台电脑传输文件

项目1-PM2.5预测

1036 Programming with Obama (15 points)

XXL-JOB 分布式任务调度中心搭建

Do you know the basic process and use case design method of interface testing?

1046 punches (15 points)

Write a resume like this, easy to get the interviewer

Pico neo3 Unity Packaging Settings

oracle数据库中列转行,列会有变化

小目标检测3_注意力机制_Self-Attention
随机推荐
支持各种文件快速重命名最简单的小技巧
项目1-PM2.5预测
数据库无法启动,报无法分配内存,怎么处理
抽象类和接口
CIKM 2022 AnalytiCup Competition: Federal Heterogeneous Task Learning
高德能力API
软件测试常用工具的用途及优缺点比较(详细)
opengauss创建用户权限问题
9、Neural Sparse Voxel Fields
分布式锁-Redission - 缓存一致性解决
leetcode:69. x 的平方根
1076 Wifi Password (15 points)
记录一些遇见的bug——Lombok和Mapstruct的冲突导致,A component required a bean of type ‘com.XXX.controller.converter.
go-grpc TSL authentication solution transport: authentication handshake failed: x509 certificate relies on ... ...
用 Antlr 重构脚本解释器
小目标检测3_注意力机制_Self-Attention
tf.reduce_mean() and tf.reduce_sum()
【LeetCode】链表题解汇总
流式结构化数据计算语言的进化与新选择
LoRa芯片的特征