功能:
1、实现屏幕的移动、屏幕的放大和缩小;
2、射线检测物体显示其对象名称;
3、显示的UI需要显示在其对象的正上方;
4、在手机上显示;
一、cube的射线检测和显示UI代码:
public class cube_ray : MonoBehaviour {
public new Transform camera;
public Canvas canvas;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
canvas.gameObject.SetActive(false );
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit))
{
if (hit.transform.gameObject.tag.CompareTo("cube")==0)
{
canvas.gameObject.SetActive(true);
float height = hit.transform.gameObject.GetComponent<Renderer>().bounds.size.y/2;
//print(height);
float UI_height = HeightToNum.num;
//print(UI_height);
canvas.transform.position = new Vector3(hit.transform.position.x,height+UI_height,hit.transform.position.z);
canvas.transform.LookAt(camera);
canvas.transform.Find("Image/Text").GetComponent<Text>().text = "商品房001";
}
if (hit.transform.gameObject.tag.CompareTo("cube0") == 0)
{
canvas.gameObject.SetActive(true);
float height = hit.transform.gameObject.GetComponent<Renderer>().bounds.size.y / 2;
//print(height);
float UI_height = HeightToNum.num;
//print(UI_height);
canvas.transform.position = new Vector3(hit.transform.position.x, height + UI_height, hit.transform.position.z);
canvas.transform.LookAt(camera);
canvas.transform.Find("Image/Text").GetComponent<Text>().text = "商品房002";
}
if (hit.transform.gameObject.tag.CompareTo("cube1") == 0)
{
canvas.gameObject.SetActive(true);
float height = hit.transform.gameObject.GetComponent<Renderer>().bounds.size.y / 2;
//print(height);
float UI_height = HeightToNum.num;
//print(UI_height);
canvas.transform.position = new Vector3(hit.transform.position.x, height + UI_height, hit.transform.position.z);
canvas.transform.LookAt(camera);
canvas.transform.Find("Image/Text").GetComponent<Text>().text = "商品房003";
}
if (hit.transform.gameObject.tag.CompareTo("cube2") == 0)
{
canvas.gameObject.SetActive(true);
float height = hit.transform.gameObject.GetComponent<Renderer>().bounds.size.y / 2;
//print(height);
float UI_height = HeightToNum.num;
//print(UI_height);
canvas.transform.position = new Vector3(hit.transform.position.x, height + UI_height, hit.transform.position.z);
canvas.transform.LookAt(camera);
canvas.transform.Find("Image/Text").GetComponent<Text>().text = "商品房004";
}
if (hit.transform.gameObject.tag.CompareTo("hospital") == 0)
{
canvas.gameObject.SetActive(true);
float height = hit.transform.gameObject.GetComponent<Renderer>().bounds.size.y / 2;
//print(height);
float UI_height = HeightToNum.num;
//print(UI_height);
canvas.transform.position = new Vector3(hit.transform.position.x, height + UI_height, hit.transform.position.z);
canvas.transform.LookAt(camera);
canvas.transform.Find("Image/Text").GetComponent<Text>().text = "天仁医院";
}
if (hit.transform.gameObject.tag.CompareTo("supermark") == 0)
{
canvas.gameObject.SetActive(true);
float height = hit.transform.gameObject.GetComponent<Renderer>().bounds.size.y / 2;
//print(height);
float UI_height = HeightToNum.num;
//print(UI_height);
canvas.transform.position = new Vector3(hit.transform.position.x, height + UI_height, hit.transform.position.z);
canvas.transform.LookAt(camera);
canvas.transform.Find("Image/Text").GetComponent<Text>().text = "熊大大超市店";
}
if (hit.transform.gameObject.tag.CompareTo("bank") == 0)
{
canvas.gameObject.SetActive(true);
float height = hit.transform.gameObject.GetComponent<Renderer>().bounds.size.y / 2;
//print(height);
float UI_height = HeightToNum.num;
//print(UI_height);
canvas.transform.position = new Vector3(hit.transform.position.x, height + UI_height, hit.transform.position.z);
canvas.transform.LookAt(camera);
canvas.transform.Find("Image/Text").GetComponent<Text>().text = "全球贸易银行";
}
if (hit.transform.gameObject.tag.CompareTo("canting") == 0)
{
canvas.gameObject.SetActive(true);
float height = hit.transform.gameObject.GetComponent<Renderer>().bounds.size.y / 2;
//print(height);
float UI_height = HeightToNum.num;
//print(UI_height);
canvas.transform.position = new Vector3(hit.transform.position.x, height + UI_height, hit.transform.position.z);
canvas.transform.LookAt(camera);
canvas.transform.Find("Image/Text").GetComponent<Text>().text = "五星级餐厅";
}
if (hit.transform.gameObject.tag.CompareTo("yinger") == 0)
{
canvas.gameObject.SetActive(true);
float height = hit.transform.gameObject.GetComponent<Renderer>().bounds.size.y / 2;
//print(height);
float UI_height = HeightToNum.num;
//print(UI_height);
canvas.transform.position = new Vector3(hit.transform.position.x, height + UI_height, hit.transform.position.z);
canvas.transform.LookAt(camera);
canvas.transform.Find("Image/Text").GetComponent<Text>().text = "好贝贝婴儿房";
}
if (hit.transform.gameObject.tag.CompareTo("jiudian") == 0)
{
canvas.gameObject.SetActive(true);
float height = hit.transform.gameObject.GetComponent<Renderer>().bounds.size.y / 2;
//print(height);
float UI_height = HeightToNum.num;
//print(UI_height);
canvas.transform.position = new Vector3(hit.transform.position.x, height + UI_height, hit.transform.position.z);
canvas.transform.LookAt(camera);
canvas.transform.Find("Image/Text").GetComponent<Text>().text = "五星级酒店";
}
if (hit.transform.gameObject.tag.CompareTo("chufang") == 0)
{
canvas.gameObject.SetActive(true);
float height = hit.transform.gameObject.GetComponent<Renderer>().bounds.size.y / 2;
//print(height);
float UI_height = HeightToNum.num;
//print(UI_height);
canvas.transform.position = new Vector3(hit.transform.position.x, height + UI_height, hit.transform.position.z);
canvas.transform.LookAt(camera);
canvas.transform.Find("Image/Text").GetComponent<Text>().text = "好太太厨房店";
}
}
}
}
二、获取UI高度的一般距离:(显示UI的需要)
public class HeightToNum : MonoBehaviour {
public Canvas canvas;
public static float num;
// Use this for initialization
//public static UI_gethalf_height _instance;
//private void Awake()
//{
// _instance = this;
//}
void Start () {
num = canvas.GetComponent<RectTransform>().rect.height / 2;
}
// Update is called once per frame
void Update () {
}
}
三.相机的移动代码:(这是网上找的,我稍微改了一点点)
public class move : MonoBehaviour {
private int isforward;//标记摄像机的移动方向
//记录两个手指的旧位置
private Vector2 oposition1 = new Vector2();
private Vector2 oposition2 = new Vector2();
Vector2 m_screenPos = new Vector2(); //记录手指触碰的位置
//用于判断是否放大
bool isEnlarge(Vector2 oP1, Vector2 oP2, Vector2 nP1, Vector2 nP2)
{
//函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
float leng1 = Mathf.Sqrt((oP1.x - oP2.x) * (oP1.x - oP2.x) + (oP1.y - oP2.y) * (oP1.y - oP2.y));
float leng2 = Mathf.Sqrt((nP1.x - nP2.x) * (nP1.x - nP2.x) + (nP1.y - nP2.y) * (nP1.y - nP2.y));
if (leng1 < leng2)
{
//放大手势
return false;
}
else
{
//缩小手势
return true;
}
}
void Start()
{
Input.multiTouchEnabled = true;//开启多点触碰
}
void Update()
{
if (Input.touchCount <= 0)
return;
if (Input.touchCount == 1) //单点触碰移动摄像机
{
if (Input.touches[0].phase == TouchPhase.Began)
m_screenPos = Input.touches[0].position; //记录手指刚触碰的位置
if (Input.touches[0].phase == TouchPhase.Moved) //手指在屏幕上移动,移动摄像机
{
//transform.Translate(new Vector3(Input.touches[0].deltaPosition.x * Time.deltaTime*50, Input.touches[0].deltaPosition.y * Time.deltaTime*50, 0));
transform.Translate(new Vector3(Input.touches[0].deltaPosition.x * Time.deltaTime * 50, 0, 0));
}
}
else if (Input.touchCount > 1)//多点触碰
{
//记录两个手指的位置
Vector2 nposition1 = new Vector2();
Vector2 nposition2 = new Vector2();
//记录手指的每帧移动距离
Vector2 deltaDis1 = new Vector2();
Vector2 deltaDis2 = new Vector2();
for (int i = 0; i < 2; i++)
{
Touch touch = Input.touches[i];
if (touch.phase == TouchPhase.Ended)
break;
if (touch.phase == TouchPhase.Moved) //手指在移动
{
if (i == 0)
{
nposition1 = touch.position;
deltaDis1 = touch.deltaPosition;
}
else
{
nposition2 = touch.position;
deltaDis2 = touch.deltaPosition;
if (isEnlarge(oposition1, oposition2, nposition1, nposition2)) //判断手势伸缩从而进行摄像机前后移动参数缩放效果
isforward = 1;
else
isforward = -1;
}
//记录旧的触摸位置
oposition1 = nposition1;
oposition2 = nposition2;
}
//移动摄像机
Camera.main.transform.Translate(isforward * Vector3.forward * Time.deltaTime*50 * (Mathf.Abs(deltaDis2.x + deltaDis1.x) + Mathf.Abs(deltaDis1.y + deltaDis2.y)));
}
}
}
}