using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class CheckKuang : MonoBehaviour { private Text carTypeTxt; private Text carSpeedTxt; public Car _car; private float _speed; private bool openCheck = false; private Ray _ray; public Camera _camera; private Vector2 scale; public float disValue; public void Init(int carType, float speed) { scale = GameManagerForZhuanWan.Instance.kuangScale; this.GetComponent().sizeDelta = new Vector2(Const.Kuang_Wight * scale.x, Const.Kuang_Height * scale.y); carTypeTxt = transform.Find("CarTypeTxt").GetComponent(); carSpeedTxt = transform.Find("CarSpeedTxt").GetComponent(); switch (carType) { case 1: carTypeTxt.text = "轿车"; break; case 2: carTypeTxt.text = "SUV"; break; case 3: carTypeTxt.text = "货车"; break; } _speed = speed; carSpeedTxt.text = _speed.ToString(); openCheck = true; _ray = new Ray(); } private void Update() { if (openCheck) { if (_car.gameObject.activeSelf == false) { this.GetComponent().enabled = false; for (int i = 0; i < transform.childCount; i++) { transform.GetChild(i).gameObject.SetActive(false); } } else { this.GetComponent().enabled = true; for (int i = 0; i < transform.childCount; i++) { transform.GetChild(i).gameObject.SetActive(true); } } //int random = Random.Range(0, 1); //if (random == 0) //{ // _speed +=0.1f; //} //else //{ // _speed -= 0.1f; //} carSpeedTxt.text = _car.overSpeed.ToString(); if (GameManagerForZhuanWan.Instance.OpenKuangAutoScale) { AutoScale(); } else { this.GetComponent().sizeDelta = new Vector2(Const.Kuang_Wight * scale.x, Const.Kuang_Height * scale.y); } } } private void AutoScale() { RaycastHit hit; float distance = Vector3.Distance(_car.transform.position, _camera.transform.position); float maxDistance = Const.Pass1CameraFarClip; distance = Mathf.Clamp(distance, 15, maxDistance); float rate = Mathf.SmoothStep(0.25f, 1f, 1 - (distance / maxDistance)); disValue = rate; float h; float w; h = rate * 75; w = rate * 80; this.GetComponent().sizeDelta = new Vector2(w, h); if (rate >= 0.9f) { //if (distance <= 15) //{ // this.GetComponent().enabled = false; // carSpeedTxt.enabled = false; // carTypeTxt.enabled = false; //} //else //{ // this.GetComponent().sizeDelta = new Vector2(w *distance* GameManagerForZhuanWan.Instance.kuangDistanceValue, h * distance * GameManagerForZhuanWan.Instance.kuangDistanceValue); //} if (distance <= 35&&distance>15) this.GetComponent().sizeDelta = new Vector2(w + rate * GameManagerForZhuanWan.Instance.kuangDistanceValue, h + (rate+0.2f) * GameManagerForZhuanWan.Instance.kuangDistanceValue); else if (distance <= 15) { this.GetComponent().enabled = false; carSpeedTxt.enabled = false; carTypeTxt.enabled = false; } else { if (GameManagerForZhuanWan.Instance.openZheDang) { _ray.direction = (_car.transform.position + new Vector3(0, 2, 0) - _camera.transform.position).normalized; _ray.origin = _camera.transform.position; if (Physics.Raycast(_ray, out hit, (_car.transform.position + new Vector3(0, 2, 0) - _camera.transform.position).magnitude)) ; { //Debug.DrawLine(_camera.transform.position + new Vector3(0, 2, 0), _car.transform.position); if (hit.collider != null) { if (hit.collider.tag == "ZhangAi") { this.GetComponent().enabled = false; carSpeedTxt.enabled = false; carTypeTxt.enabled = false; return; } } } this.GetComponent().enabled = true; carSpeedTxt.enabled = true; carTypeTxt.enabled = true; } } } } }