unity的FileHelper

using System.IO;
using UnityEngine;


public class FileHelper
{

#if UNITY_IPHONE || UNITY_ANDROID

    #if UNITY_EDITOR
        static string localPath = Application.dataPath+"/../../../../trunk/config/";
    #else
        static string localPath = Application.persistentDataPath;
    #endif
    
#elif UNITY_EDITOR
	// static string localPath = Application.dataPath+"/CustomTemp/";
    static string localPath = Application.dataPath+"/../../../../trunk/config/";
#elif UNITY_STANDALONE_WIN || UNITY_STANDALONE_MAC
    static string localPath = Application.dataPath+"/../../../../trunk/config/";
#endif

    static FileHelper()
    {
        if(!Directory.Exists(localPath))
        { 
            Directory.CreateDirectory(localPath);
        }
    }
    
    /// <summary>
    /// 获取文件夹下面的所有文件
    /// </summary>
    public static FileInfo[] GetFileInfosFromDirectory(string relativePath)
    {
        string absPath = Path.Combine(localPath,relativePath);
        if(!Directory.Exists(absPath))
        { 
            Directory.CreateDirectory(absPath);
        } 
        
        DirectoryInfo info = new DirectoryInfo(absPath);
        return info.GetFiles();
    }

    public static void WriteToFile(string name, string content)
    {
        string filePath = Path.Combine(localPath,name);
        Debug.LogFormat("filePath:{0}",filePath);
        FileInfo file = new FileInfo(filePath);
        using(StreamWriter sw = file.CreateText())
        {
            //写入信息
            sw.Write(content);
        }
    }

    
    public static void WriteToFile(string name, string content,string strPath)
    {
        string filePath = Path.Combine(strPath, name);
        Debug.LogFormat("filePath:{0}", filePath);
        FileInfo file = new FileInfo(filePath);
        using (StreamWriter sw = file.CreateText())
        {
            //写入信息
            sw.Write(content);
            sw.Flush();
            sw.Close();
        }
    }
    
    
    public static string ReadFromFile(string name)
    {
        string filePath = Path.Combine(localPath,name);
        FileInfo file = new FileInfo(filePath);
        string content = "";
        if (file.Exists)
        {
            using(StreamReader sr = file.OpenText())
            {
                content = sr.ReadToEnd();
            }
        }
        
        return content;
    }

    public static string ReadFromFile(string name, string strPath)
    {
        string filePath = Path.Combine(strPath, name);
        FileInfo file = new FileInfo(filePath);
        string content = "";
        if (file.Exists)
        {
            using (StreamReader sr = file.OpenText())
            {
                content = sr.ReadToEnd();
            }
        }

        return content;
    }
    
    public static void DeleteFile(string name)
    {
        string filePath = Path.Combine(localPath,name);
        FileInfo file = new FileInfo(filePath);
        if (file.Exists)
        {
            file.Delete();
            Debug.LogFormat("删除文件:{0}",file.Name);
        }
    }
    
}



