小技巧

获取物体

获取root底部所有的子物体()包含不可见的):GetComponentsInChildren(true);

获取root底部所有的子物体(只是可见的):GetComponentsInChildren();

Unity打开文件夹

string floader = EditorUtility.OpenFolderPanel("请选择文件夹", "", ""); 
if (!string.IsNullOrEmpty(floader)) { Debug.Log(floader); }

获取文件编码格式

/// 
    /// 获取文件编码格式
    /// 
    /// 
    /// 
    private static Encoding GetEncoding(string filename)
    {
        // Read the BOM
        var bom = new byte[4];
        using (var file = new FileStream(filename, FileMode.Open, FileAccess.Read))
        {
            file.Read(bom, 0, 4);
        }
        // Analyze the BOM
        if (bom[0] == 0x2b && bom[1] == 0x2f && bom[2] == 0x76) return Encoding.UTF7;
        if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) return Encoding.UTF8;
        if (bom[0] == 0xff && bom[1] == 0xfe) return Encoding.Unicode; //UTF-16LE
        if (bom[0] == 0xfe && bom[1] == 0xff) return Encoding.BigEndianUnicode; //UTF-16BE
        if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff) return Encoding.UTF32;
        return Encoding.ASCII;
    }

调用外部程序

System.Diagnostics.Process.Start("notepad.exe"); -- 打开记事本
System.Diagnostics.Process.Start("calc.exe "); -- 打开计算器
System.Diagnostics.Process.Start("regedit.exe "); -- 打开注册表
System.Diagnostics.Process.Start("mspaint.exe "); -- 打开画图板
System.Diagnostics.Process.Start("write.exe "); -- 打开写字板
System.Diagnostics.Process.Start("mplayer2.exe "); --打开播放器
System.Diagnostics.Process.Start("taskmgr.exe "); --打开任务管理器
System.Diagnostics.Process.Start("eventvwr.exe "); --打开事件查看器
System.Diagnostics.Process.Start("winmsd.exe "); --打开系统信息
System.Diagnostics.Process.Start("winver.exe "); --打开Windows版本信息
System.Diagnostics.Process.Start("mailto: "+ address); -- 发邮件

eg:

            #if UNITY_STANDALONE_WIN
            string _path = "file://" + Application.dataPath + @"/StreamingAssets/";
            System.Diagnostics.Process.Start("explorer", "/n, " + _path + "word" + ".doc");
            #endif

文件解压缩

帮助类

/*
 * Filename: ZipHelper.cs
 * Created Date: 2020年08月28日 09:34:28 星期五
 * Author: zhlong
 * 
 * Copyright (c) 2019 Xuzhou LingTe Technology CO.,LTD
 */
using System;
using ICSharpCode.SharpZipLib.Zip;//ZipOutputStream
using System.IO;//FileMode


namespace Zip
{
    public class ZipHelper
    {

