ugui物体随着摄像机跟随3d场景物体显示
2017-10-19 16:34
465 查看
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InScreenFollowScene : MonoBehaviour
{
private Transform mFollowTrans;
private RectTransform mRectTrans;
private void Awake ()
{
mRectTrans = this.GetComponent<RectTransform> ();
}
public void InitFollow (Transform followTrans)
{
mFollowTrans = followTrans;
CalculateFollowPosAndScale ();
}
// Update is called once per frame
void Update ()
{
if (null == mFollowTrans)
return;
UpdateTitlePosition ();
}
private Vector2 mAnchorMin;
private Vector2 mAnchorMax;
private Vector3 mLocalPos;
const float OriginSize = 1.6f;
const float FarSize = 0.5f;
const float InvisibaleSize = 0.8f;
private void UpdateTitlePosition ()
{
// if (Time.frameCount % 5 != 0)
// return;
if (null == mFollowTrans)
return;
CalculateFollowPosAndScale ();
}
private void CalculateFollowPosAndScale ()
{
Vector3 screenPos = InfraredFightController.Instance.mainCamera.WorldToScreenPoint (mFollowTrans.position);
mAnchorMin.x = screenPos.x / InfraredFightController.Instance.mainCamera.pixelWidth;
mAnchorMin.y = screenPos.y / InfraredFightController.Instance.mainCamera.pixelHeight;
float scaleZ = screenPos.z;
Camera cam = InfraredFightController.Instance.mainCamera;
float sz = OriginSize - (OriginSize - FarSize) * (scaleZ - cam.nearClipPlane) / (cam.farClipPlane - cam.nearClipPlane);
if (scaleZ < cam.nearClipPlane || sz < InvisibaleSize) {
if (gameObject.activeSelf)
gameObject.SetActive (false);
} else {
if (!gameObject.activeSelf)
gameObject.SetActive (true);
transform.localScale = Vector3.one * sz * 5f;//CalculateScaleFactorByCameraFov ();
}
mAnchorMax.x = mAnchorMin.x;
mAnchorMax.y = mAnchorMin.y;
mRectTrans.anchorMin = mAnchorMin;
mRectTrans.anchorMax = mAnchorMax;
mRectTrans.anchoredPosition = Vector2.zero;
mLocalPos = transform.localPosition;
mLocalPos.z = 0;
transform.localPosition = mLocalPos;
}
private const float MaxZoomScaleFactor = 0.6f;
private const float MinZoomScaleFactor = 1f;
//private float CalculateScaleFactorByCameraFov ()
//{
// float max = UIManager.Instance.MoblieTouchScript.CamZoomMax;
// float f = Mathf.Clamp01 ((max - UIManager.Instance.MoblieTouchScript.CamZoom) / (max - UIManager.Instance.MoblieTouchScript.CamZoomMin));
// return Mathf.Lerp (MaxZoomScaleFactor, MinZoomScaleFactor, f);
//}
}
using System.Collections.Generic;
using UnityEngine;
public class InScreenFollowScene : MonoBehaviour
{
private Transform mFollowTrans;
private RectTransform mRectTrans;
private void Awake ()
{
mRectTrans = this.GetComponent<RectTransform> ();
}
public void InitFollow (Transform followTrans)
{
mFollowTrans = followTrans;
CalculateFollowPosAndScale ();
}
// Update is called once per frame
void Update ()
{
if (null == mFollowTrans)
return;
UpdateTitlePosition ();
}
private Vector2 mAnchorMin;
private Vector2 mAnchorMax;
private Vector3 mLocalPos;
const float OriginSize = 1.6f;
const float FarSize = 0.5f;
const float InvisibaleSize = 0.8f;
private void UpdateTitlePosition ()
{
// if (Time.frameCount % 5 != 0)
// return;
if (null == mFollowTrans)
return;
CalculateFollowPosAndScale ();
}
private void CalculateFollowPosAndScale ()
{
Vector3 screenPos = InfraredFightController.Instance.mainCamera.WorldToScreenPoint (mFollowTrans.position);
mAnchorMin.x = screenPos.x / InfraredFightController.Instance.mainCamera.pixelWidth;
mAnchorMin.y = screenPos.y / InfraredFightController.Instance.mainCamera.pixelHeight;
float scaleZ = screenPos.z;
Camera cam = InfraredFightController.Instance.mainCamera;
float sz = OriginSize - (OriginSize - FarSize) * (scaleZ - cam.nearClipPlane) / (cam.farClipPlane - cam.nearClipPlane);
if (scaleZ < cam.nearClipPlane || sz < InvisibaleSize) {
if (gameObject.activeSelf)
gameObject.SetActive (false);
} else {
if (!gameObject.activeSelf)
gameObject.SetActive (true);
transform.localScale = Vector3.one * sz * 5f;//CalculateScaleFactorByCameraFov ();
}
mAnchorMax.x = mAnchorMin.x;
mAnchorMax.y = mAnchorMin.y;
mRectTrans.anchorMin = mAnchorMin;
mRectTrans.anchorMax = mAnchorMax;
mRectTrans.anchoredPosition = Vector2.zero;
mLocalPos = transform.localPosition;
mLocalPos.z = 0;
transform.localPosition = mLocalPos;
}
private const float MaxZoomScaleFactor = 0.6f;
private const float MinZoomScaleFactor = 1f;
//private float CalculateScaleFactorByCameraFov ()
//{
// float max = UIManager.Instance.MoblieTouchScript.CamZoomMax;
// float f = Mathf.Clamp01 ((max - UIManager.Instance.MoblieTouchScript.CamZoom) / (max - UIManager.Instance.MoblieTouchScript.CamZoomMin));
// return Mathf.Lerp (MaxZoomScaleFactor, MinZoomScaleFactor, f);
//}
}
相关文章推荐
- UGUI 将2DUI和场景中的3d物体关联,跟随物体移动
- Unity3D游戏开发之在3D场景中选择物体并显示轮廓效果强化版
- UGUI 制作血条,3d物体跟随移动
- [Unity3D]Unity3D游戏开发之在3D场景中选择物体并显示轮廓效果强化版
- UGUI--------UI以及3D物体显示在鼠标点击位置
- Unity3D游戏开发之在3D场景中选择物体并显示轮廓效果
- [Unity3D]Unity3D游戏开发之在3D场景中选择物体并显示轮廓效果
- UE4创建3D场景中的UI方法及在UI上面显示三维物体
- Unity3D中2DUI跟随场景中3D物体
- [Unity3D]Unity3D游戏开发之在3D场景中选择物体并显示轮廓效果
- 3D场景中的物体跟随鼠标移动
- Unity3D游戏开发之在3D场景中选择物体并显示轮廓效果强化版
- Unity 3d 实现物体跟随摄像机视野运动
- [Unity 3D教程]教你如何在3D场景中选择物体并显示轮廓效果
- 如何在3D场景中选择物体并显示轮廓效果
- 实现3d物体与Ugui的混合显示
- 在3D场景中显示汉字
- IOS 特定于设备的开发:获取和使用设备姿势(通过手机方向控制3d物体显示)
- Python批量重命名Maya场景中的3D物体
- Unity中拖动的简单实现(UGUI以及3D物体)