using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class Test : MonoBehaviour
{


    /// <summary>
    /// 根据指定的资源类型,查找游戏资源
    /// </summary>
    /// <returns>返回资源对象</returns>
    /// <param name="name">资源名称</param>
    public static T findRes<T>(string name) where T : Object
    {
        T[] objs = Resources.FindObjectsOfTypeAll<T>();
        if (objs != null && objs.Length > 0)
        {
            foreach (Object obj in objs)
            {
                if (obj.name == name)
                    return obj as T;
            }
        }
        objs = AssetBundle.FindObjectsOfType<T>();
        if (objs != null && objs.Length > 0)
        {
            foreach (Object obj in objs)
            {
                if (obj.name == name)
                    return obj as T;
            }
        }
        return default(T);
    }

    /// <summary>
    /// 获取文件扩展名
    /// </summary>
    /// <param name="file">文件名称(可以带路径).</param>
    public static string getFileExt(string file)
    {
        if (file == null || file.Length == 0)
            return "";
        int i = file.LastIndexOf('.');
        if (i < 0) return "";
        return file.Substring(i + 1);
    }

    /// <summary>
    /// 获取文件名称(包含扩展名)
    /// </summary>
    /// <param name="file">文件名称或完整路径</param>
    public static string getFileName(string file)
    {
        return getFileName(file, true);
    }

    /// <summary>
    /// 获取文件名称
    /// </summary>
    /// <param name="file">文件名称或完整路径</param>
    /// <param name="isHaveExt">是否包含有文件扩展名</param>
    public static string getFileName(string file, bool isHaveExt)
    {
        if (file == null || file.Length == 0)
            return "";
        int i = file.LastIndexOf('/');
        if (i < 0)
        {
            if (!isHaveExt)
            {
                int j = file.LastIndexOf('.');
                if (j >= 0)
                    return file.Substring(0, j);
            }
            return file;
        }
        else
        {
            if (isHaveExt)
                return file.Substring(i + 1);
            else
            {
                int j = file.LastIndexOf('.');
                if (j > i)
                    return file.Substring(i + 1, j - i - 1);
                else
                    return file.Substring(i + 1);
            }
        }
    }

    /// <summary>
    /// 删除文件的扩展名
    /// </summary>
    public static string removeFileExt(string file)
    {
        if (file == null || file.Length == 0)
            return "";
        int i = file.LastIndexOf('.');
        if (i < 0)
            return file;
        return file.Substring(0, i);
    }

    /// <summary>
    /// 获取文件路径
    /// </summary>
    /// <param name="file">文件完整路径</param>
    public static string getFilePath(string file)
    {
        if (file == null || file.Length == 0)
            return "";
        return Path.GetDirectoryName(file);
    }

    /// <summary>
    /// 创建文件夹(如果文件夹已经存在,则不再创建)
    /// </summary>
    public static bool createFolder(string path)
    {
        if (path == null || path.Length == 0 || Directory.Exists(path))
            return false;
        Directory.CreateDirectory(path);
        return true;
    }

    /// <summary>
    /// 检测文件夹是否存在
    /// </summary>
    public static bool existsPath(string path)
    {
        if (path == null || path.Length == 0)
            return false;
        else
            return Directory.Exists(path);
    }

    /// <summary>
    /// 检测文件是否存在
    /// </summary>
    public static bool existsFile(string file)
    {
        if (file == null || file.Length == 0)
            return false;
        else
            return File.Exists(file);
    }

    /// <summary>
    /// 删除文件
    /// </summary>
    public static bool deleteFile(string file)
    {
        if (!existsFile(file))
            return true;
        try
        {
            File.Delete(file);
            return true;
        }
        catch (System.Exception e)
        {
            Debug.LogError(e.Message);
            return false;
        }
    }

    /// <summary>
    /// 删除文件夹
    /// <param name="recursive">是否删除所有子文件夹和文件</param>
    /// </summary>
    public static bool deleteFolder(string path, bool recursive)
    {
        if (path == null || path.Length == 0)
            return true;
        try
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            dir.Delete(recursive);
            return true;
        }
        catch (System.Exception e)
        {
            Debug.LogError(e.Message);
            return false;
        }
    }

    /// <summary>
    /// 返回URL形式的streamingAssets地址(自动兼容平台)
    /// </summary>
    public static string streamingAssetsURL
    {
        get
        {
#if UNITY_ANDROID && !UNITY_EDITOR
				return "jar:file://" + Application.dataPath + "!/assets/";
#elif UNITY_IPHONE
				return Application.dataPath + "/Raw/";
#elif UNITY_STANDALONE_WIN || UNITY_EDITOR
            return "file://" + Application.dataPath + "/StreamingAssets/";
#endif
        }
    }

    /// <summary>
    /// 获取当前透视摄像机的视口区域
    /// </summary>
    public static Vector3[] getCameraCorners(float distance)
    {
        Camera theCamera = Camera.main;
        Transform tx = theCamera.transform;
        Vector3[] corners = new Vector3[4];

        float halfFOV = (theCamera.fieldOfView * 0.5f) * Mathf.Deg2Rad;
        float aspect = theCamera.aspect;

        float height = distance * Mathf.Tan(halfFOV);
        float width = height * aspect;

        // UpperLeft
        corners[0] = tx.position - (tx.right * width);
        corners[0] += tx.up * height;
        corners[0] += tx.forward * distance;

        // UpperRight
        corners[1] = tx.position + (tx.right * width);
        corners[1] += tx.up * height;
        corners[1] += tx.forward * distance;

        // LowerLeft
        corners[2] = tx.position - (tx.right * width);
        corners[2] -= tx.up * height;
        corners[2] += tx.forward * distance;

        // LowerRight
        corners[3] = tx.position + (tx.right * width);
        corners[3] -= tx.up * height;
        corners[3] += tx.forward * distance;

        return corners;
    }

    /// <summary>
    /// 获取使用正交相机时,主摄像机在地图上的移动区域。
    /// 使用正交相机时,可以通过旋转摄像机X轴来显示地图,此时通过Y轴控制摄像机高度,X控制水平位置,Z轴控制上下位置。
    /// </summary>
    /// <returns>返回摄像机移动区域.</returns>
    /// <param name="RotationX">摄像机旋转角度(X轴)</param>
    /// <param name="CameraSize">相机大小</param>
    /// <param name="CamersY">摄像机高度(Y轴坐标)</param>
    /// <param name="MapW">地图宽度</param>
    /// <param name="MapH">地图高度</param>
    public static Rect OrthographicCameraEdge(float RotationX, float CameraSize, float CamersY, float MapW, float MapH)
    {
        float CXSize = CameraSize * 2 * ((float)Screen.width / (float)Screen.height);
        float CYSize = CameraSize * 2 / Mathf.Cos(RotationX * Mathf.Deg2Rad);
        return new Rect(
            CXSize / 2.0f,
            CYSize / 2.0f - CamersY,
            MapW - CXSize,
            MapH - CYSize); ;
    }

    /// <summary>
    /// 显示一个提示信息 Toast
    /// </summary>
    public static void Hint(string msg)
    {
#if UNITY_ANDROID || UNITY_ANDROID_API
			AndroidCommon.Hint(msg);
#else
#endif
    }
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值