OnTriggerEnter2D(Collider2D)和Component.tag

博客提到在使用Unity时,有大量函数未曾使用过,对这些函数感到陌生,反映了在Unity开发中对部分功能的不熟悉情况。

这里写图片描述
Unity,没有使用过的函数太多,陌生。

这里写图片描述

using UnityEngine; using UnityEngine.SceneManagement; using System.Collections; [RequireComponent(typeof(Collider2D))] public class EnhancedSceneTrigger : MonoBehaviour { [Header("场景设置")] public string targetSceneName; public int targetSceneIndex = -1; [Header("触发设置")] public string playerTag = "Player"; public float triggerCooldown = 2f; // 防止重复触发的冷却时间 public bool requireInteraction = false; // 是否需要交互才触发 public KeyCode triggerKey = KeyCode.E; // 桌面端触发按键 [Header("移动设备设置")] public bool showMobileButton = true; // 是否显示移动设备按钮 public Rect mobileButtonRect = new Rect(20, Screen.height - 100, 80, 80); // 移动按钮位置大小 public string buttonText = "进入"; // 移动按钮文本 [Header("反馈效果")] public GameObject triggerEffect; // 触发时的粒子效果等 public AudioClip triggerSound; // 触发时的音效 private bool isInTriggerZone = false; private bool isOnCooldown = false; private AudioSource audioSource; private bool isMobile; private void Awake() { // 自动配置碰撞体 Collider2D col = GetComponent<Collider2D>(); if (col != null && !col.isTrigger) { col.isTrigger = true; } // 添加音频源组件 audioSource = GetComponent<AudioSource>(); if (audioSource == null && triggerSound != null) { audioSource = gameObject.AddComponent<AudioSource>(); } // 判断是否为移动设备 isMobile = Application.isMobilePlatform; } private void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag(playerTag) && !isOnCooldown) { isInTriggerZone = true; if (isMobile) { Debug.Log("进入触发区域,点击按钮切换场景"); } else { Debug.Log("进入触发区域,按" + triggerKey + "键切换场景"); } } } private void OnTriggerExit2D(Collider2D other) { if (other.CompareTag(playerTag)) { isInTriggerZone = false; } } private void Update() { // 检查触发条件 if (isInTriggerZone && !isOnCooldown) { // 桌面设备按键检测 if (!isMobile && (!requireInteraction || Input.GetKeyDown(triggerKey))) { TriggerSceneChange(); } // 移动设备不需要交互时自动触发 else if (isMobile && !requireInteraction) { TriggerSceneChange(); } } } // 移动设备按钮绘制检测 private void OnGUI() { if (isMobile && showMobileButton && isInTriggerZone && !isOnCooldown && requireInteraction) { // 调整按钮位置(确保在屏幕内) mobileButtonRect.y = Screen.height - 100; // 绘制按钮 if (GUI.Button(mobileButtonRect, buttonText)) { TriggerSceneChange(); } } } private void TriggerSceneChange() { // 播放效果音效 if (triggerEffect != null) { Instantiate(triggerEffect, transform.position, Quaternion.identity); } if (triggerSound != null && audioSource != null) { audioSource.PlayOneShot(triggerSound); } // 开始冷却 StartCoroutine(TriggerCooldown()); // 切换场景 if (!string.IsNullOrEmpty(targetSceneName)) { SceneManager.LoadScene(targetSceneName); } else if (targetSceneIndex != -1) { SceneManager.LoadScene(targetSceneIndex); } else { Debug.LogError("请设置目标场景名称或索引!"); } } // 冷却协程,防止短时间内重复触发 private IEnumerator TriggerCooldown() { isOnCooldown = true; yield return new WaitForSeconds(triggerCooldown); isOnCooldown = false; } // 在编辑器中绘制Gizmos,显示触发区域 private void OnDrawGizmos() { Gizmos.color = Color.green; Gizmos.DrawWireCube(transform.position, GetComponent<Collider2D>().bounds.size); } } 手机端的触发切换场景不起作用
09-24
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值