chat.html 5.76 KB
<!DOCTYPE html>
<html lang="en-us">

<head>
  <meta charset="utf-8">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>chat</title>
  <style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body {
      min-height: 100vh;
      background: #f0f2f5;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 20px;
      gap: 20px;
    }

    .container {
      display: flex;
      align-items: center;
      gap: 20px;
    }

    .input-container {
      width: 100%;
      max-width: 320px;
      background: white;
      padding: 15px;
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      display: flex;
      align-items: center;
      gap: 8px;
    }

    input {
      flex: 1;
      padding: 8px 12px;
      border: 1px solid #ddd;
      border-radius: 6px;
      font-size: 14px;
    }

    button {
      white-space: nowrap;
      padding: 8px 12px;
      background: #1890ff;
      color: white;
      border: none;
      border-radius: 6px;
      cursor: pointer;
      transition: all 0.3s;
    }

    button:hover {
      background: #40a9ff;
    }

    #unity-canvas {
      width: 320px;
      height: 480px;
      border-radius: 12px;
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    }

    /* 新增聊天窗口样式 */
    .chat-container {
      width: 550px;
      height: 550px;
      background: white;
      border-radius: 12px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      display: flex;
      flex-direction: column;
      gap: 10px;
      padding: 15px;
    }

    .chat-messages {
      flex: 1;
      height: 300px;
      overflow-y: auto;
      display: flex;
      flex-direction: column;
      gap: 12px;
      padding: 0 5px;
    }

    .chat-message {
      max-width: 70%;
      padding: 8px 12px;
      border-radius: 8px;
      font-size: 14px;
      line-height: 1.4;
    }

    .chat-message.user {
      background: #1890ff;
      color: white;
      align-self: flex-end;
    }

    .chat-message.bot {
      background: #f0f2f5;
      color: #333;
      align-self: flex-start;
    }

    .chat-input {
      display: flex;
      gap: 8px;
    }
  </style>
</head>

<body>
  <div class="container">
    <div>
      <canvas id="unity-canvas" tabindex="-1" style="background: #231F20"></canvas>
      <div class="input-container">
        <input type="text" placeholder="输入要朗读的文字">
        <button>朗读</button>
      </div>
    </div>
    <!-- 新增聊天窗口结构 -->
    <div class="chat-container">
      <div class="chat-messages" id="chat-messages"></div>
      <div class="chat-input">
        <input type="text" id="chat-input" placeholder="输入聊天消息">
        <button id="send-btn">发送</button>
      </div>
    </div>
  </div>
  <script src="assets/unity/build_web.loader.js"></script>
  <script>
    function initInput(canvas, config, progress) {
      let inputField = document.querySelector('input');
      let speakButton = document.querySelector('button');
      speakButton.addEventListener('click', () => {
        const text = inputField.value;
        if (text) {
          console.log('text', text);
          window.unityInstance.SendMessage('UnityReceiverFromJs', 'ReceiveText', text);
          inputField.value = '';
        }
      });
    }

    // 聊天功能初始化函数
    function initChat() {
      const chatInput = document.querySelector('#chat-input');
      const sendBtn = document.querySelector('#send-btn');
      const chatMessages = document.querySelector('#chat-messages');
      const fixedReplies = [
        "你好呀!有什么可以帮你的吗?",
        "今天天气不错呢~",
        "我收到你的消息啦!",
        "稍后我会仔细看看的~",
        "最近我学习了很多新知识,比如如何识别不同的天气模式,你想了解一下吗?",
        "用户的需求是我们最关注的,如果你有任何建议或问题,都可以随时告诉我,我会尽力帮你解决的~",
        "今天看到一个有趣的故事,说的是一只小松鼠如何在冬天储存足够的坚果,你想听我详细讲讲吗?"
      ];

      function addMessage(text, isUser) {
        const messageDiv = document.createElement('div');
        messageDiv.className = `chat-message ${isUser ? 'user' : 'bot'}`;
        messageDiv.textContent = text;
        chatMessages.appendChild(messageDiv);
        chatMessages.scrollTop = chatMessages.scrollHeight;
      }

      sendBtn.addEventListener('click', () => {
        const text = chatInput.value.trim();
        if (text) {
          addMessage(text, true);
          chatInput.value = '';
          // 模拟1秒后回复固定消息(新增语音调用)
          setTimeout(() => {
            const randomIndex = Math.floor(Math.random() * fixedReplies.length);
            const botText = fixedReplies[randomIndex];
            addMessage(botText, false);
            // 调用Unity语音朗读能力
            window.unityInstance.SendMessage('UnityReceiverFromJs', 'ReceiveText', botText);
          }, 1000);
        }
      });

      // 支持回车键发送
      chatInput.addEventListener('keypress', (e) => {
        if (e.key === 'Enter') sendBtn.click();
      });
    }

    async function initializeUnity() {
      window.unityInstance = await createUnityInstance(document.querySelector("#unity-canvas"), {
        dataUrl: "assets/unity/build_web.data",
        frameworkUrl: "assets/unity/build_web.framework.js",
        codeUrl: "assets/unity/build_web.wasm",
        streamingAssetsUrl: "StreamingAssets",
        companyName: "DefaultCompany",
        productName: "DianWang_HDRP",
        productVersion: "0.1",
      });
    }
    initializeUnity();
    initInput();
    initChat();
  </script>
</body>

</html>