You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
399 lines
12 KiB
399 lines
12 KiB
using UnityEngine; |
|
using System; |
|
using AX.TrackRecord; |
|
using AX.MessageSystem; |
|
|
|
public class MouseFollowRotation : MonoBehaviour { |
|
public static MouseFollowRotation Instance; |
|
|
|
public Transform target; |
|
public float xSpeed=200, ySpeed=200, mSpeed=35; |
|
public float yMinLimit=-40, yMaxLimit=90; |
|
public float distance=100, minDistance=5, maxDistance=400; |
|
public float distanceSize = 1, mindistanceSize = 10, maxdistanceSize = 100; |
|
|
|
//bool needDamping = false; |
|
public bool needDamping =true; |
|
public float damping = 5.0f; |
|
|
|
public float x = 0.0f; |
|
public float y = 0.0f; |
|
public float Top= 2; |
|
public bool EnableMouseScrollwhell = true; |
|
protected const float PRECISION = 0.000001f; |
|
public static bool RMouseDownWithMove = false, RMouseDownHadMove=false; |
|
public static Vector3 cur=Vector3.zero,pre=new Vector3(1,0,0); |
|
private GameObject UIRoot; |
|
public GameObject TheChangeView; |
|
|
|
private CameraAttribute camAttribute; |
|
|
|
private float oldDis; |
|
private float nowDis; |
|
private int scales;//缩放次数 |
|
public CameraAttribute CameraAttri |
|
{ |
|
set |
|
{ |
|
camAttribute = new CameraAttribute(); |
|
camAttribute = value; |
|
enabled = false; |
|
target.localPosition = camAttribute.CameraTargetPos; |
|
x = camAttribute.x; |
|
y = camAttribute.y; |
|
|
|
oldDis = distance;//******* |
|
distance = camAttribute.distance; |
|
enabled = true; |
|
|
|
nowDis = distance;//**** |
|
ScaleObjInfo(oldDis, nowDis);//***** |
|
|
|
} |
|
get |
|
{ |
|
camAttribute = new CameraAttribute(); |
|
camAttribute.CameraTargetPos = target.localPosition; |
|
camAttribute.x = x; |
|
camAttribute.y = y; |
|
camAttribute.distance = distance; |
|
TrackRecordHelpClass.Set_Pos_Rot_Sca_Record(transform, ref camAttribute.trans); |
|
return camAttribute; |
|
} |
|
} |
|
|
|
public void SetTarget( GameObject go ) |
|
{ |
|
target = go.transform; |
|
} |
|
void Awake() |
|
{ |
|
if (Instance == null) |
|
{ |
|
Instance = this; |
|
} |
|
} |
|
void Start () { |
|
Vector3 angles = transform.eulerAngles; |
|
x = angles.y; |
|
y = angles.x; |
|
//t1 = DateTime.Now; |
|
//t2 = DateTime.Now; |
|
UIRoot = GameObject.Find("UI Root"); |
|
yMinLimit =- 10; |
|
MessageDispatcher.AddListener("TabChangeView", tabChangeView); |
|
|
|
oldDis = distance; |
|
|
|
MessageDispatcher.AddListener("CAMERA_FOCUS_CAROBJ", CameraFocusCarObj); |
|
} |
|
void OnDestroy() |
|
{ |
|
MessageDispatcher.RemoveListener("TabChangeView", tabChangeView); |
|
|
|
MessageDispatcher.RemoveListener("CAMERA_FOCUS_CAROBJ", CameraFocusCarObj); |
|
} |
|
|
|
/// <summary> |
|
/// 摄像机跟随聚焦车辆 |
|
/// </summary> |
|
/// <param name="obj"></param> |
|
private void CameraFocusCarObj(IMessage obj) |
|
{ |
|
oldDis = distance; |
|
|
|
var data = (CameraFocusData)obj.Data; |
|
|
|
target.position = new Vector3(data.obj.transform.position.x, target.position.y, data.obj.transform.position.z); |
|
|
|
distance = data.distance; |
|
x = data.x; |
|
y = data.y; |
|
|
|
nowDis = distance; |
|
ScaleObjInfo(oldDis, nowDis); |
|
} |
|
|
|
public int Index =1; |
|
public enum View : int |
|
{ |
|
ziyouview=1, |
|
dingshiview=2, |
|
threeview=3 |
|
} |
|
private View theview = View.ziyouview; |
|
public void tabChangeView(IMessage mes) |
|
{ |
|
Index++; |
|
|
|
if (LineamentEvent.gameobj != null && LineamentEvent.gameobj.name.Contains("xiaofangyuan")) |
|
{ |
|
oldDis = distance; |
|
Camera.main.orthographic = false; |
|
|
|
theview = View.threeview; |
|
if (!LoadManager.Instance.IsPlayBacking) |
|
{ |
|
target.parent = LineamentEvent.gameobj.transform; |
|
target.transform.localPosition = new Vector3(0, 4.36f, 0); |
|
} |
|
distance = 15.5f; |
|
//MessageDispatcher.SendMessage("Operatinghints", (object)"第三人称视角"); |
|
Index = 0; |
|
|
|
nowDis = distance; |
|
ScaleObjInfo(oldDis, nowDis); |
|
|
|
return; |
|
} |
|
if (Index == 1) |
|
{ |
|
theview = View.ziyouview; |
|
target.parent = null; |
|
//MessageDispatcher.SendMessage("Operatinghints", (object)"自由视角"); |
|
Camera.main.orthographic = false; |
|
x = 117f; |
|
y = 36f; |
|
} |
|
else if (Index == 2) |
|
{ |
|
theview = View.dingshiview; |
|
target.parent = null; |
|
//MessageDispatcher.SendMessage("Operatinghints", (object)"顶视图"); |
|
Camera.main.orthographic = true; |
|
Camera.main.orthographicSize = 150; |
|
|
|
} |
|
else if (Index == 3) |
|
{ |
|
if (LineamentEvent.gameobj != null && LineamentEvent.gameobj.name.Contains("xiaofangyuan")) |
|
{ |
|
oldDis = distance; |
|
|
|
theview = View.threeview; |
|
if (!LoadManager.Instance.IsPlayBacking) |
|
{ |
|
target.parent = LineamentEvent.gameobj.transform; |
|
target.transform.localPosition = new Vector3(0, 4.36f, 0); |
|
distance = 15.5f; |
|
} |
|
//MessageDispatcher.SendMessage("Operatinghints", (object)"第三人称视角"); |
|
Index = 0; |
|
Camera.main.orthographic = false; |
|
nowDis = distance; |
|
ScaleObjInfo(oldDis, nowDis); |
|
} |
|
else |
|
{ |
|
theview = View.ziyouview; |
|
target.parent = null; |
|
//MessageDispatcher.SendMessage("Operatinghints", (object)"自由视角"); |
|
Index = 1; |
|
x = 117f; |
|
y = 36f; |
|
Camera.main.orthographic = false; |
|
} |
|
|
|
} |
|
|
|
} |
|
//DateTime t1, t2; |
|
public int Pos=1; |
|
|
|
bool isUseFixTop = true; |
|
void LateUpdate () |
|
{ |
|
if (Input.GetMouseButtonDown(1)) |
|
{ |
|
pre = Input.mousePosition; |
|
} |
|
if (Input.GetMouseButtonUp(1)) |
|
{ |
|
cur = Input.mousePosition; |
|
if (Vector3.Distance(cur, pre) < 3f) |
|
{ |
|
MessageDispatcher.SendMessage("ResettingButton", (object)this.name); |
|
} |
|
} |
|
|
|
|
|
|
|
if (target) |
|
{ |
|
oldDis = distance; |
|
|
|
|
|
|
|
if (theview ==View.threeview || theview == View.ziyouview) |
|
{ |
|
|
|
if (Input.GetMouseButton(1)) |
|
{ |
|
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; |
|
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; |
|
|
|
y = ClampAngle(y, yMinLimit, yMaxLimit); |
|
|
|
} |
|
if (EnableMouseScrollwhell) |
|
{ |
|
distance -= Input.GetAxis("Mouse ScrollWheel") * mSpeed; |
|
} |
|
|
|
|
|
distance = Mathf.Clamp(distance, minDistance, maxDistance); |
|
//MessageDispatcher.SendMessage("ScaleObjInfo"); |
|
|
|
if (isUseFixTop) |
|
{ |
|
Top = fixTop(distance); |
|
} |
|
//if (Input.GetKeyDown(KeyCode.Space)) |
|
//{ |
|
// isUseFixTop = !isUseFixTop; |
|
//} |
|
Quaternion rotation = Quaternion.Euler(y, x, 0.0f); |
|
Vector3 disVector = new Vector3(0.0f, Top, -distance); |
|
Vector3 position = rotation * disVector * Pos + target.position; |
|
if (needDamping) |
|
{ |
|
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping); |
|
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping); |
|
} |
|
else |
|
{ |
|
transform.rotation = rotation; |
|
transform.position = position; |
|
} |
|
} |
|
else if (theview == View.dingshiview) |
|
{ |
|
x = 90; |
|
y = 90; |
|
|
|
if (Input.GetAxis("Mouse ScrollWheel") > 0) |
|
{ |
|
if (Camera.main.orthographicSize <= 8) |
|
{ |
|
distanceSize = 5; |
|
Camera.main.orthographicSize = 8; |
|
return; |
|
} |
|
Camera.main.orthographicSize -= 8; |
|
|
|
} |
|
else if (Input.GetAxis("Mouse ScrollWheel") < 0) |
|
{ |
|
if (Camera.main.orthographicSize >= 200) |
|
{ |
|
distanceSize = 100; |
|
Camera.main.orthographicSize = 200; |
|
return; |
|
} |
|
|
|
Camera.main.orthographicSize += 8; |
|
} |
|
if (isUseFixTop) |
|
{ |
|
Top = fixTop(distance); |
|
} |
|
|
|
Quaternion rotation = Quaternion.Euler(y, x, 0.0f); |
|
Vector3 disVector = new Vector3(0.0f, Top, -distance); |
|
Vector3 position = rotation * disVector * Pos + target.position; |
|
if (needDamping) |
|
{ |
|
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * damping); |
|
transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * damping); |
|
} |
|
else |
|
{ |
|
transform.rotation = rotation; |
|
transform.position = position; |
|
} |
|
} |
|
|
|
nowDis = distance; |
|
ScaleObjInfo(oldDis,nowDis); |
|
} |
|
} |
|
|
|
private void ScaleObjInfo(float old, float now) |
|
{ |
|
if (old != now) |
|
{ |
|
scales = (int)((now - old) / (mSpeed / 10)); |
|
MessageDispatcher.SendMessage("ScaleObjInfo",(object)scales); |
|
} |
|
} |
|
|
|
void forBF() { |
|
|
|
distance=1; |
|
} |
|
|
|
static float ClampAngle (float angle, float min, float max) |
|
{ |
|
if (angle < -360) |
|
angle += 360; |
|
if (angle > 360) |
|
angle -= 360; |
|
return Mathf.Clamp (angle, min, max); |
|
} |
|
|
|
float fixTop(float distance) |
|
{ |
|
float fixedTop = 0; |
|
if (distance >= 5 && distance <= 25) |
|
{ |
|
fixedTop = (distance - 25)*3 / 20; |
|
} |
|
else if(distance>=25){ |
|
fixedTop = (distance - 25) * 8 / 45; |
|
} |
|
return fixedTop; |
|
} |
|
void Update() |
|
{ |
|
//if (Input.GetKeyDown(KeyCode.Escape)) |
|
//{ |
|
// if (MySceneManager.GetActiveScene().name.Equals("1"))//处理城市综合体 |
|
// { |
|
// distance = 100; |
|
// x = 337.4315f; |
|
// y = 24.44321f; |
|
// Top = 13.33333f; |
|
|
|
// //camAttribute.CameraTargetPos = new Vector3(72.4f, 0, -57.71f); |
|
// Transform Trager = GameObject.Find("target").transform; |
|
// Trager.transform.localPosition = new Vector3(72.4f, 0, -57.71f); |
|
// TrackRecordHelpClass.RecordEseCameraPos(Trager.gameObject); |
|
// } |
|
// else if (MySceneManager.GetActiveScene().name.Equals("3")) |
|
// { |
|
|
|
// Transform Trager = GameObject.Find("target").transform; |
|
// distance = 100; |
|
// x = 246.0187f; |
|
// y = 24.41299f; |
|
// Top = 13.33333f; |
|
// Trager.transform.localPosition = new Vector3(-13.1f, 0, 19.5f); |
|
// TrackRecordHelpClass.RecordEseCameraPos(Trager.gameObject); |
|
// } |
|
// else if (MySceneManager.GetActiveScene().name.Equals("7")) |
|
// { |
|
// Transform Trager = GameObject.Find("target").transform; |
|
// distance = 201f; |
|
// x = 361f; |
|
// y = 6.199997f; |
|
// Top = 2f; |
|
// Trager.transform.localPosition = new Vector3(-268.6f, 0, 33f); |
|
// TrackRecordHelpClass.RecordEseCameraPos(Trager.gameObject); |
|
// } |
|
|
|
|
|
//} |
|
|
|
} |
|
|
|
} |