Пример #1
0
        /// <summary>
        /// Request to load a file asynchronously.
        /// </summary>
        /// <param name="objName"></param>
        /// <param name="filePath"></param>
        /// <param name="texNames"></param>
        /// <param name="parentObj"></param>
        /// <param name="options"></param>
        public void ImportModelAsync(Entry entry, string objName, string filePath, List <string> texNames, Transform parentObj, ImportOptions options)
        {
            this.entry    = entry;
            this.objName  = objName;
            this.texNames = texNames;

            if (loaderList == null)
            {
                loaderList = new List <Loader>();
            }
            if (loaderList.Count == 0)
            {
                numTotalImports = 0;// files.Length;
                if (ImportingStart != null)
                {
                    ImportingStart();
                }
            }
            string absolutePath = filePath.Contains("//") ? filePath : Path.GetFullPath(filePath);

            absolutePath = absolutePath.Replace('\\', '/');
            Loader loader = CreateLoader(absolutePath);

            if (loader == null)
            {
                return;
            }
            numTotalImports++;
            loaderList.Add(loader);
            loader.buildOptions = options;
            if (string.IsNullOrEmpty(objName))
            {
                objName = Path.GetFileNameWithoutExtension(absolutePath);
            }
            allLoaded = false;
            StartCoroutine(loader.Load(objName, absolutePath, parentObj));
        }
Пример #2
0
 /// <summary>
 /// Start importing a new object
 /// </summary>
 /// <see cref="ImportOptions"/>
 /// <param name="absolutePath">Absolute path of the file to import</param>
 /// <param name="parentObject">Transform to which attach the new object (it can be null)</param>
 /// <param name="options">Import options</param>
 public void ImportFile(string absolutePath, Transform parentObject, ImportOptions options)
 {
     buildOptions = options;
     StartCoroutine(ImportFileAsync(absolutePath, parentObject));
 }
