首页 技术 正文
技术 2022年11月11日
0 收藏 363 点赞 3,326 浏览 1028 个字

平时做项目,代码中是不允许出现硬代码的,一般我们是怎么处理的呢? 那么硬代码又是什么呢?

我们俗称的硬代码:eg:   public UIlabel label; label.text = “欢迎来到梦幻岛”;  这样我们俗称为硬代码。

好了,那么该如何避免,话不多说,直接上图:

unity3D项目中如何避免硬代码(C#)

这是一种处理方式,TXT 格式的文档,前面是ID,后面是描述性文字。

可是我们该如何在项目中访问这个txt文件里面的数据呢? 话不多说,直接上代码:

 using UnityEngine;
using System.Collections; public class ReadTxt :MonoBehaviour{ public TextAsset txt;
public static ReadTxt _instance;
string fonttxt;
void Awake()
{
_instance = this;
} void Start() { } public string GetFont(int ID)
{
string[] taskinfoArray = txt.ToString().Split('\n');
foreach(string str in taskinfoArray){ string[] Fontlist = str.Split('|');
int id = int.Parse(Fontlist[]);
while (ID== id) {
fonttxt = Fontlist[];
break;
}
} return fonttxt;
} }

上述用到了单利,单利这里我们就不赘述了。

string[] taskinfoArray = txt.ToString().Split('\n');  这里有些朋友就要问了,Split('\n')是干什么的,它是分割函数,检测是否有换行操作,如果有换行我们把它存在数组里面,
上面的txt文档,我们就是写完一行,肯定要回车换行写下一句,这里就是用这个Split()函数进行检测的。
  好了说到这里,想必大家都清楚了吧。至于调用呢,这个就很简单了。
 using UnityEngine;
using System.Collections; public class TestTxt : MonoBehaviour { public UILabel label; void Start()
{
string bbb = ReadTxt._instance.GetFont(); label.text = bbb;
} }

然而label显示的 文字必定是:维护公告。  这里我们只是抛砖引玉,所以写的比较粗糙,望大家谅解。


    					
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,909
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298