Unity3D(C#)游戏通过API提交Qualtrics问卷及文件可行性咨询
Answer: Submitting Qualtrics Survey Responses + TXT Files from Unity/C#
Great question! Let’s break this down clearly for your Unity game project—yes, you absolutely can submit survey responses and TXT files to Qualtrics via API calls directly from your game, and Qualtrics fully supports this workflow. Here’s how to pull it off:
Key Background: Qualtrics API Capabilities
First, to confirm:
- Qualtrics provides a Response Import API (and related endpoints) that lets you programmatically submit survey responses.
- You can attach files (including TXT) to these responses either by:
- Using a file upload question in your Qualtrics survey, and mapping your game-generated TXT to that question field.
- Attaching the file as an additional asset to the survey response via the API’s multipart form support.
Step-by-Step Implementation in Unity/C#
1. Prepare Your Qualtrics Setup
- Generate an API Token: Go to your Qualtrics Account Settings > API > Create Token. Ensure the token has permissions for
Survey Responses(create/write access) andFiles(if needed). - Grab Your Survey ID: Find this in your survey’s Settings > General > Survey ID.
- Optional: Configure a File Upload Question: If you want the TXT file to be linked directly to a survey question, add a File Upload question to your Qualtrics survey and note its Question ID (you’ll need this when mapping responses).
2. Unity Code Implementation
Use Unity’s UnityWebRequest (the recommended tool for HTTP calls in Unity) to build the submission request. Here’s a reusable script example:
using UnityEngine; using UnityEngine.Networking; using System.Collections; using System.Text; public class QualtricsSubmitter : MonoBehaviour { // Replace these with your Qualtrics details private const string QUALTRICS_API_TOKEN = "YOUR_API_TOKEN_HERE"; private const string SURVEY_ID = "YOUR_SURVEY_ID_HERE"; private const string QUALTRICS_DATACENTER = "YOUR_DATACENTER_HERE"; // e.g., "eu" or "ca1" public void SubmitSurveyWithFile(string[] surveyAnswers, string txtFilePath) { StartCoroutine(PostSurveyResponse(surveyAnswers, txtFilePath)); } private IEnumerator PostSurveyResponse(string[] surveyAnswers, string txtFilePath) { // Build the Qualtrics API endpoint URL string apiUrl = $"https://{QUALTRICS_DATACENTER}.qualtrics.com/API/v3/surveys/{SURVEY_ID}/responses"; // Create multipart form data (supports both survey answers and file upload) var form = new WWWForm(); // Add survey answers (map each answer to its corresponding Question ID) // Example: If your first question has ID "QID1", add form.AddField("QID1", surveyAnswers[0]); form.AddField("QID1", surveyAnswers[0]); form.AddField("QID2", surveyAnswers[1]); // Add more fields for all your survey questions // Attach the game-generated TXT file if (System.IO.File.Exists(txtFilePath)) { byte[] fileBytes = System.IO.File.ReadAllBytes(txtFilePath); form.AddBinaryData("fileUploadField", fileBytes, "game_session_data.txt", "text/plain"); // Note: If using a Qualtrics File Upload question, replace "fileUploadField" with the question's ID (e.g., "QID3") } // Create the web request using (UnityWebRequest request = UnityWebRequest.Post(apiUrl, form)) { // Add API authorization header request.SetRequestHeader("X-API-TOKEN", QUALTRICS_API_TOKEN); // Send the request and wait for response yield return request.SendWebRequest(); // Handle response if (request.result == UnityWebRequest.Result.Success) { Debug.Log("Survey response + file submitted successfully!"); Debug.Log("Qualtrics Response: " + request.downloadHandler.text); } else { Debug.LogError("Submission failed: " + request.error); Debug.LogError("Qualtrics Error Details: " + request.downloadHandler.text); } } } }
3. Important Notes & Troubleshooting
- Datacenter URL: Make sure your API URL uses the correct datacenter for your Qualtrics account (e.g.,
https://ca1.qualtrics.comfor Canada,https://eu.qualtrics.comfor Europe). - File Size Limits: Qualtrics restricts file uploads to a maximum size (usually 10MB per file)—ensure your game-generated TXT stays under this limit.
- Editor CORS Issues: If testing in the Unity Editor, you might hit CORS errors. To fix this, add Qualtrics’ domain to Unity’s
Project Settings > Player > Publishing Settings > Cross-Origin Resource Sharing (CORS)allowed origins. - Error Handling: Add retry logic for network failures, and notify players if submission fails (e.g., "Failed to save your data—please check your internet connection").
内容的提问来源于stack exchange,提问作者Vipin Verma
相关产品推荐
相关产品推荐

