unity小技巧收集总结

版权声明:未经允许不可转载 https://blog.csdn.net/Edision_li/article/details/89921089

1、asset序列化:

[CreateAssetMenu(fileName ="ABConfig",menuName ="CreateABConfig",order =0)]
public class ABConfig : ScriptableObject {

    public List<string> m_AllPrefabsPath=new List<string>();
    public List<FileDirABName> m_AllFileDirAB = new List<FileDirABName>();

    [System.Serializable]
    public struct FileDirABName
    {
        public string ABName;
        public string Path;
    }
}

2、打包AB包:

   [MenuItem("Tools/1.打包AB包",false,1)]
    public static void BuildAB()
    {
        //EditorUserBuildSettings.activeBuildTarget检测运行所在平台
        //将AB打包在StreamingAssets文件夹下,注意先创建该文件夹
        BuildPipeline.BuildAssetBundles(Application.streamingAssetsPath, BuildAssetBundleOptions.ChunkBasedCompression,EditorUserBuildSettings.activeBuildTarget);
       AssetDatabase.Refresh(); //编辑器的刷新
    }

3、编辑器查找和进度显示:

  //查找到所有prefabs,转换成GUIID数组。
        string[] allPrefabsGUIDArr = AssetDatabase.FindAssets("t:Prefab", abConfig.m_AllPrefabsPath.ToArray());
        for (int i = 0; i < allPrefabsGUIDArr.Length; i++)
        {
            string path = AssetDatabase.GUIDToAssetPath(allPrefabsGUIDArr[i]);
            Debug.Log(path);
            EditorUtility.DisplayProgressBar("查找prefabs", "path:" + path, i * 1.0f / allPrefabsGUIDArr.Length);
        }
        EditorUtility.ClearProgressBar();

四、代码设置AB包名:

  static void SetABName(string name,string path)
    {
        AssetImporter assetImporter = AssetImporter.GetAtPath(path);
        if (assetImporter==null)
        {
            Debug.LogError("不存在此路径的文件:" + path);
        }
        else
        {
            assetImporter.assetBundleName = name;
        }
    }

五、代码清除Ab包名:

     string[] oldABNameArr = AssetDatabase.GetAllAssetBundleNames();
        for (int i = 0; i < oldABNameArr.Length; i++)
        {
            AssetDatabase.RemoveAssetBundleName(oldABNameArr[i],true);//是否强制清除
            EditorUtility.DisplayProgressBar("清除AB包名", "Name:" + oldABNameArr[i], i * 1.0f / allPrefabsGUIDArr.Length);

        }

        EditorUtility.ClearProgressBar();

六、简易单例

public class Singleton<T> where T:new()
{
    private static T m_instance;
    public static T Instance
    {
        get
        {
            if (m_instance==null)
            {
                m_instance = new T();
            }
            return m_instance;
        }
    }
}

七、移动端判断是否点在UGUI组件上

UGUI 提供了一个检测是否点击在UI上的方法 EventSystem.current.IsPointerOverGameObject(); 在EventSystem的标准输入Standalone Input Model下是正常的,但是在Touch Input Module输入模式下不正常(编辑器中正常,Android端不正常)

 public static bool IsPointerOverGameObject()
    {
        PointerEventData eventData = new PointerEventData(UnityEngine.EventSystems.EventSystem.current);
        eventData.pressPosition = Input.mousePosition;
        eventData.position = Input.mousePosition;
 
        List<RaycastResult> list = new List<RaycastResult>();
        UnityEngine.EventSystems.EventSystem.current.RaycastAll(eventData, list);
        return list.Count > 0;
    }

或者

#if UNITY_EDITOR
        if (EventSystem.current.IsPointerOverGameObject())
        {

        }
#elif UNITY_IOS || UNITY_ANDROID
        if (Input.touchCount>0&& EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
        {

        }
#endif

end

猜你喜欢

转载自blog.csdn.net/Edision_li/article/details/89921089