完整代码下载地址: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);
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |