【Unity技巧】开发技巧(技巧篇)
来源:互联网 发布:华大基因携手阿里云 编辑:程序博客网 时间:2024/04/28 11:46
写在前面
和备忘录篇一样,这篇文章旨在总结Unity开发中的一些设计技巧,当然这里只是我通过所见所闻总结的东西,如果有不对之处欢迎指出。
技巧1:把全局常量放到一个单独的脚本中
很多时候我们需要一些常量,例如是否输出Log,正式服务器和测试服务器的IP等等,我们可以把这些常量写在同一个脚本里,并设置属性为public static,然后在其他脚本里直接访问该变量即可。当代码量越来越大时,你会发现这样会减少很多查找常量的时间。而且,这样更改时也非常方便,例如当需要发布新版本时,你只要把该脚本中的log开关设为false就可以了。又或者公司更改了服务器地址,一个简单字符串的更改就可以了。
例如,我们在名为Const.cs的脚本中添加如下代码:
public class Const {public static bool IsWriteMsg = true;public static bool IsDebugBuild = true;}
其中IsWriteMsg表明是否需要将文本写到本地以供查看,IsDebugBuild表明是不是Debug模式(一般用于控制是否输出Log)。
技巧2:把全局函数放到一个单独的脚本中
和上一条类似,有时我们需要一些经常使用的、且无关对象的函数,例如解析系统某些特定含义的字符串、得到角色在场景中的位置等。我们可以把这些函数写在同一个脚本里,并设置函数属性为public static即可。
技巧3:保存字符串和JSON信息
我们经常需要和字符串打交道,例如得到服务器传来的消息等等,而且想要保存它们,复制粘贴又太麻烦了,作为一个很懒的程序员,怎么能一直重复做一件事情呢!
例如,我们在名为GlobalFunc.cs(即为技巧2中提到的全局函数脚本)的脚本中,添加如下代码(关于Json部分,需要LitJson插件,可参见这篇博文)(注意:其中有用到技巧1中的Const.cs脚本中的变量):
using UnityEngine;using System;using System.Text;using System.IO;using System.Collections;using System.Collections.Generic;using LitJson;public class GlobalFunc {static public void SaveJson(object obj, string filepathandname){Debug.LogWarning("========> SaveJson: " + filepathandname);// ELIMINATE WARNING//string levelnameLower = Application.loadedLevelName.ToLower();if(Application.isEditor){string file = "./" + filepathandname;if (File.Exists(file) ){File.Delete (file);}System.IO.TextWriter writer = new System.IO.StreamWriter(file, false);LitJson.JsonWriter jw = new JsonWriter( writer as System.IO.TextWriter );jw.PrettyPrint = true;try{LitJson.JsonMapper.ToJson( obj, jw );}catch(Exception e){UnityEngine.Debug.LogError(e);}writer.Close();}}static public void SaveText(string tex, string filepathandname){if(Const.IsWriteMsg || !Const.IsDebugBuild){return;}Debug.LogWarning("========> SaveJson: " + filepathandname);//string levelnameLower = Application.loadedLevelName.ToLower();string file = "";if(Application.platform == RuntimePlatform.Android ){file = Application.persistentDataPath+"/"+filepathandname;}else if(Application.isEditor){file = "./" + filepathandname;}if(file == ""){return ;}if (File.Exists(file) ){File.Delete (file);}System.IO.TextWriter writer = new System.IO.StreamWriter(file, false);writer.Write(tex);writer.Close();}static public string LoadText(string filepathandname){Debug.LogWarning("========> LoadJson: " + filepathandname);System.IO.TextReader r = new System.IO.StreamReader("./" + filepathandname);string tmp = r.ReadToEnd();r.Close();return tmp;}}
技巧4:自定义弹出框
弹出框,类似于Windows编程中常见的各种MessageBox,它们有固定的界面格式,程序员一般只要指定style、title和内容即可。
详见这篇博文。
技巧5:暂停游戏
关于Time.timeScale来暂停游戏的细节,请见《Unity备忘录篇》。
如果使用Time.timeScale = 0来暂停游戏,那么下面两种方法可能可以帮助你:
- 把所有的移动都放到FixedUpdate中(不太可能)
- Update中,所有的移动都使用Time.deltaTime控制
Object[] objects = FindObjectsOfType (typeof(GameObject));foreach (GameObject go in objects) { go.SendMessage ("OnPauseGame", SendMessageOptions.DontRequireReceiver);}
然后再调用OnResumeGame() 进行重启。
protected bool paused; void OnPauseGame (){ paused = true;} void OnResumeGame (){ paused = false;} void Update (){ if (!paused) {// do movement }}
这样方法有一个非常大的好处就是,你可以自己定制所有物体在暂停和重启时的行为,例如存储和加载数据等等。
技巧6:使用Vector3.Lerp移动物体
我们可以使用Lerp函数实现在两个点——start和to,进行插值,其中t是插值比率。
transform.position = Vector3.Lerp(start, to, t);
当t<=0时,Lerp函数返回start;当t>=1时,Lerp函数返回to。因此,如果你想要在某个时间内把物体从start移动到to位置,你可以通过不断增加t(通常每帧增加的值为Time.deltaTime/NumberOfSecondsToComplete)来实现。像下面这样:
Vector3 _start;Vector3 _target;float _t; void Update(){ transform.position = Vector3.Lerp(_start, _target, _t); _t += Time.deltaTime/2; //Take 2 seconds} public void SetTargetPosition(Vector3 newTargetPosition){ _start = transform.position; _target = newTargetPosition; _t = 0;}
还有一种情况就是,你想要从物体的当前位置开始进行一个平滑的移动。这时,我们需要把start替换成物体本身的位置,transform.position。
void Update(){ transform.position = Vector3.Lerp(transform.position, target.position, Time.deltaTime);}
Shader中避免If语句
可以使用lerp和step函数来代替使用If语句。例如:
/* y1, y2, b1, b2 */float4 constants = float4(5, 6, 2, 3);float2 tmp = 10 * constants.xy + constants.zw;x = lerp(tmp[1], tmp[0], step(x, 0.5));
参考:
http://gamedev.stackexchange.com/questions/45398/avoid-if-statements-in-directx-10-shaders
http://gamedev.stackexchange.com/questions/59476/using-two-shaders-instead-of-one-with-if-statements
- 【Unity技巧】开发技巧(技巧篇)
- 【Unity技巧】开发技巧(技巧篇)
- 【Unity技巧】开发技巧(技巧篇)
- 【Unity技巧】开发技巧(技巧篇)-----妈妈说女孩子要自立自强
- Unity 技巧
- 《Unity 游戏开发技巧集锦》
- Unity开发小技巧介绍
- Unity开发中的小技巧
- [Unity技巧]vs相关技巧
- Unity 4.x游戏开发技巧集锦(内部资料)
- unity问题与开发技巧汇总(转载专用)
- 技巧
- 技巧
- 技巧
- 技巧
- 技巧
- 技巧
- 技巧
- centos下7z的安装
- __malloc_ptr_t 是什么类型
- C++ 获得本地磁盘盘符的容量信息
- 当输入100和120时,无法输出no
- 卡壳卡壳(是读qia)
- 【Unity技巧】开发技巧(技巧篇)
- RVDS4.0开发OK6410(一)RVDS4.0的安装
- Java数组转字符串
- 开始python之路
- 南华大学软件测命名规则TDRE_20131104_A_7
- 数据库 简单查询 Sql Server 学生表 课程表 选课表
- java组的复制 最好不要用arraycopy ,可以用src.clone()函数
- Linux内核访问外设I/O资源的方式-静态映射
- Mahout实例