unity技巧unity3d
1) unity商店
2)官方新手入门 , unity3d学习手册
版本用2021的40?
3)Button加事件https://docs.unity3d.com/cn/current/Manual/script-Button.html
4)all compiler errors have to be fixed 报错是因为,脚本有语法错误。
5)Unable to parse Build/h5.framework.js.gz! 点击 构建和运行 运行正常, 发布到web服务器就报错了。
解决方法:左下角的Player Setting -> Publish Setting -> Decompression Fallback勾选上
, 然后重新发布。
6)从 Unity 脚本调用 JavaScript 函数WebGl调用js的官方文档
7)别人的webgl调用js的经验, unity js和cs相互调用 方案2
8)c#发送字符串到js,使用UTF8ToString
HelloString: function (str) {
console.log('str',str,UTF8ToString(str), stringToUTF8(str));
return (str);
}
// cs
[DllImport("__Internal")]
private static extern void HelloString(string str);
HelloString("This is a cs string.");
9)自定义WebGL的index.html首页。在Assets文件夹下新建文件夹WebGLTemplates->MyTemplate,创建index.html文件,在Player Settings -> Resolution and Presentation就能看到自定义模板了。
9)JavaScript调用Unity, js调用cs
// index.html
var gameInstance = createUnityInstance(document.querySelector("#unity-canvas"), {
//xxx
});
window.SendMessage = (GameObjectJS, func, arg) => {
gameInstance.then((unityInstance) => {
unityInstance.SendMessage(GameObjectJS, func, arg);
});
}
// jslib
CallCs: function() {
SendMessage("GameObjectJS", "JsCallCs", "js to cs Hello, world!");
}
//cs
CallCs();
public void JsCallCs(string str){
Debug.Log("JsCallTs"+str);
}
从JS端调用这些函数时,请在函数前加上"_"
10)unity调用js
11) unity3d编译器代码提示 https://docs.unity3d.com/cn/current/Manual/ScriptingToolsIDEs.html
Visual Studio 编辑器 Unity 包 , window下面安装一下 Game development with Unity这个就有代码提示了Tools for Unity Edit > Preferences menu. On the left, select the External Tools tab.选vs2022就搞定啦。
12)unity动画讲解
13)字符串转json
public class testjson
{
public string aa { get; set; }
public string bb { get; set; }
}
testjson res1 = = (testjson)Newtonsoft.Json.JsonConvert.DeserializeObject(output1, typeof(strJson));
14)加载prefab
var prefab = Resources.Load<GameObject>("Prefabs/OnePbf");
var go = GameObject.Instantiate(prefab);
go.transform.position = this.targetGobj.transform.position+ new Vector3(Random.Range(-1f,1f), 0, 0);
go.transform.SetParent(this.targetGobj.transform, true);
go.transform.GetComponent<PrefabOnePbf>().SetPbfData(0);
15)定时器
void Hello(){
}
this.Invoke("Hello",5.0f);//5秒后执行Hello函数
16)层级显示,同一个Canvas可以调整前后顺序,不同Canvas可以调整Sort Order来设置层级。sprite可以用order in layer。也可以用官方例子的spriteRander来写。
17)camera旋转
18)Unable to parse Build/h5.framework.js.unityweb,在player Setting里面把Decompression Fallback勾选掉
19)(官方解决gzip压缩问题)[https://docs.unity3d.com/cn/current/Manual/webgl-deploying.html]
20)unity image 动态换图片
Image img;
//image路径
string path = "Images/Item/img";
//参数为资源路径和资源类型
Sprite sprite = Resources.Load(path,typeof(Sprite)) as Sprite;
//动态更换image
Img.sprite = sprite;
21)