Esempio n. 1
0
    //Get a PackedFile from a system file path
    static public PackedFile PackFile(string windowsPath)
    {
        if (!File.Exists(windowsPath))
        {
            EditorApplication.Beep();
            Debug.LogError("[TCP2 PackFile] File doesn't exist:" + windowsPath);
            return(null);
        }

        //Get properties
        // Content
        string content = File.ReadAllText(windowsPath, System.Text.Encoding.UTF8);
        // File relative path
        string tcpRoot = TCP2_Utils.FindReadmePath();

        if (tcpRoot == null)
        {
            EditorApplication.Beep();
            Debug.LogError("[TCP2 PackFile] Can't find TCP2 Readme file!\nCan't determine root folder to pack/unpack files.");
            return(null);
        }
        tcpRoot = UnityToSystemPath(tcpRoot);
        string relativePath = windowsPath.Replace(tcpRoot, "");

        PackedFile pf = new PackedFile(relativePath, content);

        return(pf);
    }
Esempio n. 2
0
    private TextAsset[] LoadAllTemplates()
    {
        var list = new List <TextAsset>();

        var systemPath = Application.dataPath + @"/JMO Assets/Toony Colors Pro/Editor/Shader Templates/";

        if (!Directory.Exists(systemPath))
        {
            var rootDir = TCP2_Utils.FindReadmePath();
            systemPath = rootDir.Replace(@"\", "/") + "/Editor/Shader Templates/";
        }

        if (Directory.Exists(systemPath))
        {
            var txtFiles = Directory.GetFiles(systemPath, "*.txt", SearchOption.AllDirectories);

            foreach (var sysPath in txtFiles)
            {
                var unityPath = sysPath;
                if (TCP2_Utils.SystemToUnityPath(ref unityPath))
                {
                    var textAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(unityPath);
                    if (textAsset != null && !list.Contains(textAsset))
                    {
                        list.Add(textAsset);
                    }
                }
            }

            list.Sort((x, y) => x.name.CompareTo(y.name));
            return(list.ToArray());
        }

        return(null);
    }
Esempio n. 3
0
    private static Texture2D GetCustomTexture(string name)
    {
        var uiName = name + (EditorGUIUtility.isProSkin ? "pro" : "");

        if (CustomEditorTextures.ContainsKey(uiName))
        {
            return(CustomEditorTextures[uiName]);
        }

        var rootPath = TCP2_Utils.FindReadmePath(true);

        Texture2D texture = null;

        //load pro version
        if (EditorGUIUtility.isProSkin)
        {
            texture = AssetDatabase.LoadAssetAtPath("Assets" + rootPath + "/Editor/Icons/" + name + "_Pro.png", typeof(Texture2D)) as Texture2D;
        }

        //load default version
        if (texture == null)
        {
            texture = AssetDatabase.LoadAssetAtPath("Assets" + rootPath + "/Editor/Icons/" + name + ".png", typeof(Texture2D)) as Texture2D;
        }

        if (texture != null)
        {
            CustomEditorTextures.Add(uiName, texture);
            return(texture);
        }

        return(null);
    }
Esempio n. 4
0
    private static void UnpackShaders(string filter)
    {
        const string packedShadersGuid = "552f9a41dd13c0c44a9bb1aad0ec3598";
        var          path = AssetDatabase.GUIDToAssetPath(packedShadersGuid);

        if (string.IsNullOrEmpty(path))
        {
            EditorApplication.Beep();
            Debug.LogError("[TCP2 Unpack Shaders] Couldn't find file: \"TCP2 Packed Shaders.tcp2data\"\nPlease reimport Toony Colors Pro 2.");
            return;
        }

        var fullPath = Application.dataPath + path.Substring("Assets".Length);

        if (File.Exists(fullPath))
        {
            var files = TCP2_Utils.ExtractArchive(fullPath, filter);

            var @continue = 0;
            if (files.Length > 8)
            {
                do
                {
                    @continue = EditorUtility.DisplayDialogComplex("TCP2 : Unpack Shaders", "You are about to import " + files.Length + " shaders in Unity.\nIt could take a few minutes!\nContinue?", "Yes", "No", "Help");
                    if (@continue == 2)
                    {
                        TCP2_GUI.OpenHelpFor("Unpack Shaders");
                    }
                }while(@continue == 2);
            }

            if (@continue == 0 && files.Length > 0)
            {
                var tcpRoot = TCP2_Utils.FindReadmePath();
                foreach (var f in files)
                {
                    var filePath = tcpRoot + f.path;
                    var fileDir  = Path.GetDirectoryName(filePath);
                    if (!Directory.Exists(fileDir))
                    {
                        Directory.CreateDirectory(fileDir);
                    }
                    File.WriteAllText(filePath, f.content);
                }

                Debug.Log("Toony Colors Pro 2 - Unpack Shaders:\n" + files.Length + (files.Length > 1 ? " shaders extracted." : " shader extracted."));
                AssetDatabase.Refresh();
            }

            if (files.Length == 0)
            {
                Debug.Log("Toony Colors Pro 2 - Unpack Shaders:\nNothing to unpack. Shaders are probably already unpacked!");
            }
        }
    }
Esempio n. 5
0
    Texture2D TryFindDefaultRampTexture()
    {
        string rootPath = TCP2_Utils.FindReadmePath(true);

        if (!string.IsNullOrEmpty(rootPath))
        {
            string defaultTexPath = "Assets" + rootPath + "/Textures/TCP2_Ramp_3Levels.png";
            return(AssetDatabase.LoadAssetAtPath <Texture2D>(defaultTexPath));
        }

        return(null);
    }
Esempio n. 6
0
    private static void UnpackShaders(string filter)
    {
        var archFiles = Directory.GetFiles(TCP2_Utils.UnityToSystemPath(Application.dataPath), "TCP2 Packed Shaders.tcp2data", SearchOption.AllDirectories);

        if (archFiles == null || archFiles.Length == 0)
        {
            EditorApplication.Beep();
            Debug.LogError("[TCP2 Unpack Shaders] Couldn't find file: \"TCP2 Packed Shaders.tcp2data\"\nPlease reimport Toony Colors Pro 2.");
            return;
        }
        var archivePath = archFiles[0];

        if (archivePath.EndsWith(".tcp2data"))
        {
            var files = TCP2_Utils.ExtractArchive(archivePath, filter);

            var @continue = 0;
            if (files.Length > 8)
            {
                do
                {
                    @continue = EditorUtility.DisplayDialogComplex("TCP2 : Unpack Shaders", "You are about to import " + files.Length + " shaders in Unity.\nIt could take a few minutes!\nContinue?", "Yes", "No", "Help");
                    if (@continue == 2)
                    {
                        TCP2_GUI.OpenHelpFor("Unpack Shaders");
                    }
                }while(@continue == 2);
            }

            if (@continue == 0 && files.Length > 0)
            {
                var tcpRoot = TCP2_Utils.FindReadmePath();
                foreach (var f in files)
                {
                    var filePath = tcpRoot + f.path;
                    var fileDir  = Path.GetDirectoryName(filePath);
                    if (!Directory.Exists(fileDir))
                    {
                        Directory.CreateDirectory(fileDir);
                    }
                    File.WriteAllText(filePath, f.content);
                }

                Debug.Log("Toony Colors Pro 2 - Unpack Shaders:\n" + files.Length + (files.Length > 1 ? " shaders extracted." : " shader extracted."));
                AssetDatabase.Refresh();
            }

            if (files.Length == 0)
            {
                Debug.Log("Toony Colors Pro 2 - Unpack Shaders:\nNothing to unpack. Shaders are probably already unpacked!");
            }
        }
    }
Esempio n. 7
0
    static public void OpenHelp()
    {
        string rootDir = TCP2_Utils.FindReadmePath();

        if (rootDir == null)
        {
            EditorUtility.DisplayDialog("TCP2 Documentation", "Couldn't find TCP2 root folder! (the readme file is missing)\nYou can still access the documentation manually in the Documentation folder.", "Ok");
        }
        else
        {
            string helpLink = "file:///" + rootDir.Replace(@"\", "/") + "/Documentation/TCP2 Documentation.html";
            Application.OpenURL(helpLink);
        }
    }
Esempio n. 8
0
    static public Texture2D GetHelpBoxIcon(MessageType msgType)
    {
        string iconName = null;

        switch (msgType)
        {
        case MessageType.Error:
            if (errorIconCached != null)
            {
                return(errorIconCached);
            }
            iconName = "TCP2_ErrorIcon";
            break;

        case MessageType.Warning:
            if (warningIconCached != null)
            {
                return(warningIconCached);
            }
            iconName = "TCP2_WarningIcon";
            break;

        case MessageType.Info:
            if (infoIconCached != null)
            {
                return(infoIconCached);
            }
            iconName = "TCP2_InfoIcon";
            break;
        }

        if (string.IsNullOrEmpty(iconName))
        {
            return(null);
        }

        string    rootPath = TCP2_Utils.FindReadmePath(true);
        Texture2D icon     = AssetDatabase.LoadAssetAtPath("Assets" + rootPath + "/Editor/Icons/" + iconName + ".png", typeof(Texture2D)) as Texture2D;

        switch (msgType)
        {
        case MessageType.Error: errorIconCached = icon; break;

        case MessageType.Warning: warningIconCached = icon; break;

        case MessageType.Info: infoIconCached = icon; break;
        }

        return(icon);
    }
Esempio n. 9
0
    static public void OpenHelpFor(string helpTopic)
    {
        string rootDir = TCP2_Utils.FindReadmePath();

        if (rootDir == null)
        {
            EditorUtility.DisplayDialog("TCP2 Documentation", "Couldn't find TCP2 root folder! (the readme file is missing)\nYou can still access the documentation manually in the Documentation folder.", "Ok");
        }
        else
        {
            string helpAnchor = helpTopic.Replace("/", "_").Replace(@"\", "_").Replace(" ", "_").ToLowerInvariant() + ".htm";
            string topicLink  = "file:///" + rootDir.Replace(@"\", "/") + "/Documentation/Documentation Data/Anchors/" + helpAnchor;
            Application.OpenURL(topicLink);
        }
    }
Esempio n. 10
0
    //Extract an archive into an array of PackedFile
    static public PackedFile[] ExtractArchive(string archivePath, string filter = null)
    {
        string archive = File.ReadAllText(archivePath);

        string[] archiveLines = File.ReadAllLines(archivePath);

        if (archiveLines[0] != "# TCP2 PACKED SHADERS")
        {
            EditorApplication.Beep();
            Debug.LogError("[TCP2 ExtractArchive] Invalid TCP2 archive:\n" + archivePath);
            return(null);
        }

        //Find offset
        int offset = archive.IndexOf("###") + 4;

        if (offset < 20)
        {
            Debug.LogError("[TCP2 ExtractArchive] Invalid TCP2 archive:\n" + archivePath);
            return(null);
        }

        string            tcpRoot         = TCP2_Utils.FindReadmePath();
        List <PackedFile> packedFilesList = new List <PackedFile>();

        for (int line = 1; line < archiveLines.Length; line++)
        {
            //Index end, start content parsing
            if (archiveLines[line].StartsWith("#"))
            {
                break;
            }

            string[] shaderIndex = archiveLines[line].Split(new string[] { ";" }, System.StringSplitOptions.RemoveEmptyEntries);
            if (shaderIndex.Length != 3)
            {
                EditorApplication.Beep();
                Debug.LogError("[TCP2 ExtractArchive] Invalid format in TCP2 archive, at line " + line + ":\n" + archivePath);
                return(null);
            }

            //Get data
            string relativePath = shaderIndex[0];
            int    start        = int.Parse(shaderIndex[1]);
            int    length       = int.Parse(shaderIndex[2]);
            //Get content
            string content = archive.Substring(offset + start, length);

            //Skip if file already extracted
            if (File.Exists(tcpRoot + relativePath))
            {
                continue;
            }

            //Filter?
            if (!string.IsNullOrEmpty(filter))
            {
                string[] filters = filter.Split(new string[] { " " }, System.StringSplitOptions.RemoveEmptyEntries);
                bool     skip    = false;
                foreach (string f in filters)
                {
                    if (!relativePath.ToLower().Contains(f.ToLower()))
                    {
                        skip = true;
                        break;
                    }
                }
                if (skip)
                {
                    continue;
                }
            }

            //Add File
            packedFilesList.Add(new PackedFile(relativePath, content));
        }

        return(packedFilesList.ToArray());
    }
    //--------------------------------------------------------------------------------------------------

    private Mesh CreateSmoothedMeshAsset(SelectedMesh originalMesh)
    {
        //Check if we are ok to overwrite
        bool   overwrite = true;
        string rootPath  = TCP2_Utils.FindReadmePath() + OUTPUT_FOLDER;

        if (!System.IO.Directory.Exists(rootPath))
        {
            System.IO.Directory.CreateDirectory(rootPath);
        }

#if UNITY_EDITOR_WIN
        rootPath = rootPath.Replace(TCP2_Utils.UnityToSystemPath(Application.dataPath), "").Replace(@"\", "/");
#else
        rootPath = rootPath.Replace(Application.dataPath, "");
#endif

        string assetPath    = "Assets" + rootPath;
        string newAssetName = originalMesh.name + " " + MESH_SUFFIX + ".asset";
        if (originalMesh.name.Contains(MESH_SUFFIX))
        {
            newAssetName = originalMesh.name + ".asset";
        }
        assetPath += newAssetName;
        Mesh existingAsset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Mesh)) as Mesh;
        bool assetExists   = (existingAsset != null) && originalMesh.isAsset;
        if (assetExists)
        {
            if (!mAlwaysOverwrite)
            {
                overwrite = EditorUtility.DisplayDialog("TCP2 : Smoothed Mesh", "The following smoothed mesh already exists:\n\n" + newAssetName + "\n\nOverwrite?", "Yes", "No");
            }

            if (!overwrite)
            {
                return(null);
            }
            else
            {
                originalMesh.mesh = existingAsset;
                originalMesh.name = existingAsset.name;
            }
        }

        Mesh newMesh = null;
        if (originalMesh.isSkinned)
        {
            newMesh = TCP2_Utils.CreateSmoothedMesh(originalMesh.mesh, mFormat, false, true, false, !originalMesh.isAsset || (originalMesh.isAsset && assetExists));
        }
        else
        {
            newMesh = TCP2_Utils.CreateSmoothedMesh(originalMesh.mesh, mFormat, mVColors, mTangents, mUV2, !originalMesh.isAsset || (originalMesh.isAsset && assetExists));
        }

        if (newMesh == null)
        {
            ShowNotification(new GUIContent("Couldn't generate the mesh for:\n" + originalMesh.name));
        }
        else
        {
            if (originalMesh.associatedObjects != null)
            {
                Undo.RecordObjects(originalMesh.associatedObjects, "Assign TCP2 Smoothed Mesh to Selection");

                foreach (Object o in originalMesh.associatedObjects)
                {
                    if (o is SkinnedMeshRenderer)
                    {
                        (o as SkinnedMeshRenderer).sharedMesh = newMesh;
                    }
                    else if (o is MeshFilter)
                    {
                        (o as MeshFilter).sharedMesh = newMesh;
                    }
                    else
                    {
                        Debug.LogWarning("[TCP2 Smoothed Normals Utility] Unrecognized AssociatedObject: " + o + "\nType: " + o.GetType());
                    }
                    EditorUtility.SetDirty(o);
                }
            }

            if (originalMesh.isAsset)
            {
                if (overwrite && !assetExists)
                {
                    AssetDatabase.CreateAsset(newMesh, assetPath);
                }
            }
            else
            {
                return(null);
            }
        }

        return(newMesh);
    }