Beispiel #1
0
        private void InspectLoadSettings()
        {
            PDFViewer viewer = (PDFViewer)target;

            GUILayout.BeginVertical("Box");

            if (EnterGroup("Load Settings", "Paroxe.PdfRenderer.PDFViewer.ShowLoadSettings"))
            {
                EditorGUILayout.PropertyField(m_FileSource);

                PDFViewer.FileSourceType fileSourceType = (PDFViewer.FileSourceType)m_FileSource.enumValueIndex;

                if (fileSourceType == PDFViewer.FileSourceType.Resources ||
                    fileSourceType == PDFViewer.FileSourceType.StreamingAssets
                    )
                {
                    EditorGUI.indentLevel++;

                    EditorGUILayout.PropertyField(m_Folder);
                    EditorGUILayout.PropertyField(m_FileName);

                    if (fileSourceType == PDFViewer.FileSourceType.Resources)
                    {
                        if (File.Exists(Application.dataPath + "/Resources/" + viewer.GetFileLocation()) &&
                            !File.Exists(Application.dataPath + "/Resources/" + viewer.GetFileLocation().Replace(".bytes", "") + ".bytes"))
                        {
                            EditorGUILayout.HelpBox(
                                "PDF file in resources folder need to have .bytes extension to allow PDFViewer to access it correctly. \n\r    For example => pdf_sample.pdf.bytes",
                                MessageType.Warning);
                        }
                    }

                    EditorGUI.indentLevel--;
                }
                else if (fileSourceType == PDFViewer.FileSourceType.Web)
                {
                    EditorGUI.indentLevel++;
                    EditorGUILayout.PropertyField(m_FileURL, new GUIContent("Url"));
                    EditorGUI.indentLevel--;
                }
                else if (fileSourceType == PDFViewer.FileSourceType.FilePath)
                {
                    EditorGUI.indentLevel++;
                    EditorGUILayout.PropertyField(m_FilePath);
                    EditorGUI.indentLevel--;
                }

                if (fileSourceType != PDFViewer.FileSourceType.Bytes && fileSourceType != PDFViewer.FileSourceType.None)
                {
#if UNITY_IOS
                    if (fileSourceType == PDFViewer.FileSourceType.Web)
                    {
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.PrefixLabel(" ");
                        EditorGUILayout.HelpBox(
                            "You may need to add NSAppTransportSecurity entry in info.plist of the XCode project to allow PDFViewer to download pdf from web:\n\r\n\r" +
                            "<key>NSAppTransportSecurity</key>\n\r" +
                            "    <dict>\n\r" +
                            "    <key>NSAllowsArbitraryLoads</key>\n\r" +
                            "        <true/>\n\r" +
                            "</dict>", MessageType.Info);
                        EditorGUILayout.EndHorizontal();
                    }
#endif
                    if (fileSourceType == PDFViewer.FileSourceType.StreamingAssets ||
                        fileSourceType == PDFViewer.FileSourceType.Resources ||
                        fileSourceType == PDFViewer.FileSourceType.FilePath)
                    {
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.PrefixLabel(" ");

                        if (GUILayout.Button("Browse"))
                        {
                            string baseDirectory = "";

                            if (fileSourceType == PDFViewer.FileSourceType.StreamingAssets)
                            {
                                baseDirectory = Application.streamingAssetsPath;
                            }
                            else if (fileSourceType == PDFViewer.FileSourceType.Resources)
                            {
                                baseDirectory = "Assets/Resources";
                            }
                            else if (fileSourceType == PDFViewer.FileSourceType.FilePath)
                            {
                                string projectRoot = System.IO.Path.GetFullPath(System.IO.Path.Combine(Application.dataPath, ".."));
                                projectRoot = projectRoot.Replace('\\', '/');

                                baseDirectory = projectRoot;
                            }

                            if (!Directory.Exists(baseDirectory))
                            {
                                Directory.CreateDirectory(baseDirectory);
                                AssetDatabase.Refresh();
                            }

                            string folder             = "";
                            string fileName           = "";
                            string filePath           = "";
                            bool   usePersistentData  = false;
                            bool   useStreamingAssets = false;
                            bool   useResources       = false;
                            bool   useFilePath        = false;

                            if (Browse(baseDirectory, ref fileName, ref folder, ref filePath, ref useStreamingAssets, ref usePersistentData, ref useResources, ref useFilePath))
                            {
                                if (useStreamingAssets)
                                {
                                    fileSourceType = PDFViewer.FileSourceType.StreamingAssets;
                                }
                                else if (useResources)
                                {
                                    fileSourceType = PDFViewer.FileSourceType.Resources;
                                }
                                else if (useFilePath)
                                {
                                    fileSourceType = PDFViewer.FileSourceType.FilePath;
                                }

                                if (fileSourceType != PDFViewer.FileSourceType.FilePath)
                                {
                                    m_Folder.stringValue   = folder;
                                    m_FileName.stringValue = fileName;
                                }
                                else
                                {
                                    m_FilePath.stringValue = filePath;
                                }
                            }
                        }

                        EditorGUILayout.EndHorizontal();
                    }

#if UNITY_EDITOR_WIN
                    if (fileSourceType != PDFViewer.FileSourceType.Asset && fileSourceType != PDFViewer.FileSourceType.DocumentObject)
                    {
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.PrefixLabel(" ");

                        using (new EditorGUI.DisabledGroupScope(string.IsNullOrEmpty(m_FileName.stringValue.Trim())))
                        {
                            if (GUILayout.Button("Reveal File"))
                            {
                                string filePath = "";

                                if (fileSourceType == PDFViewer.FileSourceType.Resources)
                                {
                                    filePath = Application.dataPath + "/Resources/" + viewer.GetFileLocation();
                                }
                                else
                                {
                                    filePath = viewer.GetFileLocation();
                                }

                                if (fileSourceType != PDFViewer.FileSourceType.Web)
                                {
                                    if (!File.Exists(filePath))
                                    {
                                        if (fileSourceType == PDFViewer.FileSourceType.Resources &&
                                            File.Exists(filePath + ".bytes"))
                                        {
                                            ShowInExplorer(filePath + ".bytes");
                                        }
                                        else
                                        {
                                            EditorUtility.DisplayDialog("Error",
                                                                        "The file path is badly formed, contains invalid characters or doesn't exists:\r\n\r\n" +
                                                                        filePath, "Ok");
                                        }
                                    }
                                    else
                                    {
                                        ShowInExplorer(filePath);
                                    }
                                }
                                else
                                {
                                    try
                                    {
                                        System.Diagnostics.Process.Start(filePath);
                                    }
                                    catch
                                    {
                                        EditorUtility.DisplayDialog("Error",
                                                                    "The URL is badly formed or contains invalid characters:\r\n\r\n" + filePath, "Ok");
                                    }
                                }
                            }
                        }

                        EditorGUILayout.EndHorizontal();
                    }
#endif
                }

                if (fileSourceType == PDFViewer.FileSourceType.Bytes)
                {
                    EditorGUI.indentLevel++;
                    GameObject oldObject = (GameObject)m_BytesSupplierObject.objectReferenceValue;
                    EditorGUILayout.PropertyField(m_BytesSupplierObject, new GUIContent("Supplier Object"));

                    int selectedIndex = 0;

                    if (m_BytesSupplierObject.objectReferenceValue != null)
                    {
                        try
                        {
                            List <BytesSupplierInfo> possibleSuppliers = new List <BytesSupplierInfo>();
                            possibleSuppliers.Add(new BytesSupplierInfo(null, null, ""));

                            Component[] components = ((GameObject)m_BytesSupplierObject.objectReferenceValue).GetComponents(typeof(Component));

                            foreach (Component component in components)
                            {
                                Type         type    = component.GetType();
                                MethodInfo[] methods = type.GetMethods();

                                if (methods.Length == 0)
                                {
                                    continue;
                                }

                                foreach (MethodInfo method in methods)
                                {
                                    if ((method.GetParameters() == null || method.GetParameters().Length == 0) &&
                                        method.ReturnType == typeof(byte[]))
                                    {
                                        possibleSuppliers.Add(new BytesSupplierInfo(m_BytesSupplierObject.objectReferenceValue as GameObject, component,
                                                                                    method.Name));

                                        if (oldObject == m_BytesSupplierObject.objectReferenceValue &&
                                            method.Name == m_BytesSupplierFunctionName.stringValue &&
                                            component == m_BytesSupplierComponent.objectReferenceValue)
                                        {
                                            selectedIndex = possibleSuppliers.Count - 1;
                                        }
                                    }
                                }
                            }

                            string[] supplierTitles = new string[possibleSuppliers.Count];

                            for (int i = 0; i < supplierTitles.Length; ++i)
                            {
                                if (i == 0)
                                {
                                    supplierTitles[0] = "No Function";
                                    continue;
                                }
                                supplierTitles[i] = possibleSuppliers[i].m_Behaviour.name + "." +
                                                    possibleSuppliers[i].m_MethodName;
                            }

                            EditorGUILayout.BeginHorizontal();
                            EditorGUILayout.PrefixLabel("Supplier Function");

                            var choiceIndex = EditorGUILayout.Popup(selectedIndex, supplierTitles);

                            if (choiceIndex == 0)
                            {
                                m_BytesSupplierComponent.objectReferenceValue = null;
                                m_BytesSupplierFunctionName.stringValue       = "";
                            }
                            else
                            {
                                m_BytesSupplierComponent.objectReferenceValue = possibleSuppliers[choiceIndex].m_Behaviour;
                                m_BytesSupplierFunctionName.stringValue       = possibleSuppliers[choiceIndex].m_MethodName;
                            }

                            EditorGUILayout.EndHorizontal();
                        }
                        catch (Exception)
                        {
                            m_BytesSupplierComponent.objectReferenceValue = null;
                            m_BytesSupplierFunctionName.stringValue       = "";
                        }
                    }

                    EditorGUI.indentLevel--;
                }

                if (fileSourceType == PDFViewer.FileSourceType.Asset)
                {
                    EditorGUILayout.PropertyField(m_PDFAsset);
                }

                EditorGUILayout.PropertyField(m_LoadOnEnable);
                EditorGUILayout.PropertyField(m_UnloadOnDisable);

                if (fileSourceType == PDFViewer.FileSourceType.Asset)
                {
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.PrefixLabel("");
                    EditorGUILayout.HelpBox("To convert pdf file to .asset right click on pdf and select \"PDF Renderer\\Convert to .asset\"", MessageType.Info);
                    EditorGUILayout.EndHorizontal();
                }
                GUILayout.Space(4.0f);
            }
            GUILayout.EndVertical();
        }