/*add by HanSyaun for showing code on article. start*/ /*add by HanSyaun for showing code on article. end*/

2017年2月28日 星期二

程式注意事項

  1. 用 [Tooltip("text")]增加滑鼠移上去的說明
  2. 用Range產生拉bar來限定能調的數字
  3. 用requireComponent來強制一定要給gameObj
  4. 不需要給美術調的參數就private
  5. 能盡量合併的參數就合併
  6. public private分開放

     
  7. 空行要有意義
  8. private的參數預設值在start()裡給,避免有些機器不會預設給
  9. 永遠檢查null值,可設一個在start()初始化都ok才會繼續往下update()的bool
  10. 永遠小心除以零(x/0),如果是0可以給一個很小的數0.0000001
  11. 有時fixedUp會鈍鈍的,就改用Update()
  12. GameObject.Find 耗效能,可改用findWithTag
  13. 有debug error, warning等,可以提示使用者
  14. 在一段程式最後面設一個tag,前面沒執行過就會return,不會改到最後tag。第二段程式拿tag來判斷要不要執行。
  15. 有new東西出來記得在on Destory刪掉。
  16. 不要在update中存取transform(這樣等於getComponent),先在外面存起來再用
  17. compareTag比直接用==好。
  18. 存取material會造另一個instance出來,除非用sharedMaterial
  19. 善用find reference查看哪裡還有用到函式,善用find declaration找出函數或變數定義的位置。用watch和local panel來監控變數的改變。
  20. !component.enable 物件若關閉就啟動
  21. #define name與#if name來開關測試段落的code
  22. float數字記得要加f。
  23. 把常用的string或者是數字變成變數,移到最前面變成const string。之後多國語言之類的會比較方便更改。
  24. 連續用到兩次或三次的東西就把它變成function或變數。
  25. 先把別的程式要用的介面做好,內部的處理再慢慢改進。(封裝)
  26. OnTriggerEnter 寫成 onTriggerEnter


public fn(){

if xx then return;

if xx then return;

tag=true;

if tag==true then do something;

}


Applying the RequireComponent decoration to a script will ensure that the GameObject has the specified component.

If the component is missing:

    Unity will attempt to add a component of this type for you,
    If this fails (e.g. mixing RigidBody/RigidBody2D) then the script will refuse to attach.

  1. 。從rendere拿material,看起來是reference,其實是instance。所以要destroy他。如果是連結,就把連結的物件設為null,不用就斷開連結。
    void OnDestroy()
   {

       if (mat != null)
       {
           Material.Destroy(mat);
       }
   }


28. 要在onDestory裡把有new的東西都指定null,這樣才會GC。
29.error CS0428: Cannot convert method group `__' to non-delegate type `__'
Consider using parentheses to invoke the method (GetComponent<>之後要加括號)
   
30.拿rotation要用transform.eulerAngles才是0-360度。直接用transform.rotation是quaternion
31.新增material的空陣列 Material[] myMat = new Material[10];
32.怎樣在runtime把原本material size=1的skinMeshRenderer再加一個material? 直接指定一個material的array給他。
Material[] myMat = new Material[10];
targetSkinMeshRnd.sharedMaterials = myMat
33.有要用到的public變數或是getComponenet的東西,都要檢查例外,拿甚麼用就檢查甚麼。再來就是Destory,以及有沒有再浪費記憶體。
34.比較字串時使用 if (string.Compare(stringA,stringB) == 0 )
35.for比foreach快?
36.component + system。rendererAssigner和rigChanger是utility,是提供資料的component。RendererManager才是system。
37.只是temp new出來assign的東西不用放在全域來宣告,在local內宣告就好。加上temp關鍵字比較了解,不會以為可以從這個容器中拿東西。               
38.不要取的跟api的名子一樣(renderer)
39.多個"同樣"的class想要用的一樣的東西,可以用private static new一次就好,不用每個人身上的new一次。 ex: RendererManager中的hardLockShaders資料。
40.檢查空字串用string.IsNullOrEmpty(target),不要用 == " "
41.沒有把改完的color指定回去。
Color rndColor = Character.renderer.material.color
rndColor.a = 0.5;
Character.renderer.material.color = rndColor;
41.function name要讓人一看就知道裡面是在幹嘛
42.try finally用來無論怎樣都要執行finally block
43.定義變數和使用變數最好靠近一點,甚至一行。
44.使用var來new變數,可以少一點class命名的干擾 var myTrans = new Transform();
45.Array用[]。 int[] myInt = new int[5], 只是開出長度,還要一個個給值 myInt[0] = 1, myInt[1] = 2。 或是一次給: int[] myInt = {1,2,3,4,5};。 或是用找的: my int = FindGameObjectsWithTag(“Player”);  Array vs List vs Dictionrary
46.父類別已經有在onDestory()裡做事,子類別可以不覆蓋,還加上自己要做的事。
       protected override void OnDestroy()
       {
           Material.Destroy(mat);
           base.OnDestroy();
       }

沒有留言:

張貼留言