public static void CreateBackgroundElementPrefabs() { if (BackgroundTypesDictionary.Count == 0) { Debug.LogError("Please initialize the manager first; it doesn't know yet which types exist. (Background Types Dictionary is empty)"); return; } string prefabPath = GeneralInformation.Paths.PrefabsFolder; GeneralOperations.CreateFolderIfEmpty(prefabPath); // create a prefab for each type foreach (KeyValuePair <BackgroundTypes, BackgroundType> pair in BackgroundTypesDictionary) { // create parent empty object GameObject gameObject = new GameObject(pair.Key.ToString()); gameObject.AddComponent <BackgroundElement>().SetType(pair.Key); if (pair.Value.ShouldBlockUpwards) { gameObject.AddComponent <BoxCollider>(); } // create geometric placeholder child CreateBasicObject(name: "Geometric", parent: gameObject.transform, tag: "EditorOnly", mesh: pair.Value.geometricMesh, material: pair.Value.Materials[0]); // create pretty inGame child GameObject prettyChild = CreateBasicObject(name: "Pretty", parent: gameObject.transform, mesh: pair.Value.Meshes[0], material: pair.Value.Materials[0]); if (pair.Value.CanCollide) { MeshCollider collider = prettyChild.AddComponent <MeshCollider>(); // TODO: add an option for a non-automatic collider? collider.convex = true; } prettyChild.SetActive(false); // make a prefab out of it and remove it from the scene GameObject prefab = PrefabUtility.SaveAsPrefabAsset(gameObject, prefabPath + Separator + pair.Key.ToString() + ".prefab"); // Object.Destroy cannot be called in edit mode Object.DestroyImmediate(gameObject); } }
public static void InitializeManager() { // get all background types automatically // this is faster (if harder to read) than Resources.FindObjectsOfTypeAll // because it only searches a few folders instead of the whole project string[] typesPaths = AssetDatabase.FindAssets("t: BackgroundType", new[] { GeneralInformation.Paths.TypesFolder }); foreach (string path in typesPaths) { BackgroundType type = AssetDatabase.LoadAssetAtPath <BackgroundType>(AssetDatabase.GUIDToAssetPath(path)); if (BackgroundTypesDictionary == null) { BackgroundTypesDictionary = new Dictionary <BackgroundTypes, BackgroundType>(); } if (!BackgroundTypesDictionary.ContainsValue(type)) { BackgroundTypesDictionary.Add(type.Type, type); // for each background type, get the meshes from the mesh folder string[] meshesPaths = AssetDatabase.FindAssets( type.FindMeshesByName? type.Type.ToString() + " t: Mesh": "t: Mesh" , new[] { type.MeshesFolder }); if (meshesPaths.Length == 0) { Debug.LogError("Could not find meshes to use for " + type.Type.ToString() + " at " + type.MeshesFolder + ". If this is not fixed, the Prefab Creation will fail."); } foreach (string meshPath in meshesPaths) { Mesh mesh = AssetDatabase.LoadAssetAtPath <Mesh>(AssetDatabase.GUIDToAssetPath(meshPath)); if (!type.Meshes.Contains(mesh)) { type.Meshes.Add(mesh); } } } } // create x versions of each material per type, ensure it has the right scale // and offset it x different ways // we need to use Database.CreateAsset so there is a path on disk to the new materials; // otherwise the ScriptableObject trying to link to the new materials throws an error Vector2 tiling; Material newMat; string matPath; string matFolder; foreach (BackgroundType type in BackgroundTypesDictionary.Values) { if (!type.ShouldOffsetTexture) { type.NewMaterials = type.Materials; } else if (type.NewMaterials == null || type.NewMaterials.Count == 0) { // we find the folder containing the materials // and create a subfolder for the variations if (type.Materials == null || type.Materials.Count == 0) { Debug.LogError("You forgot to add materials to the element type " + type.Type + "!"); continue; } matPath = AssetDatabase.GetAssetPath(type.Materials[0]); int lastSlash = matPath.LastIndexOf(Separator); matPath = matPath.Remove(lastSlash); matFolder = matPath + Separator + GeneralInformation.Paths.AlternativeMaterialsFolderName; GeneralOperations.CreateFolderIfEmpty(matFolder); // for cleaning up purposes later if (!AlternateMaterialsFoldersPaths.Contains(matFolder)) { AlternateMaterialsFoldersPaths.Add(matFolder); if (isDebugging) { Debug.Log("created folder " + matFolder); } } tiling = type.TextureTiling; foreach (Material mat in type.Materials) { mat.mainTextureScale = tiling; for (int i = 0; i < 3; i++) { newMat = new Material(mat) { mainTextureOffset = new Vector2(Random.Range((float)0, (float)1), Random.Range((float)0, (float)1)) }; AssetDatabase.CreateAsset(newMat, matPath + Separator + GeneralInformation.Paths.AlternativeMaterialsFolderName + Separator + (mat.name + "_" + type.name + "_v" + i + ".mat")); type.NewMaterials.Add(newMat); if (isDebugging) { Debug.Log("added material " + newMat + " to the list."); } } //and then reset it so as not to commit useless stuff each time you make one small change mat.mainTextureScale = Vector2.one; } if (isDebugging) { Debug.Log("Just treated " + type.name + " background type."); } } } }