完整代码下载地址:https://download.csdn.net/download/weixin_43703390/89179785 打不开就是还在审核中…
账号注册创建模块:
1.打开百度智能云官网:[https://cloud.baidu.com/?from=console],在【产品】中找到【语言与知识】下的【AI作画】
2.打开界面后点击【立即使用】,然后创建应用,得到API Key和Secret Key
3.点击左侧【API在线调试】选择对应的版本
6.如果调试成功的画,就可在unity项目中开始使用了
代码模块
代码步骤1:首先根据API Key和Secret Key获取token
private string API_KEY = "111111"; // 替换为你的API Key
private string SECRET_KEY = "2222222"; // 替换为你的Secret Key
public string m_Token = string.Empty;
IEnumerator GetToken()
{
//获取token的api地址
string _token_url = string.Format("https://aip.baidubce.com/oauth/2.0/token" + "?client_id={0}&client_secret={1}&grant_type=client_credentials"
, API_KEY, SECRET_KEY);
using (UnityWebRequest request = new UnityWebRequest(_token_url, "GET"))
{
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
yield return request.SendWebRequest();
if (request.isDone)
{
string _msg = request.downloadHandler.text;
TokenInfo _textback = JsonUtility.FromJson<TokenInfo>(_msg);
m_Token = _textback.access_token;
}
}
}
/// <summary>
/// 返回的token
/// </summary>
[System.Serializable]
public class TokenInfo
{
public string access_token = string.Empty;
}
代码步骤2:使用网络上传图片生成描述数据
TaskData taskData=new TaskData();
string url = $"https://aip.baidubce.com/rpc/2.0/ernievilg/v1/txt2img?access_token={m_Token}";
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("text", "图片描述");
dic.Add("resolution", "1024*1024");//图片尺寸
dic.Add("style", "二次元");//图片风格
dic.Add("num", 1);//生成数量
using (UnityWebRequest www = UnityWebRequest.Post(url, "")) {
www.SetRequestHeader("Content-Type", "application/json");
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(dic));
www.uploadHandler = new UploadHandlerRaw(bodyRaw);
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
Debug.Log("结果1:"+ www.downloadHandler.text);
}
else
{
Debug.Log("结果2:" + www.error);
}
}
代码步骤3:百度服务器图片生成之后,获取图片的信息
RootObject rootObject = new RootObject();
string urls = $"https://aip.baidubce.com/rpc/2.0/ernievilg/v1/getImg?access_token={m_Token}";
using (UnityWebRequest www = UnityWebRequest.Post(urls, ""))
{
www.SetRequestHeader("Content-Type", "application/json");
Dictionary<string, object> dica = new Dictionary<string, object>();
dica.Add("taskId", "返回的任务ID");
byte[] bodyRaw = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(dica));
www.uploadHandler = new UploadHandlerRaw(bodyRaw);
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
Debug.Log("结果2:" + www.downloadHandler.text);
}
else
{
Debug.Log("结果2:" + www.error);
}
}
代码步骤4:根据返回信息中的网址下载图片 返回信息中的【status】0或1。"1"表示已生成完成,"0"表示任务排队中或正在处理.
using (UnityWebRequest www = UnityWebRequestTexture.GetTexture(rootObject.data.img)) {
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.Success)
{
// 成功下载图片
Texture2D texture = DownloadHandlerTexture.GetContent(www);
if (texture != null)
{
// 获取成功
Debug.log("获取成功");
}
}
else
{
// 下载失败
Debug.LogError("Failed to download image: " + www.error);
}
}
总结
本文介绍了如何在Unity项目中使用百度智能云的AI作画API。首先,需要在百度智能云官网注册账号并创建应用,获取API Key和Secret Key。然后,通过API在线调试确保接口可用。在代码实现部分,文章详细阐述了四个步骤:首先,根据API Key和Secret Key获取token;其次,使用网络上传图片生成描述数据;接着,在百度服务器图片生成之后,获取图片的信息;最后,根据返回信息中的网址下载图片。
每个步骤都提供了详细的代码示例和解释,便于读者理解和实现。通过本文的指导,读者可以在Unity项目中成功调用百度智能云的AI作画API,实现图片的生成和下载功能。