Пример #1
0
        /// <summary>
        /// Creates prefabs for the 3D Scatter assets.
        /// </summary>
        /// <param name="hasBillboard"></param>
        /// <returns></returns>
        public static void CreatePrefabsScatter(PrefabData prefabData)
        {
            try
            {
                string            prefabPath         = MegascansUtilities.ValidateFolderCreate(prefabData.assetPath, "Prefabs");
                int               numberOfVariations = MegascansUtilities.GetMeshChildrenCount(prefabData.importedGeometryPaths3D);
                List <GameObject> prefabObjects      = new List <GameObject>();

                for (int i = 0; i < numberOfVariations; i++)
                {
                    string prefabName = prefabData.finalAssetName.Replace("$mapName", "").Replace("$resolution", "").Replace("$lod", "").Replace("$variation", "");
                    string varName    = "Var" + (i + 1).ToString();
                    prefabName = prefabName.Contains("$variation") ? prefabName.Replace("$variation", "Var" + varName) : prefabName + varName;

                    //Setting up prefab gameobject
                    GameObject prefabGameObject = new GameObject();
                    prefabGameObject.name     = prefabName;
                    prefabGameObject.isStatic = true;
                    if (prefabData.setupLODs)
                    {
                        prefabGameObject.AddComponent <LODGroup>();
                        prefabGameObject.GetComponent <LODGroup>().fadeMode           = (LODFadeMode)prefabData.lodFadeMode; //Casting lod fade mode to enum.
                        prefabGameObject.GetComponent <LODGroup>().animateCrossFading = true;
                    }

                    List <LOD> lodsForPrefab = new List <LOD>();
                    int        numberOfFiles = prefabData.importedGeometryPaths3D.Count;

                    List <float> lodHeights = MegascansUtilities.getLODHeightList(numberOfFiles);
                    //Instantiate all the meshes in the scene, add them to the material/collider to them.
                    for (int x = 0; (x < numberOfFiles && x < 8); x++)
                    {
                        UnityEngine.Object loadedGeometry = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(prefabData.importedGeometryPaths3D[x]);
                        //Highpoly mesh check.
                        if (loadedGeometry.name.ToLower().Contains("highpoly") && !prefabData.highpoly)
                        {
                            continue;
                        }
                        GameObject geometryObject = Instantiate(loadedGeometry) as GameObject;
                        Renderer[] r;
                        if (prefabData.isAlembic) //if the instantiated mesh is an alembic asset.
                        {
                            //Get all variations in a LOD
                            List <Transform> varsInLOD = new List <Transform>();
                            foreach (Transform var in geometryObject.transform)
                            {
                                varsInLOD.Add(var);
                            }
                            //Delete all the other variations in the LOD object
                            for (int y = 0; y < varsInLOD.Count; y++)
                            {
                                //If variation does not match one currently being processed.
                                if (y != i)
                                {
                                    DestroyImmediate(varsInLOD[y].gameObject);
                                }
                            }
                            //Parent the child gameobject (geometry) to the prefab game object.
                            geometryObject.transform.parent        = prefabGameObject.transform;
                            geometryObject.transform.localPosition = Vector3.zero;
                            geometryObject.name = geometryObject.name.Replace("(Clone)", "");
                            r = geometryObject.GetComponentsInChildren <Renderer>();
                        }
                        else//if the instantiated mesh is a scatter type asset.
                        {
                            //Get all variations in a LOD
                            List <Transform> varsInLOD = new List <Transform>();
                            foreach (Transform var in geometryObject.transform)
                            {
                                varsInLOD.Add(var);
                            }
                            //Delete all the other variations in the LOD object
                            for (int y = 0; y < varsInLOD.Count; y++)
                            {
                                //If variation does not match one currently being processed.
                                if (y != i)
                                {
                                    DestroyImmediate(varsInLOD[y].gameObject);
                                }
                            }
                            //Parent the child gameobject (geometry) to the prefab game object.
                            geometryObject.transform.parent        = prefabGameObject.transform;
                            geometryObject.transform.localPosition = Vector3.zero;
                            geometryObject.name = geometryObject.name.Replace("(Clone)", "");
                            r = geometryObject.GetComponentsInChildren <Renderer>();
                        }

                        foreach (Renderer ren in r)
                        {
                            ren.material = prefabData.finalMat;
                            //Apply highpoly material if the mesh was highpoly and highpoly filter is enabled.
                            if (loadedGeometry.name.ToLower().Contains("highpoly") && prefabData.highpoly)
                            {
                                ren.material = prefabData.highpolyMat;
#if UNITY_EDITOR_OSX
                                ren.material = prefabData.finalMat;
#endif
                            }
                            //Apply collision
                            if (prefabData.setupCollision)
                            {
                                ren.gameObject.AddComponent <MeshCollider>().sharedMesh = ren.gameObject.GetComponent <MeshFilter>().sharedMesh;
                            }
                        }

                        if (prefabData.setupLODs)
                        {
                            lodsForPrefab.Add(new LOD(lodHeights[0], r));
                            lodHeights.RemoveAt(0);
                        }
                        else
                        {
                            break;
                        }
                    }
                    //Set LODs in the LOD group
                    if (prefabData.setupLODs)
                    {
                        prefabGameObject.GetComponent <LODGroup>().SetLODs(lodsForPrefab.ToArray());
                        prefabGameObject.GetComponent <LODGroup>().RecalculateBounds();
                    }
                    //Prefab saving
                    string prefLocation = prefabPath + "/" + prefabName + ".prefab";
                    prefLocation = prefLocation.Replace("(Clone)", "");
                    GameObject prefabObject = SavePrefab(prefabGameObject, prefLocation, prefabData.addAssetToScene);
                    if (prefabObject)
                    {
                        prefabObjects.Add(prefabObject);
                    }
                }

                //Setting up variation holder gameobject
                GameObject scatterParent = new GameObject(prefabData.assetName);
                scatterParent.isStatic = true;
                foreach (GameObject variation in prefabObjects)
                {
                    variation.transform.parent = scatterParent.transform;
                }
            }
            catch (Exception ex)
            {
                Debug.Log("Exception::MegascansMeshUtils::3D Asset Prefab:: " + ex.ToString());
                MegascansUtilities.HideProgressBar();
            }
        }