Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ash-Blue · Dec 11, 2014 at 01:22 PM · editorsceneeditor-scriptingmap

Post-Process All Unity Scene GameObjects

I want to loop through all of my project's scenes in a specific folder to figure out if they have certain GameObjects a player can collect. Super Metroid does this in a similar fashion, where it marks the location of items you can collect on a map. Then if you collect an item, its marked as collected.

alt text

I need to store the item location data inside a file or GameObject I can reference while the game is running. This should be doable from the Unity editor through a menu command.

images.jpg (14.6 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by jimfleming · Dec 11, 2014 at 06:09 PM

I think this is what you want. If you need to be able to iterate through the scenes to gather the game objects while its running you'll have to manage the play state manually using EditorApplication.isPlaying.

 using UnityEditor;
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public static class GatherGameObjects {
 
   public static List<string> GetScenes(string basePath = "") {
     List<string> scenes = new List<string>();
     foreach (UnityEditor.EditorBuildSettingsScene scene in UnityEditor.EditorBuildSettings.scenes) {
       if (scene.enabled && scene.path.StartsWith(basePath)) {
         scenes.Add(scene.path);
       }
     }
     return scenes;
   }
 
   public static List<GameObject> GatherGameObjectsByTag(List<string> scenes, string tag) {
     // The GO references probably go out of scope when the scene closes
     // (You'll want to store something else like their absolute path in the hierarchy)
     var gameObjects = new List<GameObject>();
 
     // Save the current scene before attempting to open others
     var safe = EditorApplication.SaveCurrentSceneIfUserWantsTo();
     if (safe) {
       foreach (var scene in scenes) {
         EditorApplication.OpenScene(scene);
 
         // If you want to use different criteria try Object.FindObjectsOfType<GameObject>()
         // I don't think this includes inactive but maybe that doesn't matter
         gameObjects.AddRange(GameObject.FindGameObjectsWithTag(tag));
       }
     }
 
     return gameObjects;
   }
 
   [MenuItem("GameObject/Gather Collectible GameObjects")]
   public static void GatherCollectibleGameObjects() {
     var scenes = GetScenes("Assets/");
     var gameObjects = GatherGameObjectsByTag(scenes, "Collectible");
 
     // TODO: Write to file
     Debug.Log(gameObjects.Count);
   }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ash-Blue · Dec 17, 2014 at 06:50 PM 0
Share

Here is the solution I came up with based upon this code. It includes some extra logic to make things Web Player safe and includes JSON file writing logic.

 using UnityEditor;
 using UnityEngine;
 using System.Linq;
 using System.Collections;
 using System.Collections.Generic;
 using System.IO;
 using Newtonsoft.Json;
 
 public class Record$$anonymous$$apLocations : $$anonymous$$onoBehaviour {
     // Add a menu item named "Do Something" to $$anonymous$$y$$anonymous$$enu in the menu bar.
     [$$anonymous$$enuItem ("PostProcess/Record $$anonymous$$ap Locations")]
     static void DoSomething () {
         # if UNITY_WEBPLAYER
             Debug.LogError("File writing is blocked in Web Player platform, please change to a different platform and try again");
         # elif UNITY_STANDALONE
             bool safe = EditorApplication.SaveCurrentSceneIfUserWantsTo();
             Dictionary<string, int> collectibleCount = new Dictionary<string, int>();
             Dictionary<string, bool> respawn = new Dictionary<string, bool>();
 
             // If the user doesn't make a menu choice to save exit early
             if (safe) {
                 List<string> scenes = GetScenes("Assets/Scenes");
                 foreach (string s in scenes) {
                     EditorApplication.OpenScene(s);
 
                     string safeSceneName = s.Split('/').Last().Replace(".unity", "");
                     collectibleCount[safeSceneName] = CountGameObjectsByTag("Collectible");
                     respawn[safeSceneName] = CountGameObjectsByTag("Respawn") > 0;
                 }
 
                 // Serializes data to JSON, uses JSON.net from the Unity asset store, but feel free to use your
                 // own solution
                 string jsonCollectible = JsonConvert.SerializeObject(collectibleCount, Formatting.Indented);
                 string jsonRespawn = JsonConvert.SerializeObject(respawn, Formatting.Indented);
 
                 // Dump both strings into proper files via System.IO
                 WriteFile("$$anonymous$$apCollectibles.json", jsonCollectible);
                 WriteFile("$$anonymous$$apRespawn.json", jsonRespawn);
                 UnityEditor.AssetDatabase.Refresh();
             }
         #endif
     }
 
     public static void WriteFile (string fileName, string text) {
         // Force suppress a Web Player platform error caused by removing System.IO file writing
         # if UNITY_STANDALONE
             File.WriteAllText(Application.dataPath + "/Resources/Data/" + fileName, text);
         # endif
     }
 
     public static List<string> GetScenes(string basePath = "") {
         List<string> scenes = new List<string>();
         foreach (UnityEditor.EditorBuildSettingsScene scene in UnityEditor.EditorBuildSettings.scenes) {
             if (scene.enabled && scene.path.StartsWith(basePath)) {
                 scenes.Add(scene.path);
             }
         }
 
         return scenes;
     }
 
     // @I$$anonymous$$PORTANT This will wipe the user's current scene data if it isn't saved, be careful
     public static int CountGameObjectsByTag (string tag) {
         return GameObject.FindGameObjectsWithTag(tag).Length;
     }
 }
 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating AssetBundles from Editor Scripts 1 Answer

How can I make function run in the editor only when I press a button in the inspector? 1 Answer

Cannot close/unload a scene that is open in editor during playmode (using C# code)? 3 Answers

Is it possible to ensure that certain game objects don't get saved in the scene or otherwise hook into the default save scene to run custom editor code? 2 Answers

Editor Scene? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges