UNITY笔记

常用API

将鼠标坐标系转换为Camera的坐标系

Camera.main.ScreenToViewportPoint

禁用碰撞体、按钮禁用

GetComponent<BoxCollider> ().enabled = false;

小Tip

数据存储的方法

PlayerPrefs.SetInt();//保存整型数据
PlayerPrefs.SetFloat();//保存浮点型数据
PlayerPrefs.SetString();//保存字符串型数据
PlayerPrefs.GetInt();//读取整型数据
PlayerPrefs.GetFloat();//读取浮点型数据
PlayerPrefs.GetString();//读取字符串型数据

PlayerPrefs.SetInt("KEY","VALUE");

读取普通文本资源:TextAsset

TextAsset text=(TextAsset)Resources.Load("unity3d");
Debug.Log(text.text);

在Project窗口的根目录创建Resources文件夹
然后把名字为unity3d.txt的文件夹的文件放在Resources文件夹下就可以读取到。

解析Json

需要第三方插件
下载网址:https://sourceforge.net/projects/litjson/
仅需要LitJson.dll文件放在项目的Plugins文件夹下
创建Json文件

using LitJson;		//引用LitJson

void Json(){
	//创建数据
	JsonDate json = new JsonDate();
	json["name"] = "皮皮虾";
	json["sex"] = "男";
	
	//转换JsonDate为Json文本
	string strJson = json.ToJson();
	
	//打印json
	Debug.Log(strJson);
}

Json文件添加一行

using LitJson;		//引用LitJson

void Json(){
	//创建数据
	JsonDate json = new JsonDate();
	json["name"] = "皮皮虾";
	json["sex"] = "男";

	JsonDate json1 = new JsonDate();
	//添加一行
	json1.Add(json);
	
	//转换JsonDate为Json文本
	string strJson = json1.ToJson();
	
	//打印json
	Debug.Log(strJson);
}

上面两个都得到

[
    {
      "Name": "皮皮虾",
      "sex": "男"
    }
]

读取Json文件

using LitJson;		//引用LitJson

void Json(){
	//创建数据
	JsonDate json = new JsonDate();
	json["name"] = "皮皮虾";
	json["sex"] = "男";

	JsonDate json1 = new JsonDate();
	//添加一行
	json1.Add(json);
	
	//转换JsonDate为Json文本
	string strJson = json1.ToJson();

	//转换Json文本为JsonDate
	JsonData jsonData2 = JsonMapper.ToObject(strJson);
	//打印jsonData2中name
	Debug.Log(jsonData2["name"]);
}

转义编码
LitJson中编码为unicode,不支持中文,所以我们要把他转换为GBK
仅需要在转换JsonDate为Json文本后使用即可

Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
var ss = reg.Replace(要转换的字符串, delegate (Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值