UGUI實用功能參考

目錄

1.UGUI 藝術字體生成與使用

2.事件掛載

3.陰影

4.runbutton(按鍵監聽)(監聽手機的返回等等)

5.Ugui簡易滾動視圖


1.UGUI 藝術字體生成與使用

生成字體代碼

using UnityEngine;
using UnityEditor;
using System.IO;

public class CustomFont : MonoBehaviour
{
    //本方法是通過裁切的sprite導出字體文件,裁切使用的是unity自帶的sprite editor,方便操作。
    //另外,裁切之後,每個sprite的名字的最後一個字符對應了ascii碼的編碼,比如:
    //0: 我們只要將sprite的名字命名成xxx0,就可以了!
    //由於使用到的了sprite加載,所以字體圖片請放在Resources目錄下面,等製作完畢,再把他們放到fonts文件夾或者其他文件夾中即可。
    [MenuItem("Assets/CreateMyFontSprite")]
    static void CreateMyFontSprite()
    {

        Debug.LogWarning("abc");

        if (Selection.objects == null)
        {
            Debug.LogWarning("沒有選中Sprite文件,需要將Sprite Mode設置成Multiple,切分好,並且以以名字的最後一個字符當做ascii碼111");
            return;
        }
        if (Selection.objects.Length == 0)
        {
            Debug.LogWarning("沒有選中Sprite文件,需要將Sprite Mode設置成Multiple,切分好,並且以以名字的最後一個字符當做ascii碼");
            return;
        }
        string resoursePath = "Resources";
        UnityEngine.Object o = Selection.objects[0];
        if (o.GetType() != typeof(Texture2D))
        {
            Debug.LogWarning("選中的並不是圖片文件");
            return;
        }
        string selectionPath = AssetDatabase.GetAssetPath(o);
        Debug.Log("selectionPath=" + selectionPath);
        //if (selectionPath.Contains(resoursePath))
        {
            string selectionExt = Path.GetExtension(selectionPath);
            if (selectionExt.Length == 0)
            {
                Debug.LogWarning("選中的擴展名是爲空");
                return;
            }
            string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length);
            string fontPathName = loadPath + ".fontsettings";
            string matPathName = loadPath + ".mat";
            float lineSpace = 0.1f;//字體行間距,下面會根據最高的字體得到行間距,如果是固定高度,可以在這裏自行調整  
            loadPath = Path.GetFileNameWithoutExtension(selectionPath);
            Sprite[] sprites = Resources.LoadAll<Sprite>(loadPath);
            Debug.Log("loadPath=" + loadPath + ",size=" + sprites.Length);
            if (sprites.Length > 0)
            {
                //以textrue方式獲得該資源,可以設置到創建的材質中去  
                Texture2D tex = o as Texture2D;
                //創建字體材質,並且將圖片設置好  
                Material mat = new Material(Shader.Find("GUI/Text Shader"));
                AssetDatabase.CreateAsset(mat, matPathName);
                mat.SetTexture("_MainTex", tex);
                //創建字體文件,設置字體文件的材質  
                Font m_myFont = new Font();
                m_myFont.material = mat;
                AssetDatabase.CreateAsset(m_myFont, fontPathName);
                //創建字體中的字符集數組  
                CharacterInfo[] characterInfo = new CharacterInfo[sprites.Length];
                bool[] flags = new bool[sprites.Length];
                //得到最高的高度,設置行高和進行偏移計算  
                for (int i = 0; i < sprites.Length; i++)
                {
                    if (sprites[i].rect.height > lineSpace)
                    {
                        lineSpace = sprites[i].rect.height;
                    }
                }
                int realIndex = 0;
                for (int i = 0; i < sprites.Length; i++)
                {
                    Sprite spr = sprites[i];
                    if (!spr.name.StartsWith("font_"))
                    {
                        Debug.Log("skip sprite " + spr.name + ", the name doesn't starts with font_");
                        flags[i] = false;
                    }
                    else
                    {
                        flags[i] = true;
                        realIndex++;
                    }
                    CharacterInfo info = new CharacterInfo();
                    //設置ascii碼,使用切分sprite的最後一個字母  
                    info.index = (int)spr.name[spr.name.Length - 1];
                    Rect rect = spr.rect;
                    //根據pivot設置字符的偏移,具體需要做成什麼樣的,可以根據自己需要修改公式  
                    float pivot = spr.pivot.y / rect.height - 0.5f;
                    if (pivot > 0)
                    {
                        pivot = -lineSpace / 2 - spr.pivot.y;
                    }
                    else if (pivot < 0)
                    {
                        pivot = -lineSpace / 2 + rect.height - spr.pivot.y;
                    }
                    else
                    {
                        pivot = -lineSpace / 2;
                    }
                    Debug.Log(pivot);
                    int offsetY = (int)(pivot + (lineSpace - rect.height) / 2);
                    //設置字符映射到材質上的座標  
                    info.uvBottomLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y) / tex.height);
                    info.uvBottomRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y) / tex.height);
                    info.uvTopLeft = new Vector2((float)rect.x / tex.width, (float)(rect.y + rect.height) / tex.height);
                    info.uvTopRight = new Vector2((float)(rect.x + rect.width) / tex.width, (float)(rect.y + rect.height) / tex.height);
                    //設置字符頂點的偏移位置和寬高  
                    info.minX = 0;
                    info.minY = -(int)rect.height - offsetY;
                    info.maxX = (int)rect.width;
                    info.maxY = -offsetY;
                    //設置字符的寬度  
                    info.advance = (int)rect.width;
                    characterInfo[i] = info;
                }
                if (realIndex <= 0)
                {
                    Debug.Log("no valid sprite");
                    return;
                }
                Debug.Log("font sprites size = " + realIndex);
                var temp = new CharacterInfo[realIndex];
                for (int i = 0, temp_index = 0; temp_index < realIndex && i < characterInfo.Length; ++i)
                {
                    if (flags[i])
                    {
                        temp[temp_index++] = characterInfo[i];
                    }
                }
                // lineSpace += 2;  
                m_myFont.characterInfo = temp;
                EditorUtility.SetDirty(m_myFont);//設置變更過的資源  
                AssetDatabase.SaveAssets();//保存變更的資源  
                AssetDatabase.Refresh();//刷新資源,貌似在Mac上不起作用  

                //由於上面fresh之後在編輯器中依然沒有刷新,所以暫時想到這個方法,  
                //先把生成的字體導出成一個包,然後再重新導入進來,這樣就可以直接刷新了  
                //這是在Mac上遇到的,不知道Windows下面會不會出現,如果不出現可以把下面這一步註釋掉  
                AssetDatabase.ExportPackage(fontPathName, "temp.unitypackage");
                AssetDatabase.DeleteAsset(fontPathName);
                AssetDatabase.ImportPackage("temp.unitypackage", true);
                AssetDatabase.Refresh();

                //最佳高度:上下各留一個像素的間距,如果不需要可以註釋掉,根據需求更改  
                //打印是爲了使使用者方便填寫行高,因爲font不支持設置行高。  
                Debug.Log("創建字體成功, 最大高度:" + lineSpace + ", 最佳高度:" + (lineSpace + 2));
            }
            else
            {
                Debug.LogWarning("沒有選中Sprite文件,需要將Sprite放到Resources文件夾下面,可以參考函數上方的說明操作");
            }
        }
    }
}

 

2.事件掛載

代碼中的事件添加


        B_CameraSwitch.onClick.AddListener(() => {
            VehicleCamera.Inst.CameraSwitch();
        });

 

3.陰影

4.runbutton(按鍵監聽)(監聽手機的返回等等)

5.Ugui簡易滾動視圖

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章