一、隐藏unity启动logo
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Scripting;
/// <summary>
/// 不显示unity启动logo
/// </summary>
[Preserve]//特性,防止在打包的时候这个脚本 没有被打包进程序
public class StopVray
{
//在启动画面显示之前执行这个方法
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
private static void Test()
{
Task.Run(() =>
{
SplashScreen.Stop(SplashScreen.StopBehavior.StopImmediate);
});
}
}
二、WebGL中文输入
1、导入WebGLSupport 插件包 插件包地址:【免费】Unity-WebGLSupport资源-CSDN文库
2、导入插件后将WebGLInput脚本添加到Input组件上
3、打包完成后修改打包后的index.html文件,添加以下函数到<script>。。。</script>中
function getFullscreenElement() {
return document.fullscreenElement //standard property
|| document.webkitFullscreenElement //safari/opera support
|| document.mozFullscreenElement //firefox support
|| document.msFullscreenElement; //ie/edge support
}
function toggleFullscreen() {
if (getFullscreenElement()) {
//console.log("FullScreen");
} else {
document.getElementById("unity-container").requestFullscreen().catch();
}
}
document.addEventListener('click', () => {
toggleFullscreen();
});
三、随机打乱List顺序
newList=oldList.OrderBy(x=>Guid.NewGuid()).ToList();
四、Dropdown添加提示语
如图:![]()
其实在Dropdown组件中已经预留好了提示语,只需要新建一个text将其拖入placeholder中。注意Dropdown的提示语只有动态添加选项时才会显示提示语,所以想要看见提示语就需要将场景中Dropdown的候选项全部删除,然后在代码中动态添加候选项。

五、发送带basic验证的POST请求
猜猜我这个用来干嘛的,嘿嘿!!!
1、基础数据类
/// <summary>
/// API请求数据包
/// </summary>
public class AskData
{
/// <summary>
/// 包含迄今为止对话的消息列表
/// </summary>
public List<Message> messages;
/// <summary>
/// 要使用的模型的 ID
/// </summary>
public string model = "gpt-3.5-turbo";
/// <summary>
/// 聊天完成时生成的最大回答数。 输入标记和生成标记的总长度受到模型上下文长度的限制。
/// </summary>
public float max_tokens = 1000;
/// <summary>
/// 是否为流式输出
/// </summary>
public bool stream = true;
/// <summary>
/// 使用什么采样温度,介于 0 和 2 之间。较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使其更加集中和确定性。
/// </summary>
public float temperature = 0.2f;
}
/// <summary>
/// 消息结构
/// </summary>
public class Message
{
/// <summary>
/// 角色名
/// </summary>
public string role;
/// <summary>
/// 对话内容
/// </summary>
public string content;
}
2、HttpClient发送带basic验证的POST请求(WebGL下不可用)
/// <summary>
/// HttpClient发送带basic验证的POST请求
/// </summary>
/// <param name="value">当前发送的内容</param>
/// <param name="messages">历史消息</param>
/// <returns></returns>
public static async Task<string> Chat(string value, List<Message> messages)
{
Message message = new Message();
message.role = "user";
message.content = value;
messages.Add(message);
Debug.Log("发送内容: " + value);
messages.Reverse();
string requestBody = JsonConvert.SerializeObject(new AskData
{
messages = messages,
});
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", API_KEY);
var content = new StringContent(requestBody, Encoding.UTF8, "application/json");
var response = await client.PostAsync(apiUrl, content);
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
Debug.Log("收到内容: " + responseContent);
return responseContent;
}
else
{
Debug.Log("chat error " + response.StatusCode);
return "SERVER ERROR";
}
}
}
3、UnityWebRequest发送带basic验证的POST请求(WebGL下可用)
/// <summary>
/// UnityWebRequest发送带basic验证的POST请求
/// </summary>
/// <returns></returns>
IEnumerator Chat()
{
Message message = new Message();
message.role = "user";
message.content = inputContent.text;
messages.Add(message);
Debug.Log("发送内容: " + inputContent.text);
string json = JsonConvert.SerializeObject(new AskData
{
messages = messages,
});
UnityWebRequest request = new UnityWebRequest(apiUrl, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
request.downloadHandler = new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("Authorization", "Bearer " + API_KEY);
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
Debug.LogError(request.error);
else
Debug.Log("收到内容: " + request.downloadHandler.text);
}
答案是用于调用ChatGpt或其他AI大模型
六、对Short的每一位赋值
short a = 0;
//例子:如何设置short中每一位的值
// 1 << n 表示将数字 1 左移 n 位,这样可以将相应的位设为 1。
// |= (1 << n) 使用按位或操作将 a 的第 n 位设为 1。
// &= ~(1 << n) 使用按位与和取反操作将 a 的第 n 位清为 0。
a |= (short)(1 << 0); // b0 = 1
a |= (short)(1 << 1); // b1 = 1
// 设置 b2 为 0 (无操作,因为默认值是0)
// 设置 b3 为 1:
a |= (short)(1 << 3); // b3 = 1
// 继续设置其他位 b4~b15 ...
// 如果需要清除某一位,可以使用按位与和取反操作
// 例如,清除 b1 为 0:
a &= (short)~(1 << 1); // b1 = 0
//short第16位赋值为1
a = (short)(a | (1 << 15));
//例子:获取short中每一位的值,1个字节(bit)= 8位,short 为2个字节
for (int i = 0; i < 16; i++)
{
// 使用位移和按位与操作来获取第 i 位的值
int bitValue = (a >> i) & 1;
Debug.Log("第" + i + "位的值为: " + bitValue);
}
七、IIS服务器全MIME类型设置
文件扩展名:.* MIME类型:application/force-download
八、在IIS(Internet Information Services)上处理跨域资源共享(CORS, Cross-Origin Resource Sharing)问题
在服务器中的web.config添加以下代码段
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>
4246

被折叠的 条评论
为什么被折叠?



