Unity3d 框选
2015-10-09 23:14
615 查看
有时候,我们在游戏中会遇到需要框选对象的情况,比如红警。
在unity3d中可以用GL非常简单的实现。
1.先用GL画出线框,并求出Bounds
2.遍历对象,将对象的世界坐标转换到屏幕坐标,并测试该坐标是否在Bounds中。
using UnityEngine;
using System.Collections;
public class BoxSelector : MonoBehaviour {
}
在unity3d中可以用GL非常简单的实现。
1.先用GL画出线框,并求出Bounds
2.遍历对象,将对象的世界坐标转换到屏幕坐标,并测试该坐标是否在Bounds中。
using UnityEngine;
using System.Collections;
public class BoxSelector : MonoBehaviour {
Material _mat ; bool _select = false; Vector3 _p1; Vector3 _p2; GameObject _cubeObj; Color _srcColor ; Renderer _render ; // Use this for initialization void Start () { if( !_mat ) { _mat = new Material("Shader \"Lines/Colored Blended\" {" + "SubShader { Pass { " + " Blend SrcAlpha OneMinusSrcAlpha " + " ZWrite Off Cull Off Fog { Mode Off } " + " BindChannels {" + " Bind \"vertex\", vertex Bind \"color\", color }" + "} } }"); } _cubeObj = GameObject.Find("Cube"); _render = _cubeObj.GetComponent<Renderer>(); _srcColor = _render.material.color; Bounds bounds = _cubeObj.GetComponent<BoxCollider>().bounds; } void OnDestroy() { Destroy(_mat); } // Update is called once per frame void Update () { if(Input.GetMouseButtonDown(0)) { _select = true; _p1 = Input.mousePosition; } if(Input.GetMouseButton(0)) { _p2 = Input.mousePosition; } if(Input.GetMouseButtonUp(0)) { _select = false; Vector3 center = (_p1+_p2)*0.5f; Vector3 size = new Vector3(Mathf.Abs(_p1.x-_p2.x),Mathf.Abs(_p1.y-_p2.y),0); Bounds bounds = new Bounds(center,size); BoxTest(bounds); } if(Input.GetMouseButtonDown(0)) { Vector3 ray = new Vector3(Input.mousePosition.x,Input.mousePosition.y,(_cubeObj.transform.position - Camera.main.transform.position).z); Vector3 pos = Camera.main.ScreenToWorldPoint(ray); RaycastHit hit; if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit)) { if(hit.collider.gameObject != _cubeObj) { _render.material.color = _srcColor; } } else { _render.material.color = _srcColor; } } } void DrawBox(Vector3 p1,Vector3 p2) { _mat.SetPass(0); GL.PushMatrix(); GL.LoadPixelMatrix(); GL.Begin(GL.LINES); GL.Vertex(p1); GL.Vertex3(p1.x,p2.y,0); GL.Vertex3(p1.x,p2.y,0); GL.Vertex(p2); GL.Vertex(p2); GL.Vertex3(p2.x,p1.y,0); GL.Vertex3(p2.x,p1.y,0); GL.Vertex(p1); GL.End(); GL.PopMatrix(); } void OnPostRender() { if(_select) { DrawBox(_p1,_p2); } } void BoxTest(Bounds bounds) { if(_cubeObj) { Vector3 pos = Camera.main.WorldToScreenPoint(_cubeObj.transform.position); pos.z = bounds.center.z; if(bounds.Contains(pos)) { _render.material.color = Color.blue; } } } void OnDrawGizmos() { if(_cubeObj != null) { Bounds bounds = _cubeObj.GetComponent<BoxCollider>().bounds; Gizmos.color = new Color(1,0,0,0.5f); Gizmos.DrawCube(_cubeObj.transform.position,bounds.size); } }
}
相关文章推荐
- Unity3D开发者快速上手Unreal Engine 4指南
- Unity3D占用内存太大的解决方法
- Cardboard Unity SDK Reference
- Unity3d渲染层级关系小结
- unity与java相互调用
- unity csharp socket 异步通信 客户端
- Unity3D学习笔记——NGUI之UIToggle
- Unity 使用MiniJson解析本地数据
- Unity 4.x Asset Bundle 重名
- Unity添加自定义拓展方法ExternMethod
- 在Unity3D中加载外部图片的两种方法
- Unity 使用JsonFx解析本地json数据
- Unity协同程序(Coroutine)的解析
- Unity3d优化之从Draw Calls到GC
- Unity人工智能学习—确定性AI算法之追踪算法五
- untiy3d base64 To texture2d(unity3dbase64字符串数据转换图片)
- Unity人工智能学习—确定性AI算法之追踪算法四
- unity导出安卓APK后图片错乱的解决办法--getResources().getIdentifier()的使用
- TSSAO Temporal Screen-Space Ambient Occlusion (Unity3d 5 示例实现)
- TSSAO Temporal Screen-Space Ambient Occlusion (Unity3d 5 示例实现)