標籤:unity 編輯器 外掛程式
好久沒有更新,有些急事終於處理完了,接著更新部落格!
廢話不多說,接著上面的講,今天我們來接著講述。
前面說到 怎麼去建立一個自己 ,用代碼繪製一個色塊,今天我們來講述下,怎麼繪製一個圖片在情境裡面。
首先 我們先做片的功課。
在unity中圖片的種類分了很多,預設是:Texture, 還有其他的一些屬性。
有些人可以會遇到 我放進去的素材為什麼編譯之後會變模糊那,這是由於,在unity中你沒有修改圖片的屬性,導致的。
在 texture模式下,
我們來分析下這種圖片的屬性,
wrap mode 這個是圖片選擇 圖片時候四方連續是會用到,也可以減輕,圖片白邊的影響。
filter mode 是 圖片的 檔案模式,說白一點就是 點,兩角線,三角線。。越高圖片品質越大,記憶體也就越高。
anise level 說白了 這個 我還沒用到 ,貌似是位元影像才會用到。
max size 最大支援圖片(注意哦是最大哦,假如我的圖是100*100的 你選擇1024 沒關係的 不會浪費 記憶體的,因為這裡說的是最大的圖集支援,如果你的圖片超過了1024,那這個值也要跟著變。)
format 這個就是 壓縮模式了,決定記憶體的最重要的部分,三個選項,自動壓縮(最節省),16位壓縮(還可以說的過去),真彩色可以說沒有壓縮,保證品質的同時記憶體相應的增大了。
進階選項模式:最為常用的模式。
Non power of 2 :如果你不想你的圖變成 正方形符合2的n次方的圖,就選擇none,這樣可以保證你圖的大小的合理性。不然選自動適應,最大適應,最小適應。
cubemap: 一般不使用,這個是用在模型上面的貼圖。
read/w:是否支援讀寫,建議勾選。
其他看下文檔。
format:一般我們用32位的,也可以選擇16位的。減少記憶體。
最後 送一句話,小圖盡量拼成大圖,對UV(也可以使用NGUI的自動壓縮圖,也可以自己壓縮,後面會講怎麼製作自己的圖集),大圖最好不要超過1024,面的數量盡量的減少,盡量使面合并。 目的減少記憶體,減少drawcall。
好,下一步。我們開始做把圖片渲染到情境裡面。
前面講了,我們繪製片,下面,我們做的之需要兩部。
1.把shader換掉,前面使用的是,純色的shader,下面我們將使用,帶貼圖的shader。
2. 前面我們使用的是固定的寬和高,因為圖片的大小不是固定的,所以我們要把比例進行適配一下。
上代碼:
shader部分,很簡單:
Shader "VK/VKTextureShader" { Properties { _Color ("Main Color", COLOR) = (1,1,1,1) _MainTex ("Base (RGB) Trans (A)", 2D) =""{} } SubShader { Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"} Pass { Blend SrcAlpha OneMinusSrcAlpha Cull off SetTexture [_MainTex] { constantColor [_Color] Combine texture *constant } } }}
調用前面的,繪製片的公用方法類InitBase,繼承VKview ,前面已經有,複製就好。
using UnityEngine;using System.Collections;public class VKImageView : VKView {Material imgViewDefultMat;Mesh imgViewDefultMesh;[HideInInspector] public Texture imgViewTex,highLightedTex;[HideInInspector] public float scale =1;[HideInInspector] public string info= null;[HideInInspector] public bool highLighted = false;[HideInInspector] public float alpha =1;// Use this for initializationvoid Start () {imgViewDefultMat = new Material (Shader.Find("VK/VKTextureShader"));imgViewDefultMesh = new Mesh ();GetComponent<MeshFilter> ().mesh = imgViewDefultMesh;GetComponent<MeshRenderer>().material = imgViewDefultMat;updateImageView ();}public void updateImageView(){if(imgViewTex!=null){if(!highLighted){if(imgViewDefultMat!=null) imgViewDefultMat.mainTexture = imgViewTex;if(imgViewDefultMesh!=null)imgViewDefultMesh.vertices = InitBase.initVertice(imgViewTex.width *scale,imgViewTex.height*scale,ancPointx,ancPointy);height = imgViewTex.height;width = imgViewTex.width;}else{if(imgViewDefultMat!=null)imgViewDefultMat.mainTexture = highLightedTex;if(imgViewDefultMesh!=null)imgViewDefultMesh.vertices = InitBase.initVertice(highLightedTex.width *scale,highLightedTex.height*scale,ancPointx,ancPointy);height = highLightedTex.height;width = highLightedTex.width;}}else{if(imgViewDefultMat!=null)imgViewDefultMat.mainTexture = null;if(imgViewDefultMesh!=null)imgViewDefultMesh.vertices = InitBase.initVertice(width*scale,height*scale,ancPointx,ancPointy);}if(imgViewDefultMat!=null){Color newcolor = imgViewDefultMat.color;imgViewDefultMat.color = new Color(newcolor.r,newcolor.g,newcolor.b,alpha);}if(imgViewDefultMesh!= null){imgViewDefultMesh.triangles = InitBase.initTri ();imgViewDefultMesh.normals = InitBase.initNormal();imgViewDefultMesh.uv = InitBase.initUV();}}public void switchButton(){ //後面會講到,可以先刪掉,這個是轉化按鈕來用的。VKButton button = gameObject.AddComponent<VKButton> ();button.buttonDefultMesh = imgViewDefultMesh;button.buttonDefultMat = imgViewDefultMat;button.buttonTex = imgViewTex;button.pressButtonTex = highLightedTex;button.info = info;button.scale = scale;button.ancPointx = ancPointx;button.ancPointy = ancPointy;button.updateButton ();DestroyImmediate (GetComponent<VKImageView>());}}
Editor下面的 編輯器類
using UnityEngine;using System.Collections;using UnityEditor;[CustomEditor(typeof(VKImageView))]public class VKImageViewEditor : Editor { public override void OnInspectorGUI () { base.OnInspectorGUI (); VKImageView imgView = (VKImageView)target; imgView.imgViewTex = EditorGUILayout.ObjectField ("ImageTexture",imgView.imgViewTex,typeof(Texture),true)as Texture; imgView.highLightedTex = EditorGUILayout.ObjectField ("HighLightedTex",imgView.highLightedTex,typeof(Texture),true)as Texture; imgView.alpha = EditorGUILayout.Slider("Alpha",imgView.alpha,0.0f,1.0f); imgView.highLighted = EditorGUILayout.Toggle ("highLighted",imgView.highLighted); imgView.info = EditorGUILayout.TextField ("info",imgView.info); imgView.ancPointx = EditorGUILayout.Slider("AnchorX",imgView.ancPointx,0.0f,1.0f); imgView.ancPointy = EditorGUILayout.Slider("AnchorY",imgView.ancPointy,0.0f,1.0f); if(imgView.imgViewTex == null){ imgView.width=EditorGUILayout.IntField("Width",imgView.width); imgView.height=EditorGUILayout.IntField("Height",imgView.height); } GUILayout.BeginHorizontal(); if(GUILayout.Button("2X")){ imgView.scale = 0.5f; } if(GUILayout.Button("1X")){ imgView.scale = 1f; } if(GUILayout.Button("1.5X")){ imgView.scale = 0.75f; } GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); if(GUILayout.Button("ChangeName")){ if(imgView.imgViewTex!=null){ imgView.name = imgView.imgViewTex.name; } } if(GUILayout.Button("SwitchButton")){ imgView.switchButton(); } GUILayout.EndHorizontal(); imgView.updateImageView(); if(imgView!=null) EditorUtility.SetDirty (imgView); EditorUtility.UnloadUnusedAssets (); }}
較多,第一部分先講到這裡。下次接著,不會的或看不懂可以留言哦,學習,噴子請繞路,謝謝。