轉發,請保持地址:http://blog.csdn.net/stalendp/article/details/17114135
這篇文章將收集unity的相關技巧,會不斷地更新內容。
1)保存運行中的狀態
unity在運行狀態時是不能夠保存的。但在運行時編輯的時候,有時會發現比較好的效果想保存。這時可以在 “Hierarchy”中複製相關對象樹,暫停遊戲後替換原來的,就可以了。(其實這個拷貝過程是序列化過程,這種方法是序列化到內存中;另外一種方法就是序列化到磁盤上,即把內容拖動到文件夾中變成prefab,效果也是一樣的)
2)Layer的用法
LayerMask.NameToLayer("Ground");
// 通過名字獲取layer
3D Raycast
- RaycastHit hit;
- if(Physics.Raycast(cam3d.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity, (1<<LayerMask.NameToLayer("Ground")))) {
- ...
- }
2D Raycast
- Collider2D h = Physics2D.OverlapPoint(cam2d.ScreenToWorldPoint(Input.mousePosition), (1<<LayerMask.NameToLayer("xxx")));
- if(h) {
- ...
- }
3)物理攝像頭取色(WebCamTexture)
- Texture2D exactCamData() {
- // get the sample pixels
- Texture2D snap = new Texture2D((int)detectSize.x, (int)detectSize.y);
- snap.SetPixels(webcamTexture.GetPixels((int)detectStart.x, (int)detectStart.y, (int)detectSize.x, (int)detectSize.y));
- snap.Apply();
- return snap;
- }
保存截圖:
- System.IO.File.WriteAllBytes(Application.dataPath + "/test.png", exactCamData().EncodeToPNG());
4) 操作componenent
添加:
- CircleCollider2D cld = (CircleCollider2D)colorYuan[i].AddComponent(typeof(CircleCollider2D));
- cld.radius = 1;
刪除:
- Destroy(transform.gameObject.GetComponent<SpriteRenderer>());
5)動畫相關
狀態Init到狀態fsShake的的條件爲:參數shake==true;代碼中的寫法:
觸發fsShake:
- void Awake() {
- anims = new Animator[(int)FColorType.ColorNum];
- }
- ....
- if(needShake) {
- curAnim.SetTrigger("shake");
- }
關閉fsShake
- void Update() {
- ....
- if(curAnim) {
- AnimatorStateInfo stateInfo = curAnim.GetCurrentAnimatorStateInfo(0);
- if(stateInfo.nameHash == Animator.StringToHash("Base Layer.fsShake")) {
- curAnim.SetBool("shake", false);
- curAnim = null;
- print ("======>>>>> stop shake!!!!");
- }
- }
- ....
- }
關於Animator.StringToHash函數的說明:
animator的setBool,setTrigger等函數都有兩種參數形式,一個接收string,一個接收hash;其實Animator內部是使用hash來記錄相應信息的,所以接收string類型的函數內部會幫你調用StringToHash這個函數;所以如果要經常調用setTrigger等,請把參數名稱hash化保持以便反覆使用,從而提高性能。
在狀態機animator中獲取animation state 和animation clip的方法(參考http://answers.unity3d.com/questions/692593/get-animation-clip-length-using-animator.html):
- public static AnimationClip getAnimationClip(Animator anim, string clipName) {
- UnityEditorInternal.State state = getAnimationState(anim, clipName);
- return state!=null ? state.GetMotion() as AnimationClip : null;
- }
- public static UnityEditorInternal.State getAnimationState(Animator anim, string clipName) {
- UnityEditorInternal.State state = null;
- if(anim != null) {
- UnityEditorInternal.AnimatorController ac = anim.runtimeAnimatorController as UnityEditorInternal.AnimatorController;
- UnityEditorInternal.StateMachine sm = ac.GetLayer(0).stateMachine;
- for(int i = 0; i < sm.stateCount; i++) {
- UnityEditorInternal.State _state = sm.GetState(i);
- if(state.uniqueName.EndsWith("." + clipName)) {
- state = _state;
- }
- }
- }
- return state;
- }
6)scene的切換
同步方式:
- Application.LoadLevel(currentName);
異步方式:
- Application.LoadLevelAsync("ARScene");
7)加載資源
- Resources.Load<Texture>(string.Format("{0}{1:D2}", mPrefix, 5));
8)Tag VS. Layer
-> Tag用來查詢對象
-> Layer用來確定哪些物體可以被raycast,還有用在camera render中
9)旋轉
transform.eulerAngles 可以訪問 rotate的 xyz
- transform.RotateAround(pivotTransVector, Vector3.up, -0.5f * (tmp-preX) * speed);
- PlayerPrefs.SetInt("isInit_" + Application.loadedLevelName, 1);
11)動畫編碼
http://www.cnblogs.com/lopezycj/archive/2012/05/18/Unity3d_AnimationEvent.html
http://game.ceeger.com/Components/animeditor-AnimationEvents.html
http://answers.unity3d.com/questions/8172/how-to-add-new-curves-or-animation-events-to-an-im.html
12) 遍歷子對象
- Transform[] transforms = target.GetComponentsInChildren<Transform>();
- for (int i = 0, imax = transforms.Length; i < imax; ++i) {
- Transform t = transforms[i];
- t.gameObject.SendMessage(functionName, gameObject, SendMessageOptions.DontRequireReceiver);
- }
13)音效的播放
先添加Auido Source, 設置Audio Clip, 也可以在代碼中加入。然後在代碼中調用audio.Play(), 參考如下代碼:
- public AudioClip aClip;
- ...
- void Start () {
- ...
- audio.clip = aClips;
- audio.Play();
- ...
- }
另外,如果是3d音效的話,需要調整audio Souce中的panLevel才能聽到聲音,不清楚原因。
14)調試技巧(Debug)
可以在OnDrawGizmos函數來進行矩形區域等,達到調試的目的,請參考NGUI中的UIDraggablePanel.cs文件中的那個函數實現。
- #if UNITY_EDITOR
- /// <summary>
- /// Draw a visible orange outline of the bounds.
- /// </summary>
- void OnDrawGizmos ()
- {
- if (mPanel != null)
- {
- Bounds b = bounds;
- Gizmos.matrix = transform.localToWorldMatrix;
- Gizmos.color = new Color(1f, 0.4f, 0f);
- Gizmos.DrawWireCube(new Vector3(b.center.x, b.center.y, b.min.z), new Vector3(b.size.x, b.size.y, 0f));
- }
- }
- #endif
15)延時相關( StartCoroutine)
- StartCoroutine(DestoryPlayer());
- ...
- IEnumerator DestoryPlayer() {
- Instantiate(explosionPrefab, transform.position, transform.rotation);
- gameObject.renderer.enabled = false;
- yield return new WaitForSeconds(1.5f);
- gameObject.renderer.enabled = true;
- }
16)Random做種子
- Random.seed = System.Environment.TickCount;
- 或者
- Random.seed = System.DateTime.Today.Millisecond;
17) 調試技巧(debug),可以把值方便地在界面上打印出來
- void OnGUI() {
- GUI.contentColor = Color.green;
- GUILayout.Label("deltaTime is: " + Time.deltaTime);
- }
18)分發消息
sendMessage, BroadcastMessage等
19) 遊戲暫停(對timeScale進行設置)
- Time.timeScale = 0;
20) 實例化一個prefab
- Rigidbody2D propInstance = Instantiate(backgroundProp, spawnPos, Quaternion.identity) as Rigidbody2D;
21)Lerp函數的使用場景
- // Set the health bar's colour to proportion of the way between green and red based on the player's health.
- healthBar.material.color = Color.Lerp(Color.green, Color.red, 1 - health * 0.01f);
22)在特定位置播放聲音
- // Play the bomb laying sound.
- AudioSource.PlayClipAtPoint(bombsAway,transform.position);
23) 浮點數相等的判斷(由於浮點數有誤差, 所以判斷的時候最好不要用等號,尤其是計算出來的結果)
- if (Mathf.Approximately(1.0, 10.0/10.0))
- print ("same");
24)通過腳本修改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);
25) 獲取當前level的名稱
- Application.loadedLevelName
26)雙擊事件
- void OnGUI() {
- Event Mouse = Event.current;
- if ( Mouse.isMouse && Mouse.type == EventType.MouseDown && Mouse.clickCount == 2) {
- print("Double Click");
- }
- }
27) 日期:
- System.DateTime dd = System.DateTime.Now;
- GUILayout.Label(dd.ToString("M/d/yyyy"));
28) RootAnimation中移動的腳本處理
- class RootControl : MonoBehaviour {
- void OnAnimatorMove() {
- Animator anim = GetComponent<Animator>();
- if(anim) {
- Vector3 newPos = transform.position;
- newPos.z += anim.GetFloat("Runspeed") * Time.deltaTime;
- transform.position = newPos;
- }
- }
- }
29) BillBoard效果(廣告牌效果,或者向日葵效果,使得對象重視面向攝像機)
- public class BillBoard : MonoBehaviour {
- // Update is called once per frame
- void Update () {
- transform.LookAt(Camera.main.transform.position, -Vector3.up);
- }
- }
30)script中的屬性編輯器(Property Drawers),還可以自定義屬性編輯器
參考: http://blogs.unity3d.com/2012/09/07/property-drawers-in-unity-4/
http://docs.unity3d.com/Manual/editor-PropertyDrawers.html
其中Popup好像無效,但是enum類型的變量,能夠達到Popup的效果
- public class Example : MonoBehaviour {
- public string playerName = "Unnamed";
- [Multiline]
- public string playerBiography = "Please enter your biography";
- [Popup ("Warrior", "Mage", "Archer", "Ninja")]
- public string @class = "Warrior";
- [Popup ("Human/Local", "Human/Network", "AI/Easy", "AI/Normal", "AI/Hard")]
- public string controller;
- [Range (0, 100)]
- public float health = 100;
- [Regex (@"^(?:\d{1,3}\.){3}\d{1,3}$", "Invalid IP address!\nExample: '127.0.0.1'")]
- public string serverAddress = "192.168.0.1";
- [Compact]
- public Vector3 forward = Vector3.forward;
- [Compact]
- public Vector3 target = new Vector3 (100, 200, 300);
- public ScaledCurve range;
- public ScaledCurve falloff;
- [Angle]
- public float turnRate = (Mathf.PI / 3) * 2;
- }
31)Mobile下面使用lightmapping問題的解決方案
在mobile模式下,lightmapping可能沒有反應,可以嘗試使用mobile下的shader,可以解決問題。更多請參考:http://forum.unity3d.com/threads/138978-Lightmap-problem-in-iPhone
32) Unity下畫線的功能(用於debug)
- Debug.DrawLine (Vector3.zero, new Vector3 (10, 0, 0), Color.red);
33)Shader中代碼的複用(CGINCLUDE的使用)
- Shader "Self-Illumin/AngryBots/InterlacePatternAdditive" {
- Properties {
- _MainTex ("Base", 2D) = "white" {}
- //...
- }
- CGINCLUDE
- #include "UnityCG.cginc"
- sampler2D _MainTex;
- //...
- struct v2f {
- half4 pos : SV_POSITION;
- half2 uv : TEXCOORD0;
- half2 uv2 : TEXCOORD1;
- };
- v2f vert(appdata_full v) {
- v2f o;
- // ...
- return o;
- }
- fixed4 frag( v2f i ) : COLOR {
- // ...
- return colorTex;
- }
- ENDCG
- SubShader {
- Tags {"RenderType" = "Transparent" "Queue" = "Transparent" "Reflection" = "RenderReflectionTransparentAdd" }
- Cull Off
- ZWrite Off
- Blend One One
- Pass {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma fragmentoption ARB_precision_hint_fastest
- ENDCG
- }
- }
- FallBack Off
- }
34)獲取AnimationCurve的時長
- _curve.keys[_curve.length-1].time;
35)C#中string轉變成byte[]:
- byte[] b1 = System.Text.Encoding.UTF8.GetBytes (myString);
- byte[] b2 = System.Text.Encoding.ASCII.GetBytes (myString);
- System.Text.Encoding.Default.GetBytes(sPara)
- new ASCIIEncoding().GetBytes(cpara);
- char[] cpara=new char[bpara.length];
- for(int i=0;i <bpara.length;i ++){char[i]=system.convert.tochar(bpara[i]);}
36) 排序
- list.Sort(delegate(Object a, Object b) { return a.name.CompareTo(b.name); });
37) NGUI的相關類關係:
38)使得腳本能夠在editor中實時反映:
在腳本前加上:[ExecuteInEditMode], 參考UISprite。
39)隱藏相關屬性
屬性前加上 [HideInInspector],在shader中也適用;
比如:
- public class ResourceLoad : MonoBehaviour {
- [HideInInspector] public string ressName = "Sphere";
- public string baseUrl = "http://192.168.56.101/ResUpdate/{0}.assetbundle";
- Shader "stalendp/imageShine" {
- Properties {
- [HideInInspector] _image ("image", 2D) = "white" {}
- _percent ("_percent", Range(-5, 5)) = 1
- _angle("_angle", Range(0, 1)) = 0
- }
40)屬性的序列化
可被序列化的屬性,可以顯示在Inspector面板上(可以使用HideInInspector來隱藏);public屬性默認是可被序列化的,private默認是不可序列化的。使得private屬性可被序列化,可以使用[SerializeField]來修飾。如下:
- [SerializeField] private string ressName = "Sphere";
41)Shader編譯的多樣化(Making multiple shader program variants)
shader通常如下的寫法:
#pragma multi_compile FANCY_STUFF_OFF FANCY_STUFF_ON 這樣可以把Shader編譯成多個版本。使用的時候,局部設置Material.EnableKeyword, DisableKeyword;或則 全局設置Shader.EnableKeyword and DisableKeyword進行設置。
42)多個scene共享內容
- void Awake() {
- DontDestroyOnLoad(transform.gameObject);
- }
首先需要設置AnimationType的類型爲1 (需要在animationClip的Debug模式的屬性中進行設置,如下圖。另外,如果需要在animator中播放動畫,需要類型爲2);
代碼如下:
- string clipName = "currentClip";
- AnimationClip clip = ...;
- // 設置animationClip
- if (anim == null) {
- anim = gameObject.AddComponent<Animation>();
- }
- anim.AddClip(clip, clipName);
- anim[clipName].speed = _speedScale;
- // 計算時間
- float duration = anim[clipName].length;
- duration /= _speedScale;
- // 播放動畫
- anim.Play(clipName);
- yield return new WaitForSeconds(duration + 0.1f);
- // 動畫結束動作
- anim.Stop();
- anim.RemoveClip(clipName);
44) RenderTexture的全屏效果
cs端:
- 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);
- }
45)使用RenderTexture製作屏幕特效:
- [ExecuteInEditMode]
- public class MyScreenEffect : MonoBehaviour {
- ...
- void OnRenderImage(RenderTexture source, RenderTexture dest) {
- Graphics.Blit(source, dest, material);
- }
- }
46) 特定條件下暫停unity
在調試戰鬥的時候,需要在特定條件下暫停unity,以便查看一些對象的設置。可以使用Debug.Break();來達到目的。這是需要處於debug模式,才能夠達到效果。
47)在特定exception發生時暫停執行;
默認情況下,unity在遇到異常時,是不會停止執行的。但是如果要在nullReferenceException等exception發生時,進行調試。可以在MonoDevelop的菜單中選擇Run/Exceptions..., 做相關設置:
48)NGUI性能建議
相關知識: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中。
49)NGUI中設置自定義的Shader,並改變properties的值。
使用UITexture,可以設置自定義的Material。需要注意的是,改變properties的值,需要使用UITexture中的drawCall的dynamicMaterial,如下:
- UITexture tex=....;
- tex.drawCall.dynamicMaterial.SetFloat("_percent", du/DU);
50) 鑑別unity中的InputTouch的類型:
- public enum OperateStatus {
- OS_Idle=0,
- OS_Draging=1,
- OS_Scaling=2
- }
- .....
- void Update () {
- ......
- TouchPhase tp = Input.GetTouch(0).phase;
- if(tp==TouchPhase.Ended || tp==TouchPhase.Canceled) {
- oStatus = OperateStatus.OS_Idle;
- } else {
- oStatus = Input.touchCount>1 ? OperateStatus.OS_Scaling : OperateStatus.OS_Draging;
- }
- .....
- }
Unity中涉及物理模擬的有靜態碰撞(static collider)和動態碰撞(dynamic collider)。其中,靜態碰撞物體是指具有collider屬性但沒有rigidbody的物體,動態碰撞物體是指具有collider和rigidbody屬性的物體。
Unity引擎期待靜態物體是靜止,會把場景中的所有靜態物體統一計算成一個叫static collider cache的東西來加速物理模擬。但是如果其中某個靜態物體進行了移動,系統就要重新計算static collider cache;而這個代價是很大的。所以如果一個物體是要經常移動的,應該設計成動態物體,即添加rigidbody成分;至於使rigidbody不響應重力等作用力的影響,請參考rigidbody中的"Use Gravity"和“Is Kinematic”屬性。
如果rigidbody是kinematic的,它不響應force,可以用來製作飛行障礙物等效果。如果不是kinematic的,就會響應foce。
參考如下官方教程最後的性能分析:http://unity3d.com/learn/tutorials/projects/roll-a-ball/collecting-and-counting
52) unity中四種Collider的對比:
類型 | 能否移動 | 是否接收Trigger消息 | 是否受碰撞影響 | 是否受物理影響 |
Static | 代價大 | No | No | No |
Kinematic Rigidbody | Yes | Yes | No | No |
Character Controller | Yes | 未知 | Yes | No |
Rigidbody | Yes | Yes | Yes | Yes |
仔細觀察上面的表格,發現越往下,特性越多。官方在介紹collision的時候,沒有加入討論Character Controller,這個是我自己添加比較的。
另外,如果一個物體能夠受碰撞影響,當然就能夠接收Collition消息了。
關於接收Collision消息和Trigger消息的官方的詳細表,請參考http://docs.unity3d.com/Manual/CollidersOverview.html 的文末的表格。
53) 使一個物體依附在一個表面上,保持物體的up方向和表面的法線方向一致(物體記爲obj,表面記爲suf):
方法1:
- obj.transform.rotation = Quaternion.FromToRotation(obj.transform.up, suf.transform.up)* obj.transform.rotation;
方法2:
- Vector3 axis = Vector3.Cross(obj.transform.up, suf.transform.up);
- float angle = Vector3.Angle(obj.transform.up, suf.transform.up);
- obj.transform.Rotate(axis, angle, Space.World);
- @script ExecuteInEditMode
- private var gui : GUIText;
- private var updateInterval = 1.0;
- private var lastInterval : double; // Last interval end time
- private var frames = 0; // Frames over current interval
- function Start()
- {
- lastInterval = Time.realtimeSinceStartup;
- frames = 0;
- }
- function OnDisable ()
- {
- if (gui)
- DestroyImmediate (gui.gameObject);
- }
- function Update()
- {
- #if !UNITY_FLASH
- ++frames;
- var timeNow = Time.realtimeSinceStartup;
- if (timeNow > lastInterval + updateInterval)
- {
- if (!gui)
- {
- var go : GameObject = new GameObject("FPS Display", GUIText);
- go.hideFlags = HideFlags.HideAndDontSave;
- go.transform.position = Vector3(0,0,0);
- gui = go.guiText;
- gui.pixelOffset = Vector2(5,55);
- }
- var fps : float = frames / (timeNow - lastInterval);
- var ms : float = 1000.0f / Mathf.Max (fps, 0.00001);
- gui.text = ms.ToString("f1") + "ms " + fps.ToString("f2") + "FPS";
- frames = 0;
- lastInterval = timeNow;
- }
- #endif
- }
- public class AlphaNumericSort : BaseHierarchySort
- {
- public override int Compare(GameObject lhs, GameObject rhs)
- {
- if (lhs == rhs) return 0;
- if (lhs == null) return -1;
- if (rhs == null) return 1;
- return EditorUtility.NaturalCompare(lhs.name, rhs.name);
- }
- }
BaseHierarchySort的官方地址:http://docs.unity3d.com/ScriptReference/BaseHierarchySort.html