        public static void ZipFile(string path,string fileName,string ZipName,string pass)
        {
            ZipFile(path+"//",new string[] { fileName}, path + "//"+ZipName,7,pass,"啦啦啦,我是一个压缩文件");
        }
        #region 文件解压缩
        ///   
        /// 压缩指定文件生成ZIP文件  
        ///   
        /// 顶层文件夹名称  
        /// 待压缩文件列表  
        /// ZIP文件  
        /// 压缩比  
        /// 密码  
        /// 压缩文件注释文字  
        public static void ZipFile
            (
            string topDirName,  //顶层文件夹名称 \Storage Card\PDADataExchange\send\xml\
            string[] fileNamesToZip,  //待压缩文件列表  version.xml
            string ZipedFileName,   //ZIP文件  \Storage Card\PDADataExchange\send\zip\version.zip
            int CompressionLevel,    //压缩比  7
            string password,    //密码   ""
            string comment   //压缩文件注释文字  ""
            )
        {
            ZipOutputStream s = new ZipOutputStream(System.IO.File.Open(ZipedFileName, FileMode.Create));

            if (password != null && password.Length > 0)
                s.Password = password;

            if (comment != null && comment.Length > 0)
                s.SetComment(comment);

            s.SetLevel(CompressionLevel); // 0 - means store only to 9 - means best compression  

            foreach (string file in fileNamesToZip)
            {
                FileStream fs = File.OpenRead(topDirName + file);    //打开待压缩文件  
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);      //读取文件流  
                ZipEntry entry = new ZipEntry(file);    //新建实例  

                entry.DateTime = DateTime.Now;

                entry.Size = fs.Length;
                fs.Close();

                s.PutNextEntry(entry);
                s.Write(buffer, 0, buffer.Length);
            }
            s.Finish();
            s.Close();
        }
        ///   
        /// 解压缩ZIP文件到指定文件夹  ---PC端
        ///   
        /// ZIP文件  
        /// 解压文件夹  
        /// 压缩文件密码  
        public static void UnZipFile(string zipfileName, string UnZipDir, string password)
        {

            //zipfileName=@"\Storage Card\PDADataExchange\receive\zip\test.zip";
            //UnZipDir= @"\Storage Card\PDADataExchange\receive\xml\";
            //password="";

            ZipInputStream s = new ZipInputStream(File.OpenRead(zipfileName));
            if (password != null && password.Length > 0)
                s.Password = password;
            try
            {
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(UnZipDir);
                    string pathname = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);

                    //生成解压目录   
                    pathname = pathname.Replace(":", "$");//处理压缩时带有盘符的问题  
                    directoryName = directoryName + "\\" + pathname;
                    Directory.CreateDirectory(directoryName);

                    if (fileName != String.Empty)
                    {
                        //解压文件到指定的目录  
                        FileStream streamWriter = File.Create(directoryName + "\\" + fileName);

                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        streamWriter.Close();
                    }
                }
                s.Close();
            }
            catch (Exception eu)
            {
                throw eu;
            }
            finally
            {
                s.Close();
            }

        }



        ///   
        /// 解压缩ZIP文件到指定文件夹  ---安卓端
        ///   
        /// ZIP文件  
        /// 解压文件夹  
        /// 压缩文件密码  
        public static void UnZipFile_Android(string zipfileName, string UnZipDir, string password)
        {

            //zipfileName=@"\Storage Card\PDADataExchange\receive\zip\test.zip";
            //UnZipDir= @"\Storage Card\PDADataExchange\receive\xml\";
            //password="";

            ZipInputStream s = new ZipInputStream(File.OpenRead(zipfileName));
            if (password != null && password.Length > 0)
                s.Password = password;
            try
            {
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = UnZipDir;
                    string pathname = Path.GetDirectoryName(theEntry.Name);
                    string fileName = Path.GetFileName(theEntry.Name);

                    //生成解压目录   
                    pathname = pathname.Replace(":", "$");//处理压缩时带有盘符的问题  
                    directoryName = directoryName + pathname;
                    Directory.CreateDirectory(directoryName);

                    if (fileName != String.Empty)
                    {
                        //解压文件到指定的目录  
                        FileStream streamWriter = File.Create(directoryName + fileName);

                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        streamWriter.Close();
                    }
                }
                s.Close();
            }
            catch (Exception eu)
            {
                throw eu;
            }
            finally
            {
                s.Close();
            }

        }
        #endregion

        #region 工具
        /// 
        /// 根据文件路径返回文件名称
        /// 
        /// 
        /// 
        public static string GetPathToFileName(string Path)
        {
            Path = Path.Replace("\\","/");
            int index = Path.LastIndexOf("/");
            string name = Path.Substring(index+1);
            return name;
        }
        #endregion
    }
}

使用教程

private string rarPath;

        private void Awake()
        {
            uimanager.AddPanel(new UiTips(GameObject.Find("Canvas").transform));
        }
        private void Start()
        {
            rarPath = Application.persistentDataPath + "/data/";
            if (!Directory.Exists(rarPath)) {
                Directory.CreateDirectory(rarPath);
            }
            Zip.ZipHelper.UnZipFile_Android("/storage/emulated/0/Res/1.zip", rarPath, "110");
            string str = File.ReadAllText(rarPath+"1.txt");
            File.Delete(rarPath+"1.txt");
            Debug.LogError(str);

            uimanager.GetPanel().Open(new UiTips.Model(str));
        }

最近做一个项目,最开始是在PC端播放视频。

采用的是很大众的UGUI内播放MP4的形式。

主要是创建RawImage,

在上面添加VideoPlayer组件,然后添加代码:

//如果videoPlayer没有对应的视频texture,则返回if (videoPlayer.texture == null){return;}

//把VideoPlayerd的视频渲染到UGUI的RawImagerawImage.texture = videoPlayer.texture;然后在Update中调用此函数,但是导出到端的时候无法播放视频,网上很多人推荐用插件,后来查了很久,发现只需要设置两个参数就可以,完全不用做别的内容。如图:

小技巧_第1张图片
image

Device Filter的值设置成ARMv7,然后取消Android TV Compatibility就可以了,在Unity2017亲测有效

你可能感兴趣的:(小技巧)