diff --git a/Assets/Scripts/CameraController.cs b/Assets/Scripts/CameraController.cs index 85b5f1f..31b48df 100644 --- a/Assets/Scripts/CameraController.cs +++ b/Assets/Scripts/CameraController.cs @@ -1,11 +1,13 @@ using System; using System.Collections; +using System.Collections.Generic; using TMPro; using Unity.VisualScripting; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.InputSystem; using UnityEngine.InputSystem.EnhancedTouch; +using UnityEngine.UI; using WeChatWASM; using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch; @@ -42,6 +44,8 @@ public class CameraController : MonoBehaviour //[SerializeField] private float zoomInertiaDecay = 0.93f; // 惯性衰减系数 [SerializeField] private float zoomMin = 2f; [SerializeField] private float zoomMax = 30f; + // Canvas的UI射线检测组件(自动获取) + public GraphicRaycaster uiRaycaster; //[SerializeField] private float zoomSnapThreshold = 0.01f; //[SerializeField] private float zoomSmoothTime = 0.15f; // 新增平滑时间 @@ -53,7 +57,7 @@ public class CameraController : MonoBehaviour // 运行时状态 private Camera _cam; - private Vector2? _posInput = null; + private Vector2 _posInput = Vector2.zero; private Vector2? _moveInput = null; private Vector2 _zoomInput = Vector2.zero; private float _rotateInput; @@ -67,9 +71,12 @@ public class CameraController : MonoBehaviour private float _rotationVelocity; private float _beforeTouchDistance = -1; private Vector2? _beforeMoveInput = null; - + private int _backgroundLayer = 0; + private int _uiLayer = 0; private void Awake() { + _backgroundLayer = LayerMask.NameToLayer("Background"); + //_uiLayer = LayerMask.NameToLayer("UI"); _cam = GetComponent(); } @@ -129,7 +136,7 @@ public class CameraController : MonoBehaviour } private void MouseAction_canceled(InputAction.CallbackContext ctx) { - _posInput = null; + //_posInput = null; _beforeMoveInput = null; _moveInput = null; @@ -210,20 +217,36 @@ public class CameraController : MonoBehaviour //Vector2 mousePos = moveAction.action.ReadValue(); // 创建射线(ScreenPointToRay支持Vector2,内部会自动处理z轴为0) - if (EventSystem.current.IsPointerOverGameObject()) - { - return true; - } - return false; - //Ray ray = _cam.ScreenPointToRay(_moveInput); - //// 射线检测(检测UI层) - //if (Physics.Raycast(ray, out RaycastHit hit, 100f, LayerMask.GetMask("UI"))) - //{ - // //enabled = false; - // //StartCoroutine(EnableAfterDelay(0.1f)); - // return false; - //} + if (uiRaycaster != null) + { + // 第一步:先检测UI + PointerEventData pointerData = new PointerEventData(EventSystem.current); + pointerData.position = _posInput; + List uiResults = new List(); + uiRaycaster.Raycast(pointerData, uiResults); + + if (uiResults.Count > 0) + { + return true; // 有UI则不检测3D + } + } + + + Ray ray = _cam.ScreenPointToRay(_posInput); + if (Physics.Raycast(ray, out RaycastHit hit, 100f)) + { + if (hit.collider.gameObject.layer == _backgroundLayer) + { + //enabled = false; + //StartCoroutine(EnableAfterDelay(0.1f)); + return false; + } + } + + return false; + + // 射线检测(检测U //return true; } @@ -293,7 +316,7 @@ public class CameraController : MonoBehaviour //_zoomInput.Set(0, 0); //_posInput.Value.Set(0, 0); } - float touchDistance = Vector2.Distance(_posInput.Value, _zoomInput); + float touchDistance = Vector2.Distance(_posInput, _zoomInput); if (_beforeTouchDistance == -1) { _beforeTouchDistance = touchDistance; @@ -351,9 +374,9 @@ public class CameraController : MonoBehaviour // 回调方法实现 private void OnMovePerformed(InputAction.CallbackContext ctx) { + _posInput = ctx.ReadValue(); if (_isPressDown) { - _posInput = ctx.ReadValue(); if (_beforeMoveInput == null) { _beforeMoveInput = _posInput; @@ -367,7 +390,7 @@ public class CameraController : MonoBehaviour private void OnMoveCanceled(InputAction.CallbackContext ctx) { - _posInput = null; + //_posInput = null; _moveInput = null; _beforeMoveInput = null; diff --git a/Assets/Scripts/Enemy.cs b/Assets/Scripts/Enemy.cs index b823f90..0a8d0d9 100644 --- a/Assets/Scripts/Enemy.cs +++ b/Assets/Scripts/Enemy.cs @@ -66,6 +66,20 @@ public class Enemy : MonoBehaviour } + public void ResetMaterial() + { + var color = _renderer.material.color; + color.a = 1; + _renderer.material.color = color; + } + + public void SetAlphaMaterial() + { + var color = _renderer.material.color; + color.a = 0; + _renderer.material.color = color; + } + // Update is called once per frame void Update() { @@ -134,7 +148,10 @@ public class Enemy : MonoBehaviour public void Respawn() { - AudioTool.AudioSource.PlayOneShot(boomClip); + if (boomClip != null) + { + AudioTool.AudioSource.PlayOneShot(boomClip); + } gameObject.transform.DOKill(true); Global.LockedEnemies.Remove(GetHashCode()); Global.DespawnEnemy(this); diff --git a/Assets/Scripts/EnemyManager.cs b/Assets/Scripts/EnemyManager.cs index 89773ae..d37d571 100644 --- a/Assets/Scripts/EnemyManager.cs +++ b/Assets/Scripts/EnemyManager.cs @@ -9,7 +9,7 @@ using UnityEngine.InputSystem; public class EnemyManager : MonoBehaviour { public float attackRadius = 5f; - public Transform startLoc; + public Transform[] startLoc = new Transform[] { }; public Transform[] pathesLoc = new Transform[] { }; [Tooltip("每只怪出现的间隔")] public float enemyAppearDuration = 0.1f; @@ -42,9 +42,6 @@ public class EnemyManager : MonoBehaviour /// public float enemyMoveSpeed = 1f; - [Tooltip("爆炸音效")] - public AudioClip boomClip; - [Tooltip("每波怪的个数")] public float initEachEnemyCount = 50; @@ -86,7 +83,7 @@ public class EnemyManager : MonoBehaviour for (int i = 0; i < initEachEnemyCount; i++) { //var pos = new Vector3(Random.Range(0f, Screen.width), Random.Range(Screen.height + 125f, Screen.height + 353f), _cam.nearClipPlane - _cam.transform.position.z); - SpawnEnemy(Mathf.FloorToInt(Random.value * enemyPrefabList.Length)); + var enemyComp = SpawnEnemy(Mathf.FloorToInt(Random.value * enemyPrefabList.Length)); //float duration = Mathf.Max(0.2f, Random.value / _currAttackIndex * 10); //if (i % 5 == 0) //{ @@ -94,6 +91,11 @@ public class EnemyManager : MonoBehaviour //} //else //{ + if (enemyComp != null) + { + yield return null; // Adjust the interval as needed + enemyComp.ResetMaterial(); + } yield return new WaitForSeconds(enemyAppearDuration); // Adjust the interval as needed //} } @@ -102,56 +104,57 @@ public class EnemyManager : MonoBehaviour } } - public void SpawnEnemy(int enemyIndex) + public Enemy SpawnEnemy(int enemyIndex) { if (enemyIndex < 0 || enemyIndex >= enemyPrefabList.Length) { Debug.LogError("Invalid enemy index: " + enemyIndex); - return; + return null; } //var fromPos = _cam.ScreenToWorldPoint(spawnPosition); //spawnPosition.y = 350;//移动到指定位置 //var toPos = _cam.ScreenToWorldPoint(spawnPosition); var pos = Vector3.zero; var enemy = Instantiate(enemyPrefabList[enemyIndex], Vector3.zero, Quaternion.identity); - if (startLoc == null && pathesLoc.Length > 0) + //if (startLoc.Length == 0 && pathesLoc.Length > 0) + //{ + var startPos = startLoc.Last().position; + var bounds = enemy.GetComponentInChildren().bounds; + //float angle = Mathf.PI * 2 * Random.value; // 修正角度计算逻辑 + float x = Random.Range(0, Screen.width); + float y = Screen.height + 100; + pos = new Vector3(x, y, _cam.transform.position.z); + bool isLeft = pos.x < Screen.width * 0.5f; + pos = _cam.ScreenToWorldPoint(pos); + pos.y = pathesLoc.Last().position.y; + pos.z = startPos.z; + //往中聚拢怪物尺寸的长度 + if (isLeft) { - var bounds = enemy.GetComponentInChildren().bounds; - //float angle = Mathf.PI * 2 * Random.value; // 修正角度计算逻辑 - float x = Random.Range(0, Screen.width); - float y = Screen.height + 100; - pos = new Vector3(x, y, 0); - bool isLeft = pos.x < Screen.width * 0.5f; - pos = _cam.ScreenToWorldPoint(pos); - pos.y = pathesLoc.Last().position.y; - //往中聚拢怪物尺寸的长度 - if (isLeft) - { - pos.x += bounds.size.magnitude * 0.5f; - } - else - { - pos.x -= bounds.size.magnitude * 0.5f; - } - - //pathesLoc.Last().position + new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * attackRadius; + pos.x += bounds.size.magnitude * 0.5f; } else { - pos = startLoc.position; + pos.x -= bounds.size.magnitude * 0.5f; } + //pathesLoc.Last().position + new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * attackRadius; + //} + //else + //{ + // pos = startLoc[Random.Range(0, startLoc.Length)].position; + //} + enemy.position = pos; var enemyComp = enemy.GetComponent(); enemy.eulerAngles = new Vector3(enemy.eulerAngles.x + enemyComp.addEulerX, enemy.eulerAngles.y + 180, enemy.eulerAngles.z); //Debug.Log(enemy.eulerAngles); - enemyComp.boomClip = boomClip; Global.Enemies.Add(enemyComp); // 初始化时将物体移动到最近的NavMesh点 NavMeshHit initHit; if (NavMesh.SamplePosition(enemy.position, out initHit, 8.0f, NavMesh.AllAreas)) { - transform.position = initHit.position; + enemy.position = initHit.position; } else { @@ -162,6 +165,8 @@ public class EnemyManager : MonoBehaviour destPos = new Vector3(enemy.position.x, destPos.y, destPos.z); ToDestination(enemyComp, destPos); + enemyComp.SetAlphaMaterial(); + return enemyComp; //enemy.DOMove(toPos, enemyMoveSpeed) // .SetEase(Ease.Linear) // 强制匀速,无加速减速 // .SetSpeedBased(); // 标记为可回收,完成后回收到池; // 关键:将第二个参数视为“速度”而非“时间” diff --git a/Assets/Scripts/Shot.cs b/Assets/Scripts/Shot.cs index 713e1b6..0a0c2e2 100644 --- a/Assets/Scripts/Shot.cs +++ b/Assets/Scripts/Shot.cs @@ -26,7 +26,10 @@ public class Shot : MonoBehaviour public Transform turret; public Transform gun; + [Tooltip("发射音效")] public AudioClip shotAudioClip; + [Tooltip("击中音效")] + public AudioClip hitAudioClip; [Tooltip("发射时间间隔")] public float shotDuration = 0.2f; [Tooltip("连发发射时间间隔")] @@ -263,6 +266,12 @@ public class Shot : MonoBehaviour if (bulletObj != null) { + // 10. 播放音效 + if (!isNextLineShot && shotAudioClip != null) + { + AudioTool.AudioSource.PlayOneShot(shotAudioClip); + } + //// 1. 强制重置所有状态 //PoolTool.ResetTransform(bulletObj); //PoolTool.ResetTransform(muzzleObj); @@ -383,6 +392,12 @@ public class Shot : MonoBehaviour //{ //Debug.Log(currentShotCount); //} + + if (hitAudioClip != null) + { + AudioTool.AudioSource.PlayOneShot(hitAudioClip); + } + if (currentShotCount <= continueShotCount) { currentShotCount++; @@ -455,12 +470,6 @@ public class Shot : MonoBehaviour // RecycleToPool(muzzleObj, muzzlePool, activeMuzzles); // } //}); - - // 10. 播放音效 - if (shotAudioClip != null) - { - AudioTool.AudioSource.PlayOneShot(shotAudioClip); - } } } diff --git a/Assets/Scripts/Tools/AudioTool.cs b/Assets/Scripts/Tools/AudioTool.cs index 5bb819e..b6834f4 100644 --- a/Assets/Scripts/Tools/AudioTool.cs +++ b/Assets/Scripts/Tools/AudioTool.cs @@ -17,7 +17,7 @@ public class AudioTool { var audioGameObject = new GameObject("GlobalAudioSource"); _audioSource = audioGameObject.AddComponent(); - _audioSource.volume = 0.5f; + _audioSource.volume = 0.33f; Object.DontDestroyOnLoad(audioGameObject); } return _audioSource; diff --git a/Assets/TowerDefence/Materials/Mat_Cube.mat b/Assets/TowerDefence/Materials/Mat_Cube.mat index e883029..027a639 100644 Binary files a/Assets/TowerDefence/Materials/Mat_Cube.mat and b/Assets/TowerDefence/Materials/Mat_Cube.mat differ diff --git a/Assets/TowerDefence/Prefabs/EnemyCube.prefab b/Assets/TowerDefence/Prefabs/EnemyCube.prefab index ed40ee9..8f02bab 100644 Binary files a/Assets/TowerDefence/Prefabs/EnemyCube.prefab and b/Assets/TowerDefence/Prefabs/EnemyCube.prefab differ diff --git a/Assets/TowerDefence/Prefabs/Qiu.prefab b/Assets/TowerDefence/Prefabs/Qiu.prefab index 6afc400..8d51d8c 100644 Binary files a/Assets/TowerDefence/Prefabs/Qiu.prefab and b/Assets/TowerDefence/Prefabs/Qiu.prefab differ diff --git a/Assets/TowerDefence/Scenes/Tower.unity b/Assets/TowerDefence/Scenes/Tower.unity index b9ecf1c..ad864f8 100644 Binary files a/Assets/TowerDefence/Scenes/Tower.unity and b/Assets/TowerDefence/Scenes/Tower.unity differ diff --git a/Assets/TowerDefence/Sound.meta b/Assets/TowerDefence/Sound.meta new file mode 100644 index 0000000..267e6d8 --- /dev/null +++ b/Assets/TowerDefence/Sound.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: XXwe4yOsW3/dMMBzC1jPC8h83tfB/g93jIgfIpggr04FAFQLuAPZ0Zs= +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TowerDefence/Sound/Audio.meta b/Assets/TowerDefence/Sound/Audio.meta new file mode 100644 index 0000000..bcf39dd --- /dev/null +++ b/Assets/TowerDefence/Sound/Audio.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: Ci4btnj/UH4DjBxU5p6J+WPBPbfw7HxW6JXXY/7jYllQhbqnUu3Njos= +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TowerDefence/Sound/Audio/baoshixiaochu.mp3 b/Assets/TowerDefence/Sound/Audio/baoshixiaochu.mp3 new file mode 100644 index 0000000..201aa5e Binary files /dev/null and b/Assets/TowerDefence/Sound/Audio/baoshixiaochu.mp3 differ diff --git a/Assets/TowerDefence/Sound/Audio/baoshixiaochu.mp3.meta b/Assets/TowerDefence/Sound/Audio/baoshixiaochu.mp3.meta new file mode 100644 index 0000000..3f44b91 --- /dev/null +++ b/Assets/TowerDefence/Sound/Audio/baoshixiaochu.mp3.meta @@ -0,0 +1,23 @@ +fileFormatVersion: 2 +guid: WXhK4y2lVi5Q0Jdo2o+m5LxOyeR6OArhUoJErsePDCNFMEng5r0pKRM= +AudioImporter: + externalObjects: {} + serializedVersion: 7 + defaultSettings: + serializedVersion: 2 + loadType: 0 + sampleRateSetting: 0 + sampleRateOverride: 44100 + compressionFormat: 1 + quality: 1 + conversionMode: 0 + preloadAudioData: 0 + platformSettingOverrides: {} + forceToMono: 0 + normalize: 1 + loadInBackground: 0 + ambisonic: 0 + 3D: 1 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TowerDefence/Sound/Audio/ding.mp3 b/Assets/TowerDefence/Sound/Audio/ding.mp3 new file mode 100644 index 0000000..26d2566 Binary files /dev/null and b/Assets/TowerDefence/Sound/Audio/ding.mp3 differ diff --git a/Assets/TowerDefence/Sound/Audio/ding.mp3.meta b/Assets/TowerDefence/Sound/Audio/ding.mp3.meta new file mode 100644 index 0000000..9e29578 --- /dev/null +++ b/Assets/TowerDefence/Sound/Audio/ding.mp3.meta @@ -0,0 +1,23 @@ +fileFormatVersion: 2 +guid: XnwdvCquUnivlXs1Aa5DYb7VjTp1uY5iAqEzAYDUnByAilWQt8+6k8w= +AudioImporter: + externalObjects: {} + serializedVersion: 7 + defaultSettings: + serializedVersion: 2 + loadType: 0 + sampleRateSetting: 0 + sampleRateOverride: 44100 + compressionFormat: 1 + quality: 1 + conversionMode: 0 + preloadAudioData: 0 + platformSettingOverrides: {} + forceToMono: 0 + normalize: 1 + loadInBackground: 0 + ambisonic: 0 + 3D: 1 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TowerDefence/Sound/Music.meta b/Assets/TowerDefence/Sound/Music.meta new file mode 100644 index 0000000..d31f302 --- /dev/null +++ b/Assets/TowerDefence/Sound/Music.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: By9J436tWy7lbhGkTTIXOHBXpALErfz7Pn4aW0E71gUoXPLKJD0ji/k= +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TowerDefence/Sound/Music/bg-music1.mp3 b/Assets/TowerDefence/Sound/Music/bg-music1.mp3 new file mode 100644 index 0000000..c26f288 Binary files /dev/null and b/Assets/TowerDefence/Sound/Music/bg-music1.mp3 differ diff --git a/Assets/TowerDefence/Sound/Music/bg-music1.mp3.meta b/Assets/TowerDefence/Sound/Music/bg-music1.mp3.meta new file mode 100644 index 0000000..056f98c --- /dev/null +++ b/Assets/TowerDefence/Sound/Music/bg-music1.mp3.meta @@ -0,0 +1,23 @@ +fileFormatVersion: 2 +guid: CywfsiKrVy0pKKtI4ZwffkOrnjFk5/3iMWZO/ydd05n7d8B0u6Q8wzI= +AudioImporter: + externalObjects: {} + serializedVersion: 7 + defaultSettings: + serializedVersion: 2 + loadType: 0 + sampleRateSetting: 0 + sampleRateOverride: 44100 + compressionFormat: 1 + quality: 1 + conversionMode: 0 + preloadAudioData: 0 + platformSettingOverrides: {} + forceToMono: 0 + normalize: 1 + loadInBackground: 0 + ambisonic: 0 + 3D: 1 + userData: + assetBundleName: + assetBundleVariant: