1.访问火山引擎
2.注册后实名信息
3.产品—火山方舟—视觉大模型。找到对应模型,开通或者免费领取体验资源
4.点击查看详情—立即使用,按步骤获取产品密钥
5.打开unity 新建脚本(以下为主要方法,构建请求体后创建签名及认证)
IEnumerator GenerateImage()
{
long timestamp = GetTimestamp();
string method = "POST"; // 请求方法,固定为POST,可根据实际情况调整
string path = ""; // 这里假设你的请求路径是/generate,需根据实际API调整
string contentType = "application/json"; // 根据请求体内容类型设置
// 构建请求的JSON格式数据,按照文档里请求格式要求填充各参数
ImageGenerationRequest requestData = new ImageGenerationRequest
{
req_key = "high_aes_general_v20_L",
prompt = InputField.text,
model_version = "general_v2.0_L",
req_schedule_conf = "general_v20_9B_rephraser",
seed = -1,
scale = 3.5f,
ddim_steps = 16,
width = 512,
height = 512,
use_sr = true,
return_url = false,
logo_info = new LogoInfo
{
add_logo = false,
position = 0,
language = 0,
opacity = 0.3f,
logo_text_content = "这里是明水印内容"
}
};
string requestBody = JsonUtility.ToJson(requestData);
// 生成签名
string signature = GenerateSignature(timestamp, accessKey, secretKey, method, path, contentType, requestBody);
//string signature = "1caf8e82d88a9fc8f094f6d800616522e7c78f23f7e4ee568625b9b825e15eb0";
//Debug.Log(signature);
UnityWebRequest request = new UnityWebRequest(apiUrl, method);
byte[] bodyRaw = Encoding.UTF8.GetBytes(requestBody);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
// 设置Content-Type请求头
request.SetRequestHeader("Content-Type", contentType);
//设置Host请求头,从API请求地址中提取主机部分
//string host = new Uri(apiUrl).Host;
// request.SetRequestHeader("host", host);
// 设置X-Date请求头,使用获取到的时间戳转换后的格式
string xDate = GetFormattedXDate(timestamp);
request.SetRequestHeader("X-Date", xDate);
// 计算并设置X-Content-Sha256请求头,对请求体内容计算SHA256哈希值
string xContentSha256 = HashSHA256(bodyRaw);
request.SetRequestHeader("X-Content-Sha256", xContentSha256);
Debug.Log(xContentSha256);
// 构建并设置Authorization请求头,包含签名等认证信息
string credentialScope = $"{xDate.Substring(0, 8)}/{region}/{service}/request";
string authorizationHeader = $"HMAC-SHA256 Credential={accessKey}/{credentialScope}, SignedHeaders=host;x-content-sha256;x-date, Signature={signature}";
request.SetRequestHeader("Authorization", authorizationHeader);
yield return request.SendWebRequest();
// 按照新的方式判断请求结果,使用isDone和isNetworkError、isHttpError属性来替代原来的.result属性判断
if (request.isDone)
{
// 解析返回的JSON数据,获取图片相关内容并处理显示(按文档返回格式要求操作)
string responseJson = request.downloadHandler.text;
Debug.Log(responseJson);
ImageGenerationResponse response = JsonUtility.FromJson<ImageGenerationResponse>(responseJson);
if (response.data.binary_data_base64 != null && response.data.binary_data_base64.Count > 0)
{
byte[] imageBytes = System.Convert.FromBase64String(response.data.binary_data_base64[0]);
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(imageBytes);
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
resultImage.sprite = sprite;
}
else
{
Debug.LogError("没有获取到有效的图片数据");
}
}
else
{
Debug.LogError("请求出错: " + request.error);
}
}
6.效果