大家好,我创建了一个笔记系统,用户按“E”键,笔记将在玩家屏幕上出现。基本上,当按下“E”键时,笔记纸 Default NotePaper 将消失,NoteCanvas 将显示在摄像机级别,但再次按“E”键时,没有恢复,如NoteCanvas在屏幕上显示。 我尝试了Claude Gemini AI,他们也不能解决这个问题。 由于参考,我附上了我的代码:
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using SojaExiles; // Drawer命名空间
public class NoteSystem : MonoBehaviour
{
[Header("玩家设置")]
public Transform player;
public float interactDistance = 3f;
[Header("UI元件")]
public TMP_Text hintText; // "按E读"
public GameObject notePanel; // 显示笔记文本的面板
public TMP_Text noteTextUI; // 内部面板的TMP文本
public GameObject notePaper; // 3D纸张对象
[Header("笔记内容")]
[TextArea]
public string noteContent = "求f(x) = x²的导数";
[Header("Drawer参考")]
public Drawer_Pull_Z drawerPull; // Drawer脚本引用
private bool isNear = false;
private bool isReading = false;
private float keyPressCooldown = 0f;
private float cooldownDuration = 0.3f; // 防止快速触发
void Start()
{
// 初始化状态
if (hintText != null) hintText.gameObject.SetActive(false);
if (notePanel != null) notePanel.SetActive(false);
if (notePaper != null) notePaper.SetActive(true);
// 设置面板颜色
if (notePanel != null)
{
Image panelImage = notePanel.GetComponent<Image>();
if (panelImage != null)
panelImage.color = new Color32(255, 255, 204, 255);
}
if (noteTextUI != null) noteTextUI.color = Color.black;
}
void Update()
{
if (player == null || drawerPull == null) return;
// 更新冷却时间
if (keyPressCooldown > 0)
keyPressCooldown -= Time.deltaTime;
// 距离检查
float distance = Vector3.Distance(transform.position, player.position);
isNear = distance < interactDistance;
// 如果抽屉关闭,强制隐藏
if (!drawerPull.open)
{
isReading = false; // 强制状态重置
if (notePanel != null) notePanel.SetActive(false);
if (notePaper != null) notePaper.SetActive(true);
if (hintText != null) hintText.gameObject.SetActive(false);
return;
}
// 根据距离和阅读状态显示/隐藏提示
if (hintText != null)
hintText.gameObject.SetActive(isNear && !isReading);
// 处理按“E”键来切换笔记(带有冷却)
if (isNear && Input.GetKeyDown(KeyCode.E) && keyPressCooldown <= 0)
{
keyPressCooldown = cooldownDuration; // 重置冷却
isReading = !isReading; // 切换而不是始终设置为True
Debug.Log("E按下!isReading 现在是:" + isReading);
// 应用状态
if (notePanel != null)
{
notePanel.SetActive(isReading);
Debug.Log("notePanel.SetActive(" + isReading + ") ");
}
if (notePaper != null)
{
notePaper.SetActive(!isReading);
Debug.Log("notePaper.SetActive(" + (!isReading) + ") ");
}
// 更新文本如果显示
if (isReading && noteTextUI != null)
noteTextUI.text = noteContent;
}
}
}
请参见:https://preview.redd.it/sarzuqkokqsg1.png
请参见:https://preview.redd.it/34i6t9tpkqsg1.png
评论 (0)