在实际项目中,我会通过更改代码的模板文件,来优化生命周期函数的使用方式。在Unity的安装目录中,有创建代码的模版文件。通过自定义这个文件,可以更改创建默认文件的内容。文件目录为:
- Windows: Unity安装目录/Editor/Data/Resources/ScriptTemplates/81-C# Script-NewBehaviourScript.cs.txt
- Mac: Unity安装目录/Contents/Resources/ScriptTemplates/81-C# Script-NewBehaviourScript.cs.txt
根据自己团队开发人员的编程习惯,我自定义了一个代码模版。大家可以根据自己的需要做相应的更改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
using UnityEngine; using System.Collections; public class #SCRIPTNAME# : MonoBehaviour { #region Public Attributes #endregion #region Private Attributes #endregion #region Unity Messages // void Awake() // { // // } // void OnEnable() // { // // } // // void Start() // { // // } // // void Update() // { // // } // // void OnDisable() // { // // } // // void OnDestroy() // { // // } #endregion #region Public Methods #endregion #region Override Methods #endregion #region Private Methods #endregion #region Inner #endregion } |
代码模版中添加了常用的生命周期函数,并按照顺序进行排列。由于空函数也会产生性能消耗,这里采用注释的方式规避这个弊端。另外,我按用途添加了几个#region分隔函数区域。#region是C#的功能,可以标定折叠区域,方便查找对应函数。
本文出自 松阳论道 转载必须注明出处
http://blog.songyang.net/5731.html