Exemplo n.º 1
0
        public bool HasAllDependencies()
        {
            string dirPath = MCS_Utilities.Paths.ConvertFileToDir(srcPath);

            unmetDependencies.Clear();
            foreach (string key in paths)
            {
                string path = MCS_Utilities.Paths.ConvertRelativeToAbsolute(dirPath, key);

                UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(path);
                if (obj == null)
                {
                    unmetDependencies.Add(path);
                }
            }

            //if we ourselves are a .mat or .prefab generaiton...
            if (schematics[0].type_and_function.primary_function == PrimaryFunction.material)
            {
                string matPath = srcPath.Replace(".mon", ".mat");
                //file exists but unity hasn't imported it yet, this means we need to update the mat, but we can't b/c it isn't loaded, we'll try again
                if (File.Exists(matPath))
                {
                    UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(matPath);
                    if (obj == null)
                    {
                        unmetDependencies.Add(matPath);
                    }
                }
            }
            if (
                //artitst tools
                (schematics[0].type_and_function.primary_function == PrimaryFunction.item && schematics[0].type_and_function.artisttools_function == ArtistToolsFunction.item)
                ||
                //moonshot
                (schematics[0].type_and_function.primary_function == PrimaryFunction.unknown && schematics[0].type_and_function.artisttools_function == ArtistToolsFunction.geometry)
                )
            {
                string prefabPath = srcPath.Replace(".mon", ".prefab");
                if (File.Exists(prefabPath))
                {
                    UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(prefabPath);
                    if (obj == null)
                    {
                        unmetDependencies.Add(prefabPath);
                    }
                }
            }


            MonDeserializer monDes = new MonDeserializer();

            foreach (string guid in GUIDs)
            {
                bool found     = false;
                bool checkDisk = true;

                //does this GUID exist internally in the schematics?
                foreach (AssetSchematic schematic in schematics)
                {
                    if (schematic == null || schematic.origin_and_description == null || String.IsNullOrEmpty(schematic.origin_and_description.mcs_id) || !guid.Equals(schematic.origin_and_description.mcs_id))
                    {
                        continue;
                    }

                    if (schematic.type_and_function == null || schematic.type_and_function.primary_function != PrimaryFunction.material)
                    {
                        continue;
                    }

                    string matPath = schematic.stream_and_path.generated_path;
                    if (String.IsNullOrEmpty(matPath))
                    {
                        matPath = dirPath + "/Materials/" + schematic.origin_and_description.name + ".mat";
                    }
                    else
                    {
                        matPath = MCS_Utilities.Paths.ConvertRelativeToAbsolute(dirPath, matPath);
                    }

                    //fix for maya
                    matPath = matPath.Replace(":", "_");

                    /*
                     * string baseFBXPath = dirPath + "/Materials/material__" + guid + ".mat";
                     * string baseFBXMetaPath = dirPath + "/Materials/material__" + guid + ".mat.meta";
                     * if (File.Exists(baseFBXPath))
                     * {
                     *  File.Delete(baseFBXPath);
                     * }
                     * if (File.Exists(baseFBXMetaPath))
                     * {
                     *  File.Delete(baseFBXMetaPath);
                     * }
                     */

                    UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(matPath);

                    if (obj != null)
                    {
                        found = true;
                    }
                    else
                    {
                        //we know the path now
                        unmetDependencies.Add(matPath);
                    }

                    checkDisk = false;
                }

                if (checkDisk)
                {
                    string[] paths = Directory.GetFiles(dirPath, "*.mon", SearchOption.AllDirectories);
                    for (int i = 0; i < paths.Length; i++)
                    {
                        string           path            = paths[i].Replace(@"\", "/");
                        AssetSchematic[] otherSchematics = monDes.DeserializeMonFile(path);
                        if (otherSchematics[0].origin_and_description.mcs_id.Equals(guid))
                        {
                            //if the guid was a mat, make sure the mat exists
                            if (otherSchematics[0].type_and_function.primary_function == PrimaryFunction.material)
                            {
                                string             matPath = path.Replace(".mon", ".mat");
                                UnityEngine.Object obj     = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(matPath);
                                if (obj != null)
                                {
                                    found = true;
                                }
                                else
                                {
                                    //we know the path now
                                    unmetDependencies.Add(path);
                                }
                            }
                            else
                            {
                                found = true;
                            }
                            break;
                        }
                    }
                }

                if (!found)
                {
                    unmetDependencies.Add("Material: " + guid);
                }
            }

            return(unmetDependencies.Count == 0);
        }
    Material FindLoadAndAssignFigureMaterial(Material fbxMaterial, string rendererName, string dirPath)
    {
        Material dstMaterial;
        bool     useLods      = true;
        string   materialName = fbxMaterial.name;

        if (materialName == "EyeSheen" || materialName == "EyeAndLash")
        {
            materialName = "EyeAndLash";
            useLods      = false;
        }
        string lodSuffix = "";

        if (useLods)
        {
            int lodPos = rendererName.LastIndexOf("_LOD");
            lodSuffix = rendererName.Substring(lodPos);
        }

        string dstMatBaseName = materialName + lodSuffix;

        string matPath = dirPath + "/Materials/" + dstMatBaseName + ".mat";

        //UnityEngine.Debug.Log("MatPath: " + matPath);

        dstMaterial = AssetDatabase.LoadAssetAtPath <Material>(matPath);
        if (dstMaterial != null)
        {
            return(dstMaterial);
        }

        string dirMon = dirPath + "/Materials";

        string monPath = dirPath + "/Materials/" + dstMatBaseName + ".mon";
        //AssetDatabase.ImportAsset(monPath, ImportAssetOptions.Default | ImportAssetOptions.ForceSynchronousImport);
        MonDeserializer monDes = new MonDeserializer();

        AssetSchematic[] schematics   = monDes.DeserializeMonFile(monPath);
        AssetCreator     assetCreator = new AssetCreator();

        foreach (AssetSchematic schematic in schematics)
        {
            if (schematic.type_and_function.primary_function != Morph3d.Utility.Schematic.Enumeration.PrimaryFunction.material)
            {
                continue;
            }
            Material newMaterial = assetCreator.CreateMorphMaterial(schematic, TextureLoader.GetTextures(schematic, dirMon));

            if (schematic.stream_and_path.generated_path == "" || schematic.stream_and_path.generated_path == null)
            {
                schematic.stream_and_path.generated_path = dirMon + "/" + schematic.origin_and_description.name + ".mat";
            }

            int    pos           = schematic.stream_and_path.generated_path.LastIndexOf('/');
            string directoryPath = schematic.stream_and_path.generated_path.Substring(0, pos);

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }

            //does a material already exist, if so, replace over it
            string dstPath = MCS_Utilities.Paths.ConvertRelativeToAbsolute(dirMon, schematic.stream_and_path.generated_path);
            if (!File.Exists(dstPath))
            {
                AssetDatabase.CreateAsset(newMaterial, dstPath);
            }
            else
            {
                Material oldMat = AssetDatabase.LoadAssetAtPath <Material>(dstPath);
                oldMat.CopyPropertiesFromMaterial(newMaterial);
            }
            dstMaterial = AssetDatabase.LoadAssetAtPath <Material>(dstPath);
            return(dstMaterial);
        }

        dstMaterial = AssetDatabase.LoadAssetAtPath <Material>(matPath);


        return(dstMaterial);
    }
