Unity中一些为自己的记录

来源:互联网 发布:深圳cnc编程招聘信息 编辑:程序博客网 时间:2024/05/16 04:18
//关闭unity
#if UNITY_EDITOR
            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的值

  1. //shader的写法  
  2. Properties {  
  3.     ...  
  4.     disHeight ("threshold distance", Float) = 3  
  5. }  
  6.    
  7. SubShader {  
  8.     Pass {  
  9.         CGPROGRAM  
  10.         #pragma vertex vert    
  11.         #pragma fragment frag   
  12.         ...  
  13.         uniform float disHeight;  
  14.         ...   
  15. // ===================================  
  16. // 修改shader中的disHeight的值  
  17. 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端:
[csharp]
  1. MeshFilter mesh = quard.GetComponent<MeshFilter> ();  
  2. // 创建和摄像机相关的renderTexture  
  3. RenderTexture rTex = new RenderTexture ((int)cam.pixelWidth, (int)cam.pixelHeight, 16);  
  4. cam.targetTexture = rTex;  
  5. mesh.renderer.material.mainTexture = rTex;  
shader端:
[csharp]
  1. #include "UnityCG.cginc"             
  2. sampler2D _MainTex;  
  3. sampler2D _NoiseTex;  
  4.       
  5. struct v2f {      
  6.     half4 pos:SV_POSITION;      
  7.     half2 uv : TEXCOORD0;   
  8.     float4 srcPos: TEXCOORD1;    
  9. };    
  10.   
  11. v2f vert(appdata_full v) {    
  12.     v2f o;    
  13.     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);    
  14.     o.uv = v.texcoord.xy;  
  15.     o.srcPos = ComputeScreenPos(o.pos);  
  16.     return o;    
  17. }    
  18.   
  19. fixed4 frag(v2f i) : COLOR0 {  
  20.     float tmp = saturate(1-(length(i.uv-float2(0.5,0.5)))*2)*0.04;            
  21.     float2 wcoord = (i.srcPos.xy/i.srcPos.w) + tex2D(_NoiseTex, i.uv) * tmp - tmp/2;  
  22.     return tex2D(_MainTex, wcoord);  
  23. }    
使用RenderTexture制作屏幕特效:
  1. [ExecuteInEditMode]  
  2. public class MyScreenEffect : MonoBehaviour {  
  3.     ...  
  4.     void OnRenderImage(RenderTexture source, RenderTexture dest) {  
  5.         Graphics.Blit(source, dest, material);  
  6.     }  
  7. }  
如果需要多次渲染,需要使用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), "在舞台上单击/双击查看输出!");}}


0 0
原创粉丝点击