Unity3d的几个比较琐碎的知识点备忘,
- GameObject的显示、隐藏和查找
- C#的int,string和float的转换
- C#中数组、ArrayList和List三者的区别
- C#中Dictionary的说明
以我工程的结构为例
一、GameObject的显示、隐藏和查找
<p style="text-align:center;">
<a target="_blank" href="http://www.hudongdong.com/content/uploadfile/201606/d09d1466691714.png" id="ematt:838"><img src="http://www.hudongdong.com/content/uploadfile/201606/d09d1466691714.png" title="点击查看原图" alt="屏幕快照 2016-06-23 16.45.38.png" border="0" width="178" height="237" /></a>
</p>
<p>
在GameObject中,没有像cocos2d中的setVisible方法,所以只能选择激活不激活的方法来隐藏和显示该对象。所以想要隐藏pros这个节点可以采用这个函数
</p>
<p>
GameObject.Find("pros").SetActive(false);
</p>
<p>
查找GameObject的方法很多,
</p>
<p style="text-align:center;">
<a target="_blank" href="http://www.hudongdong.com/content/uploadfile/201606/5ee41466692029.png" id="ematt:839"><img src="http://www.hudongdong.com/content/uploadfile/201606/5ee41466692029.png" title="点击查看原图" alt="屏幕快照 2016-06-23 22.26.20.png" border="0" width="500" height="240" /></a>
</p>
<p style="text-align:left;">
但是如果这个对象的执行setActive(false)之后,直接通过GameObject.Find是找不到该对象的,执行之后会返回为空,所以可以通过活跃的父节点查找不活跃的节点。
</p>
<p style="text-align:left;">
GameObject.Find("UI Root").transform.Find("pros").gameObject
</p>
<p>
这个执行可以得到对象,当然如果不是动态的话,也可以把该节点创建成public的对象,保存在一个变量中。
</p>
<p>
比如这个代码控制
</p>
<p>
public GameObject obj;
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.A)){
if(obj.activeSelf){
obj.SetActive(false);
}
else
obj.SetActive(true);
}
}
</p>
<p>
然后用public手动管理这个对象也可以
</p>
<p style="text-align:center;">
<a target="_blank" href="http://www.hudongdong.com/content/uploadfile/201606/b2711466692528.png" id="ematt:840"><img src="http://www.hudongdong.com/content/uploadfile/201606/b2711466692528.png" title="点击查看原图" alt="屏幕快照 2016-06-23 22.34.36.png" border="0" width="530" height="148" /></a>
</p>
<h2>
二、C#的int,string和float的转换
</h2>
<p>
首先先说这个Convert类,
<p>
Convert 将一个基本数据类型转化为另一基本数据类型。
</p>
<p>
支持的转化类型:受支持的基类型是 Boolean、Char、SByte、Byte、Int16、Int32、Int64、UInt16、UInt32、UInt64、Single、Double、Decimal、DateTime 和 String。
</p>
<p>
这个类具体用法可以百度。
</p>
<h3>
1、int转string
</h3>
<p>
int i =10;
string k =System.Convert.ToString(i);
string s= i.ToString();
string m =""+i;
</p>
<h3>
2、string转int
</h3>
<p>
string str = "123";
int i = int.Parse(str);
int k16 = System.Convert.ToInt16(str);
int k32 = System.Convert.ToInt32(str);
</p>
<h3>
3、float转int,转型取舍不同,注释中有对比
</h3>
<p>
float f = 0.6f; //0.4, 0.5, 0.6
int i = Mathf.CeilToInt(f); //1, 1, 1,
int k = Mathf.FloorToInt(f); //0, 0, 0
int k16 = System.Convert.ToInt16(f); //0, 0, 1
int k32 = System.Convert.ToInt32(f); //0, 0, 1
</p>
</p>
<h2>
三、C#中数组、ArrayList和List三者的区别
</h2>
<p>
Array和ArrayList存储的对象是否固定,ArrayList和List<T>考虑类型是否安全,
</p>
<p>
因为array的话,就是直接指定了类型,只能存放该类型,而ArrayList则可以存放任意object类型。
</p>
<p>
这个文章说的很好:<a href="http://blog.csdn.net/zhang_xinxiu/article/details/8657431" target="_blank">http://blog.csdn.net/zhang_xinxiu/article/details/8657431</a>
</p>
<p>
就是array是这样的:
</p>
<p>
string[] s=new string[2];
//赋值
s[0]="a";
s[1]="b";
//修改
s[1]="a1";
</p>
<p>
这样的话就只能存放string,在声明数组的时候必须指定数组的长度,数组的长度过长,会造成内存浪费,过段会造成数据溢出的错误。如果在声明数组时我们不清楚数组的长度,就会变得很麻烦。针对数组的这些缺点,C#中最先提供了ArrayList对象来克服这些缺点
</p>
<p>
ArrayList是这样的
</p>
<p>
ArrayList list1 = new ArrayList();
//新增数据
list1.Add("cde");
list1.Add(5678);
//修改数据
list[2] = 34;
//移除数据
list.RemoveAt(0);
//插入数据
list.Insert(0, "qwe");
</p>
<p>
这样就ArrayList可以存放任意类型,我们不仅插入了字符串cde,而且插入了数字5678。这样在ArrayList中插入不同类型的数据是允许的。因为ArrayList会把所有插入其中的数据当作为object类型来处理,在我们使用ArrayList处理数据时,很可能会报类型不匹配的错误,也就是ArrayList不是类型安全的。
</p>
<p>
List就是这样的
</p>
<p>
List<string> list = new List<string>();
//新增数据
list.Add(“abc”);
//修改数据
list[0] = “def”;
//移除数据
list.RemoveAt(0);
</p>
<p>
这样就指定了类型,就不怕类型错误了
</p>
<h2>
四、C#中Dictionary的说明
</h2>
<p>
Dictionary的用法网上很多,这里需要提的就是使用的时候要导入头文件
</p>
<p>
Using System.Collection.Generic;
</p>
<p>
不然会调不出来。
</p>
</p>
版权属于:胡东东博客
本文链接:http://blog.hudongdong.com/u3d-C/309.html
自2017年12月26日起,『转载以及大段采集进行后续编辑』须注明本文标题和链接!否则禁止所有转载和采集行为!
☟☟如文章有用,可点击一次下方广告支持一下☟☟