Exemplo n.º 3
0
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            ContentLibrarySO content_so = (ContentLibrarySO)Resources.Load("ContentLibrarySO");
            bool             created    = false;

            //set to true if we find a new asset we weren't tracking before, used to clear out our depenedency psuedo graph and prevent loops
            bool             newAssetImported = false;
            HashSet <string> pathsToReimport  = new HashSet <string>();

            if (content_so == null)
            {
                created    = true;
                content_so = ScriptableObject.CreateInstance <ContentLibrarySO>();
            }

            foreach (var str in importedAssets)
            {
                //UnityEngine.Debug.Log("importing: " + str);
                if (!content_so.importerSeenPaths.Contains(str))
                {
                    content_so.importerSeenPaths.Add(str);
                    newAssetImported = true;
                }

                if (str.Contains(".mr"))
                {
                    // Only process install files here. morph files will be handled separately.
                    if (!(str.Contains(".morph.mr") || str.Contains(".morphs.mr")))
                    {
                        Debug.Log("Found MR File. ");
                        MCS_Utilities.MCSResource resource = new MCS_Utilities.MCSResource();
                        resource.Read(str, false);

                        foreach (string key in resource.header.Keys)
                        {
                            string outputDir = String.Empty;
                            if (key.Contains(".morph") || key.Contains(".bin"))
                            {
                                outputDir = System.IO.Path.Combine(Application.streamingAssetsPath, key);
                            }
                            else
                            {
                                outputDir = Path.Combine(Path.Combine(Path.GetDirectoryName(str), Path.GetFileNameWithoutExtension(str)), key);
                            }
                            //Debug.Log("Output File :" + outputDir);
                            resource.UnpackResource(key, outputDir);
                        }
                        AssetDatabase.Refresh();
                    }
                }
                if (str.Contains(".mon"))
                {
                    schematicLookup.Clear();
                    int    tmpPos = str.LastIndexOf('/');
                    string dirMon = Path.GetDirectoryName(str);// str.Substring(0, tmpPos);

                    AssetSchematic[] schematics;

                    AssetDependency         ad  = null;
                    AssetDependencyImporter adi = null;

                    bool skipAsset = false;

                    if (!content_so.importerDependencies.TryGetValue(str, out ad))
                    {
                        MonDeserializer monDes = new MonDeserializer();
                        schematics = monDes.DeserializeMonFile(str);

                        adi            = new AssetDependencyImporter();
                        adi.srcPath    = str;
                        adi.schematics = schematics;
                        adi.DetermineAllDependencies();
                        content_so.importerDependencies.Add(str, adi);
                    }
                    else
                    {
                        adi        = (AssetDependencyImporter)ad;
                        schematics = adi.schematics;
                    }

                    if (!adi.HasAllDependencies())
                    {
                        adi.attempts++;
                        pathsToReimport.Add(str);
                        skipAsset = true;
                    }


                    AssetCreator ac = new AssetCreator();
                    if (!TryToImportMaterialsFromSchematics(str, dirMon, schematics))
                    {
                        //we have missing materials
                        continue;
                    }

                    if (skipAsset)
                    {
                        continue;
                    }

                    //clean up the variables before re-using them.
                    //look at the first schematic to determine the "MAIN" function, this is legacy, eventually assetschematicimporter will handle this instead
                    AssetSchematic mon = schematics[0];

                    ac = new AssetCreator();

                    mon.stream_and_path.root_path = str;

                    switch (mon.type_and_function.primary_function)
                    {
                    case PrimaryFunction.material:
                        break;

                    default:
                        //handle an FBX (clothing, hair, figure, etc)
                        string fbxFilePath = str.Replace(".mon", ".fbx");
                        if (File.Exists(fbxFilePath))
                        {
                            //use a custom texture and material locator
                            AssetSchematicImporter assetSchematicImporter = new AssetSchematicImporter(null, null, GetTextureFromPathEditor, GetMaterialFromGUIDEditor);
                            assetSchematicImporter.basePath = dirMon;
                            GameObject fbxGO = AssetDatabase.LoadAssetAtPath <GameObject>(fbxFilePath);
                            if (fbxGO == null)
                            {
                                UnityEngine.Debug.LogError("Missing required FBX: " + fbxFilePath);
                                //monDes.ResaveMonFile(str);
                                break;
                            }
                            if (fbxGO != null)
                            {
                                List <AssetSchematic> schematicsList = new List <AssetSchematic>();
                                foreach (AssetSchematic schematic in schematics)
                                {
                                    schematicsList.Add(schematic);
                                }
                                GameObject outputGO = assetSchematicImporter.CreateGameObjectFromExistinGameObject(fbxGO, schematicLookup, schematicsList);
                                //one note, components like Animator and LODGroup will be STRIPPED as they are not included in the component whitelist from CreateGameObjectFromExistinGameObject
                                if (ImportUtilities.RemapMorphsIfRequired(outputGO))
                                {
                                    content_so.refreshOnComplete = true;
                                }
                                //AssetDatabase.Refresh();

                                string prefabFilePath = str.Replace(".mon", ".prefab");

                                GameObject prefabObj;
                                if (!File.Exists(prefabFilePath))
                                {
                                    //UnityEngine.Debug.Log("File does not exist: " + prefabFilePath);
                                    prefabObj = PrefabUtility.CreatePrefab(prefabFilePath, outputGO, ReplacePrefabOptions.Default);     //force a completely new and clean prefab, do not allow old values to transfer over
                                }
                                else
                                {
                                    prefabObj = AssetDatabase.LoadAssetAtPath <GameObject>(prefabFilePath);
                                    //UnityEngine.Debug.Log("Result of load: " + (prefabObj == null ? "null" : "not null"));
                                    if (prefabObj != null)
                                    {
                                        prefabObj = PrefabUtility.ReplacePrefab(outputGO, prefabObj, ReplacePrefabOptions.ConnectToPrefab);
                                        //UnityEngine.Debug.Log("Update of load: " + (prefabObj == null ? "null" : "not null"));
                                    }
                                    else
                                    {
                                        //replace it, there is something wrong with it's state
                                        UnityEngine.Debug.LogWarning("Replacing prefab that is in unworkable state: " + prefabFilePath);
                                        prefabObj = PrefabUtility.CreatePrefab(prefabFilePath, outputGO, ReplacePrefabOptions.Default);     //force a completely new and clean prefab, do not allow old values to transfer over
                                    }
                                }

                                //These components automatically come back, we'll FORCE them to be destroyed b/c we don't need them
                                try
                                {
                                    Animator animator = prefabObj.GetComponent <Animator>();
                                    LODGroup lodGroup = prefabObj.GetComponent <LODGroup>();

                                    if (animator != null)
                                    {
                                        GameObject.DestroyImmediate(animator, true);
                                    }
                                    if (lodGroup != null)
                                    {
                                        GameObject.DestroyImmediate(lodGroup, true);
                                    }
                                    PrefabUtility.RecordPrefabInstancePropertyModifications(prefabObj);

                                    if (Application.isPlaying)
                                    {
                                        GameObject.Destroy(outputGO);
                                    }
                                    else
                                    {
                                        GameObject.DestroyImmediate(outputGO, false);
                                    }
                                } catch (Exception e)
                                {
                                    UnityEngine.Debug.Log("Caught an exception during import component update");
                                    UnityEngine.Debug.LogException(e);
                                }
                            }
                        }
                        break;
                    }
                }
            }

            foreach (var str in deletedAssets)
            {
                if (str.Contains("MCS") && str.Contains(".fbx"))
                {
                    AssetSchematic mon = content_so.AssetSchematicList.Where(x => x.stream_and_path.source_path == str).SingleOrDefault();
                    if (mon != null)
                    {
                        AssetDatabase.DeleteAsset(mon.stream_and_path.generated_path);
                        mon.stream_and_path.generated_path = "";
                        mon.stream_and_path.source_path    = "";
                        if (mon.stream_and_path.root_path == "")
                        {
                            content_so.DeleteItem(mon);
                        }
                    }
                }
                else if (str.Contains("MCS") && str.Contains(".prefab"))
                {
                    AssetSchematic mon = content_so.AssetSchematicList.Where(x => x.stream_and_path.generated_path == str).SingleOrDefault();
                    if (mon != null)
                    {
                        if (AssetDatabase.LoadAssetAtPath(mon.stream_and_path.generated_path, typeof(GameObject)) == null)
                        {
                            mon.stream_and_path.generated_path = "";
                        }
                    }
                }
                else if (str.Contains("MCS") && str.Contains(".mon"))
                {
                    AssetSchematic mon = content_so.AssetSchematicList.Where(x => x.stream_and_path.root_path == str).SingleOrDefault();
                    if (mon != null && mon.stream_and_path != null)
                    {
                        mon.stream_and_path.root_path = "";
                        if (mon.stream_and_path.source_path == "")
                        {
                            content_so.DeleteItem(mon);
                        }
                    }
                }
                else if (str.Contains("MCS") && str.Contains(".mat"))
                {
                    AssetSchematic mon = content_so.AssetSchematicList.Where(x => x.stream_and_path.generated_path == str).SingleOrDefault();
                    if (mon != null && mon.stream_and_path.generated_path != null && AssetDatabase.LoadAssetAtPath(mon.stream_and_path.generated_path, typeof(Material)) == null)
                    {
                        mon.stream_and_path.generated_path = "";
                    }
                }
            }

            for (var i = 0; i < movedAssets.Length; i++)
            {
                //Debug.Log("Moved Asset: " + movedAssets[i] + " from: " + movedFromAssetPaths[i]);
            }

            if (created)
            {
                if (AssetDatabase.IsValidFolder(ROOT_FOLDER) == false)
                {
                    AssetDatabase.CreateFolder("Assets", "MCS");
                }
                if (AssetDatabase.IsValidFolder(RESOURCES_FOLDER) == false)
                {
                    AssetDatabase.CreateFolder(ROOT_FOLDER, "Resources");
                }

                if (AssetDatabase.IsValidFolder(RESOURCES_PREFAB_FOLDER) == false)
                {
                    AssetDatabase.CreateFolder(RESOURCES_FOLDER, "Prefabs");
                }

                if (AssetDatabase.IsValidFolder(RESOURCES_MATERIALS_FOLDER) == false)
                {
                    AssetDatabase.CreateFolder(RESOURCES_FOLDER, "Materials");
                }

                AssetDatabase.CreateAsset(content_so, RESOURCES_FOLDER + "/ContentLibrarySO.asset");
            }


            bool recursed = false;

            if (newAssetImported)
            {
                foreach (string path in pathsToReimport)
                {
                    //UnityEngine.Debug.Log("Reimporting unmet dependency asset: " + path);
                    AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceSynchronousImport);

                    AssetDependencyImporter adi = (AssetDependencyImporter)content_so.importerDependencies[path];
                    if (adi != null)
                    {
                        foreach (string dependencyPath in adi.unmetDependencies)
                        {
                            if (dependencyPath.StartsWith("Material: "))
                            {
                                continue;
                            }

                            if (content_so.importerSeenPaths.Contains(dependencyPath))
                            {
                                continue;
                            }

                            AssetDatabase.ImportAsset(dependencyPath, ImportAssetOptions.ForceSynchronousImport);
                            recursed = true;
                        }
                    }
                }
            }
            else
            {
                //nothing has changed, so stop
                //UnityEngine.Debug.Log("Flushing dependencies graph");
                foreach (string key in content_so.importerDependencies.Keys)
                {
                    AssetDependencyImporter adi = (AssetDependencyImporter)content_so.importerDependencies[key];
                    if (adi.unmetDependencies.Count > 0)
                    {
                        UnityEngine.Debug.LogError("Missing dependencies for: " + key);
                        foreach (string dp in adi.unmetDependencies)
                        {
                            UnityEngine.Debug.Log("Unmet: " + dp);
                        }
                    }
                }
                content_so.ClearDependencies();
            }

            if (!recursed && content_so.refreshOnComplete)
            {
                content_so.refreshOnComplete = false;
                //This causes an error with the prefab, but does work
                //AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
                //This does not work
                //AssetDatabase.ImportAsset("Assets/StreamingAssets");
            }

            EditorUtility.SetDirty(content_so);
        }