Пример #3
0
        private void OnGUI()
        {
            EditorGUILayout.BeginHorizontal();
            defaultImportPath = EditorGUILayout.TextField("Default import path:", defaultImportPath);
            if (GUILayout.Button("...", GUILayout.Width(24), GUILayout.Height(15)))
            {
                defaultImportPath = EditorUtility.OpenFolderPanel("Select a folder", defaultImportPath, "");
                defaultImportPath = defaultImportPath.Replace('\\', '/');
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            lastPath = EditorGUILayout.TextField("OBJ file path:", lastPath);
            if (!string.IsNullOrEmpty(lastPath))
            {
                if (GUILayout.Button("^", GUILayout.Width(24), GUILayout.Height(15)))
                {
                    FileInfo fileInfo = new FileInfo(lastPath);
                    defaultImportPath = fileInfo.Directory.FullName;
                    defaultImportPath = defaultImportPath.Replace('\\', '/');
                }
            }
            if (GUILayout.Button("...", GUILayout.Width(24), GUILayout.Height(15)))
            {
                string[] filters      = { "OBJ files", "obj", "All files", "*" };
                string   absolutePath = EditorUtility.OpenFilePanelWithFilters("Laod OBJ model", defaultImportPath, filters);
                if (!string.IsNullOrEmpty(absolutePath))
                {
                    FileInfo fileInfo = new FileInfo(absolutePath);
                    Debug.Log(fileInfo.Directory.FullName);
                    //EditorPrefs.SetString("OBJ Import path", fileInfo.Directory.FullName);
                    lastPath = absolutePath;
                }
            }
            EditorGUILayout.EndHorizontal();

            scale                = EditorGUILayout.FloatField("Scale:", scale);
            convertUpAxis        = EditorGUILayout.Toggle("Convert vertical axis", convertUpAxis);
            convertToDoubleSided = EditorGUILayout.Toggle("Convert to double-sided (duplicate&flip polygons)", convertToDoubleSided);
            litDiffuseMap        = EditorGUILayout.Toggle("Lit diffuse map", litDiffuseMap);
            importAssets         = EditorGUILayout.Toggle("Import assets", importAssets);
            if (importAssets)
            {
                importAssetPath = EditorGUILayout.TextField("OBJ asset path:", importAssetPath);
                importAssetPath = importAssetPath.Replace('\\', '/');
            }
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Reset settings", GUILayout.Width(100), GUILayout.Height(24)))
            {
                ResetSettings();
            }
            if (GUILayout.Button("Save settings", GUILayout.Width(100), GUILayout.Height(24)))
            {
                SaveSettings();
            }
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Import", GUILayout.Width(80), GUILayout.Height(24)))
            {
                lastPath = lastPath.Replace('\\', '/');
                string absolute_path = lastPath;
                if (!string.IsNullOrEmpty(absolute_path))
                {
                    objObject                   = new GameObject();
                    objObject.name              = "OBJ Loader";
                    objImporter                 = objObject.AddComponent <ObjectImporter>();
                    objImporter.importAssets    = importAssets;
                    objImporter.importAssetPath = importAssetPath;

                    //string absolute_path = @"E:\DEV\PROTO\DATA\Models\teleportbeam.obj";
                    //absolute_path = absolute_path.Replace('\\', '/');
                    GameObject parentObject = Selection.activeObject as GameObject;
                    if (parentObject && !parentObject.activeInHierarchy)
                    {
                        parentObject = null;
                    }
                    ImportOptions opt = new ImportOptions();
                    opt.zUp                  = convertUpAxis;
                    opt.litDiffuse           = litDiffuseMap;
                    opt.convertToDoubleSided = convertToDoubleSided;
                    opt.modelScaling         = scale;
                    objImporter.ImportFile(absolute_path, parentObject ? parentObject.transform : null, opt);
                    //ObjLoader loader = new ObjLoader();
                    //loader.zUpToYUp = false;
                    //loader.scaling = Vector3.one * 10f;
                    //Debug.Log("Loading OBJ: " + absolute_path);
                    //loader.Load("obj_import_test", absolute_path, go);
                    loading = true;
                }
            }
            if (GUILayout.Button("Cancel", GUILayout.Width(80), GUILayout.Height(24)))
            {
                EditorUtility.ClearProgressBar();
                if (loading)
                {
                    if (objObject)
                    {
                        DestroyImmediate(objObject);
                    }
                    loading = false;
                }
                Close();
            }
            EditorGUILayout.EndHorizontal();
        }
Пример #4
0
        private void OnGUI()
        {
            EditorGUILayout.BeginHorizontal();
            defaultImportPath = EditorGUILayout.TextField("Default import path:", defaultImportPath);
            if (GUILayout.Button("...", GUILayout.Width(24), GUILayout.Height(15)))
            {
                defaultImportPath = EditorUtility.OpenFolderPanel("Select a folder", defaultImportPath, "");
                defaultImportPath = defaultImportPath.Replace('\\', '/');
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            lastPath = EditorGUILayout.TextField("OBJ file path:", lastPath);
            if (!string.IsNullOrEmpty(lastPath))
            {
                if (GUILayout.Button("^", GUILayout.Width(24), GUILayout.Height(15)))
                {
                    FileInfo fileInfo = new FileInfo(lastPath);
                    defaultImportPath = fileInfo.Directory.FullName;
                    defaultImportPath = defaultImportPath.Replace('\\', '/');
                }
            }
            if (GUILayout.Button("...", GUILayout.Width(24), GUILayout.Height(15)))
            {
                string[] filters      = { "OBJ files", "obj", "All files", "*" };
                string   absolutePath = EditorUtility.OpenFilePanelWithFilters("Laod OBJ model", defaultImportPath, filters);
                if (!string.IsNullOrEmpty(absolutePath))
                {
                    FileInfo fileInfo = new FileInfo(absolutePath);
                    Debug.Log(fileInfo.Directory.FullName);
                    //EditorPrefs.SetString("OBJ Import path", fileInfo.Directory.FullName);
                    lastPath = absolutePath;
                }
            }
            EditorGUILayout.EndHorizontal();

            scale                = EditorGUILayout.FloatField("Scale:", scale);
            convertUpAxis        = EditorGUILayout.Toggle("Convert vertical axis", convertUpAxis);
            convertToDoubleSided = EditorGUILayout.Toggle("Convert to double-sided (duplicate&flip polygons)", convertToDoubleSided);
            litDiffuseMap        = EditorGUILayout.Toggle("Lit diffuse map", litDiffuseMap);
            buildColliders       = EditorGUILayout.Toggle("Generate mesh colliders", buildColliders);
            if (buildColliders)
            {
                colliderConvex = EditorGUILayout.Toggle("Convex mesh colliders", colliderConvex);
                if (colliderConvex)
                {
                    EditorGUILayout.HelpBox(
                        "Building convex meshes may not work for meshes with too many smooth surface regions.\n" +
                        "If you get errors find each involved object and fix its mesh collider (e.g. remove it or uncheck \"Convex\").",
                        MessageType.Warning);
                    colliderTrigger = EditorGUILayout.Toggle("Mesh colliders as trigger", colliderTrigger);
#if !UNITY_2018_3_OR_NEWER
                    colliderInflate   = EditorGUILayout.Toggle("Mesh colliders inflated", colliderInflate);
                    colliderSkinWidth = EditorGUILayout.FloatField("Mesh colliders inflation amount", colliderSkinWidth);
#endif
                }
            }
#if UNITY_2017_3_OR_NEWER
            use32bitIndices = EditorGUILayout.Toggle("Use 32 bit indices", use32bitIndices);
#endif
            hideWhileLoading = EditorGUILayout.Toggle("Hide while loading", hideWhileLoading);

            importAssets = EditorGUILayout.Toggle("Import assets", importAssets);
            if (importAssets)
            {
                importAssetPath = EditorGUILayout.TextField("OBJ asset path:", importAssetPath);
                importAssetPath = importAssetPath.Replace('\\', '/');
            }

            EditorGUILayout.Separator();

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Reset settings", GUILayout.Width(100), GUILayout.Height(24)))
            {
                ResetSettings();
            }
            if (GUILayout.Button("Save settings", GUILayout.Width(100), GUILayout.Height(24)))
            {
                SaveSettings();
            }
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Import", GUILayout.Width(80), GUILayout.Height(24)))
            {
                lastPath = lastPath.Replace('\\', '/');
                string absolute_path = lastPath;
                if (!string.IsNullOrEmpty(absolute_path))
                {
                    objObject                   = new GameObject();
                    objObject.name              = "OBJ Loader";
                    objImporter                 = objObject.AddComponent <ObjectImporter>();
                    objImporter.importAssets    = importAssets;
                    objImporter.importAssetPath = importAssetPath;

                    GameObject parentObject = Selection.activeObject as GameObject;
                    if (parentObject && !parentObject.activeInHierarchy)
                    {
                        parentObject = null;
                    }
                    ImportOptions opt = new ImportOptions();
                    opt.zUp                  = convertUpAxis;
                    opt.litDiffuse           = litDiffuseMap;
                    opt.convertToDoubleSided = convertToDoubleSided;
                    opt.modelScaling         = scale;
                    opt.buildColliders       = buildColliders;
                    opt.colliderTrigger      = colliderTrigger;
                    opt.colliderConvex       = colliderConvex;
#if !UNITY_2018_3_OR_NEWER
                    opt.colliderInflate   = colliderInflate;
                    opt.colliderSkinWidth = colliderSkinWidth;
#endif
                    opt.hideWhileLoading = hideWhileLoading;
                    objImporter.ImportFile(absolute_path, parentObject ? parentObject.transform : null, opt);
                    loading = true;
                }
            }
            if (GUILayout.Button("Cancel", GUILayout.Width(80), GUILayout.Height(24)))
            {
                EditorUtility.ClearProgressBar();
                if (loading)
                {
                    if (objObject)
                    {
                        DestroyImmediate(objObject);
                    }
                    loading = false;
                }
                Close();
            }
            EditorGUILayout.EndHorizontal();
        }
Пример #5
0
        /// <summary>
        /// Request to load a file asynchronously.
        /// </summary>
        /// <param name="objName"></param>
        /// <param name="filePath"></param>
        /// <param name="parentObj"></param>
        /// <param name="options"></param>
        public bool ImportModelAsync(string objName, string filePath, Transform parentObj, ImportOptions options, out string error)
        {
            if (loaderList == null)
            {
                loaderList = new List <Loader>();
            }
            if (loaderList.Count == 0)
            {
                numTotalImports = 0;// files.Length;
                if (ImportingStart != null)
                {
                    ImportingStart();
                }
            }
            string absolutePath = filePath.Contains("//") ? filePath : Path.GetFullPath(filePath);

            absolutePath = absolutePath.Replace('\\', '/');
            Loader loader = CreateLoader(absolutePath, out error);

            if (loader == null)
            {
                return(false);
            }
            numTotalImports++;
            loaderList.Add(loader);
            loader.buildOptions = options;
            if (string.IsNullOrEmpty(objName))
            {
                objName = Path.GetFileNameWithoutExtension(absolutePath);
            }
            allLoaded = false;
            StartCoroutine(loader.Load(objName, absolutePath, parentObj));
            error = "";
            return(true);
        }