转发,请保持地址:http://blog.csdn.net/stalendp/article/details/17114135
相关文章:【Unity】技巧集合2
这篇文章将收集unity的相关技巧,会不断地更新内容。
1. 保存运行中的状态
unity在运行状态时是不能够保存的。但在运行时编辑的时候,有时会发现比较好的效果想保存。这时可以在 “Hierarchy”中复制相关对象树,暂停游戏后替换原来的,就可以了。(其实这个拷贝过程是序列化过程,这种方法是序列化到内存中;另外一种方法就是序列化到磁盘上,即把内容拖动到文件夹中变成prefab,效果也是一样的)
2. Layer的用法
LayerMask.NameToLayer("Ground"); // 通过名字获取layer
3D Raycast
RaycastHit hit;
if(Physics.Raycast(cam3d.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity, (1<<LayerMask.NameToLayer("Ground")))) {
...
}
2D Raycast
Collider2D h = Physics2D.OverlapPoint(Input.mousePosition, (1<<LayerMask.NameToLayer("xxx")));
if(h) {
...
}
uGUI中的做法:
void Update () {
if (Input.GetMouseButtonDown (0)) {
if (EventSystem.current.IsPointerOverGameObject ()) {
PointerEventData pe = new PointerEventData(EventSystem.current);
pe.position = Input.mousePosition;
List<RaycastResult> hits = new List<RaycastResult>();
EventSystem.current.RaycastAll( pe, hits );
foreach(RaycastResult h in hits) {
if(h.gameObject.layer == tangramLayer) {
Debug.Log("======" + Time.time + ", obj: " + h.gameObject.name);
}
}
}
..................
3. 物理摄像头取色(WebCamTexture)
Texture2D exactCamData() {
// get the sample pixels
Texture2D snap = new Texture2D((int)detectSize.x, (int)detectSize.y);
snap.SetPixels(webcamTexture.GetPixels((int)detectStart.x, (int)detectStart.y, (int)detectSize.x, (int)detectSize.y));
snap.Apply();
return snap;
}
保存截图:
System.IO.File.WriteAllBytes(Application.dataPath + "/test.png", exactCamData().EncodeToPNG());
4. 操作componenent
添加:
CircleCollider2D cld = (CircleCollider2D)colorYuan[i].AddComponent(typeof(CircleCollider2D));
cld.radius = 1;
删除:
Destroy(transform.gameObject.GetComponent<SpriteRenderer>());
5. 动画相关
状态Init到状态fsShake的的条件为:参数shake==true;代码中的写法:
触发fsShake:
void Awake() {
anims = new Animator[(int)FColorType.ColorNum];
}
....
if(needShake) {
curAnim.SetTrigger("shake");
}
关闭fsShake
void Update() {
....
if(curAnim) {
AnimatorStateInfo stateInfo = curAnim.GetCurrentAnimatorStateInfo(0);
if(stateInfo.nameHash == Animator.StringToHash("Base Layer.fsShake")) {
curAnim.SetBool("shake", false);
curAnim = null;
print ("======>>>>> stop shake!!!!");
}
}
....
}
关于Animator.StringToHash函数的说明:
animator的setBool,setTrigger等函数都有两种参数形式,一个接收string,一个接收hash;其实Animator内部是使用hash来记录相应信息的,所以接收string类型的函数内部会帮你调用StringToHash这个函数;所以如果要经常调用setTrigger等,请把参数名称hash化保持以便反复使用,从而提高性能。
在状态机animator中获取animation state 和animation clip的方法(参考http://answers.unity3d.com/questions/692593/get-animation-clip-length-using-animator.html):
public static AnimationClip getAnimationClip(Animator anim, string clipName) {
UnityEditorInternal.State state = getAnimationState(anim, clipName);
return state!=null ? state.GetMotion() as AnimationClip : null;
}
public static UnityEditorInternal.State getAnimationState(Animator anim, string clipName) {
UnityEditorInternal.State state = null;
if(anim != null) {
UnityEditorInternal.AnimatorController ac = anim.runtimeAnimatorController as UnityEditorInternal.AnimatorController;
UnityEditorInternal.StateMachine sm = ac.GetLayer(0).stateMachine;
for(int i = 0; i < sm.stateCount; i++) {
UnityEditorInternal.State _state = sm.GetState(i);
if(state.uniqueName.EndsWith("." + clipName)) {
state = _state;
}
}
}
return state;
}
6. scene的切换
同步方式:
Application.LoadLevel(currentName);
异步方式:
Application.LoadLevelAsync("ARScene");
7. 加载资源
Resources.Load<Texture>(string.Format("{0}{1:D2}", mPrefix, 5));
8. Tag VS. Layer
-> Tag用来查询对象
-> Layer用来确定哪些物体可以被raycast,还有用在camera render中
9. 旋转
transform.eulerAngles 可以访问 rotate的 xyz
transform.RotateAround(pivotTransVector, Vector3.up, -0.5f * (tmp-preX) * speed);
10. 保存数据
PlayerPrefs.Set