Unity中一些为自己的记录
来源:互联网 发布:深圳cnc编程招聘信息 编辑:程序博客网 时间:2024/05/16 04:18
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
System.Diagnostics.Process.GetCurrentProcess().Kill();
#endif
1、问题: 从3dsMax导出fbx给Unity用,总是会丢失贴图。尽管Unity导入fbx时会自动导入材质球,但是贴图文件都会变成空的,要重新一个一个贴图填进去,真是太浪费时间了
解决方案: 3dsMax中模型和贴图采用统一命名,贴图文件拷贝到Unity项目中,然后再导入fbx,就会为自动创建的材质找到对应贴图了。
2、通过脚本修改shader中uniform的值
- //shader的写法
- Properties {
- ...
- disHeight ("threshold distance", Float) = 3
- }
- SubShader {
- Pass {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- ...
- uniform float disHeight;
- ...
- // ===================================
- // 修改shader中的disHeight的值
- gameObject.renderer.sharedMaterial.SetFloat("disHeight", height);
3、使得脚本能够在editor中实时反映:在脚本前加上:[ExecuteInEditMode],。Inspector中右键[ContextMenu("名字")]
可被序列化的属性,可以显示在Inspector面板上(可以使用HideInInspector来隐藏);public属性默认是可被序列化的,private默认是不可序列化的。使得private属性可被序列化,可以使用[SerializeField]来修饰。[SerializeField] private string ressName = "Sphere";
4、1)在NGUI中,一个Panel对应一个DrawCall(当然一个panel里面的UISprite来自多个图集,就会有多个drawCall);2)当一个UISprite等UIWidget控件的active改变时,DrawCall会重新计算一遍;3)DrawCall的计算比较耗时,首先会收集DrawCall下所有UIWidget的verts,norms,trans,uvs,cols等信息,还要向GPU传输数据,相对比较耗时。
问题:当在一个Panel中有大量的UIWidget,有一部分是经常改变active的,有些部分则不怎么变。这样就会导致频繁地计算DrawCall。
性能提升方案:把经常改变active的控件单独到一个panel中。把不怎么变动的部分独立到另外的panel中。
5、NGUI中设置自定义的Shader,并改变properties的值。
使用UITexture,可以设置自定义的Material。需要注意的是,改变properties的值,需要使用UITexture中的drawCall的dynamicMaterial,UITexture tex=....; tex.drawCall.dynamicMaterial.SetFloat("_percent", du/DU);
6、RenderTexture的全屏效果
cs端:
shader端:
- MeshFilter mesh = quard.GetComponent<MeshFilter> ();
- // 创建和摄像机相关的renderTexture
- RenderTexture rTex = new RenderTexture ((int)cam.pixelWidth, (int)cam.pixelHeight, 16);
- cam.targetTexture = rTex;
- mesh.renderer.material.mainTexture = rTex;
- #include "UnityCG.cginc"
- sampler2D _MainTex;
- sampler2D _NoiseTex;
- struct v2f {
- half4 pos:SV_POSITION;
- half2 uv : TEXCOORD0;
- float4 srcPos: TEXCOORD1;
- };
- v2f vert(appdata_full v) {
- v2f o;
- o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
- o.uv = v.texcoord.xy;
- o.srcPos = ComputeScreenPos(o.pos);
- return o;
- }
- fixed4 frag(v2f i) : COLOR0 {
- float tmp = saturate(1-(length(i.uv-float2(0.5,0.5)))*2)*0.04;
- float2 wcoord = (i.srcPos.xy/i.srcPos.w) + tex2D(_NoiseTex, i.uv) * tmp - tmp/2;
- return tex2D(_MainTex, wcoord);
- }
使用RenderTexture制作屏幕特效:
- [ExecuteInEditMode]
- public class MyScreenEffect : MonoBehaviour {
- ...
- void OnRenderImage(RenderTexture source, RenderTexture dest) {
- Graphics.Blit(source, dest, material);
- }
- }
如果需要多次渲染,需要使用RenderTexture.GetTemporary生成临时的RenderTexture,这个方法会从unity维护的池中获取,所以在使用完RenderTexture后,尽快调用RenderTexture.ReleaseTemporary使之返回池中。 RenderTexture存在于GPU的DRAM中,具体实现可以参考Cocos2dx中的写法来理解(CCRenderTexture.cpp中有具体实现)。GPU的架构请参考:http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter30.html
7、 浮点数相等的判断(由于浮点数有误差, 所以判断的时候最好不要用等号,尤其是计算出来的结果)
if (Mathf.Approximately(1.0, 10.0/10.0))
print ("same");
Unity3D 模拟鼠标单击/双击
using UnityEngine;using System.Collections;public class MouseResponseManager{public static void Register(GameObject objectItem, Action clickCallback, Action doubleCallback){if(objectItem == null){objectItem = new GameObject();objectItem.name = "MouseResponseItem";}MouseResponseItem mouseResponseItem = objectItem.AddComponent<MouseResponseItem> ();mouseResponseItem.Init (clickCallback, doubleCallback);}}public class MouseResponseItem : MonoBehaviour{private bool mouseDownStatus;private int mouseDownCount;private float lastTime;private float currentTime;private Action clickCallback;private Action doubleCallback;public void Init(Action clickCallback, Action doubleCallback){this.clickCallback = clickCallback;this.doubleCallback = doubleCallback;}void Update(){if(Input.GetMouseButtonDown(0)){if(!this.mouseDownStatus){this.mouseDownStatus = true;//Debug.Log("Click !");if(this.clickCallback != null) this.clickCallback();// 如果按住数量为 0if(this.mouseDownCount == 0){// 记录最后时间this.lastTime = Time.realtimeSinceStartup;}this.mouseDownCount ++;}}if(Input.GetMouseButtonUp(0)){//Debug.Log("Up !");this.mouseDownStatus = false;}if(this.mouseDownStatus){//Debug.Log("Hold !");if(this.mouseDownCount >= 2){this.currentTime = Time.realtimeSinceStartup;if(this.currentTime - this.lastTime < 0.3f){this.lastTime = this.currentTime;this.mouseDownCount = 0;//Debug.Log("Double Click");if(this.doubleCallback != null) this.doubleCallback();}else{// 记录最后时间this.lastTime = Time.realtimeSinceStartup;this.mouseDownCount = 1;}}}}}
using UnityEngine;using System.Collections;public class Test : MonoBehaviour {void Start(){MouseResponseManager.Register(null, ()=>{Debug.Log("click");}, ()=>{Debug.Log("double click");});}void OnGUI (){GUI.Label(new Rect(15, 15,300, 100), "在舞台上单击/双击查看输出!");}}
- Unity中一些为自己的记录
- 其他一些为自己的记录
- Unity中3D一些使用方法代码(为自己)
- 通讯中为自己的记录
- 记录一些Unity使用中遇到的奇葩问题
- 记录自己开发中踩过的一些坑
- Unity一些API函数解析(为自己整理)
- 一些Unity 优化建议 整理(为自己)
- 自己总结unity的一些面试题
- 把自己表中的一些记录 添加到自己的表中。。。。
- 决定记录一些工作中自己Fix一些Bug的思路
- 初用birt,记录一些自己的东西
- 奇思怪想----记录下自己的一些想法
- 自己装fedroa 18 的一些记录
- 记录一些自己看到的算法
- 自己学到的C++一些技巧记录
- 记录自己理解的一些设计模式
- 记录一些自己常用的链接
- ECC加密算法入门介绍
- linux 命令的相关网址
- cocos2d-x 中添加显示文字的三种方式 LabelTTF 、LabelBMFont 和 LabelAtlas
- Linux Linux下如何编译并运行C程序
- C++中的继承与虚函数各种概念
- Unity中一些为自己的记录
- Android检测系统的摄像头是否可用
- Android View 与ViewGroup绘制即事件分发机制
- 【未读好文章】
- android 从assets目录解压zip到本地sdCard
- Afinal简介
- freemarker 数字格式化深入理解
- Day3-相对布局
- 日志