Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NerdRageStudios · Feb 13, 2017 at 02:47 AM · editorbuildeditor-scriptingwarningpostprocess

Create custom warning dialog on build, if a script or gameobject exists in the scene

Hi, I am trying to create a warning dialog to show when building / or have built, if I have my developer debugging scripts still enabled.

I tried looking at https://docs.unity3d.com/460/Documentation/ScriptReference/Callbacks.PostProcessBuildAttribute.html but for the life of me I cant seem to work out how to get that to work if there is a script or gameobject still enabled in the scene.

Anyone have a good way of doing this?

Comment
Add comment · Show 1
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 RobAnthem · Feb 13, 2017 at 04:21 AM 0
Share

You could always do a

 #if (UNITY_EDITOR)
 
 #endif

encapsulation on your class. Which will prevent it from getting compiled

2 Replies

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

Answer by jdean300 · Feb 13, 2017 at 04:10 AM

I have done something similar to this. What I did was create my own MenuItem that runs the build using BuildPipeline.BuildPlayer. Either before or after running the build (depending on what you want to do), I load whatever scenes I want to check for editing using this bit of code:

 using (var sceneEdit = new SceneEditHelper("Assets/Scenes/Menu.unity"))
 {
     // use SceneEditHelpers methods to analyze the scene, check for components, etc.
 }

Which makes use of this class:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEditor.SceneManagement;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class SceneEditHelper : IDisposable
 {
     private Scene m_Scene;
 
     public SceneEditHelper(string sceneName)
     {
         m_Scene = EditorSceneManager.OpenScene(sceneName, OpenSceneMode.Single);
     }
 
     public void Dispose()
     {
         // These two lines make sure that any edits to the scene we have made are saved when
         // we dispose of the SceneEditHelper (which the using statement does automatically)
         EditorSceneManager.MarkSceneDirty(m_Scene);
         EditorSceneManager.SaveScene(m_Scene);
     }
 
     /// <summary>
     /// Gets the first component of the given type in the scene,
     /// or null if no component of the given type exists.
     /// </summary>
     public T GetFirstComponentOfType<T>()
         where T : MonoBehaviour 
     {
         var roots = m_Scene.GetRootGameObjects();
         var firstOrDefault = roots.FirstOrDefault(root => root.GetComponentInChildren<T>() != null);
 
         return firstOrDefault != null 
             ? firstOrDefault.GetComponentInChildren<T>() 
             : null;
     }
 
     /// <summary>
     /// Gets all component of the given type from the scene
     /// </summary>
     public IEnumerable<T> GetComponentsOfType<T>()
         where T : MonoBehaviour
     {
         var roots = m_Scene.GetRootGameObjects();
         var components = new HashSet<T>();
 
         foreach (var comp in roots.SelectMany(root => root.GetComponentsInChildren<T>()))
         {
             components.Add(comp);
         }
 
         return components;
     }
 
     /// <summary>
     /// Gets all GameObjects in the scene that fulfill the given predicate
     /// </summary>
     public IEnumerable<GameObject> GetGameObjectsWhere(Func<GameObject, bool> pred)
     {
         var roots = m_Scene.GetRootGameObjects();
         var objects = new List<GameObject>();
 
         foreach (var go in roots)
         {
             if (pred(go))
                 objects.Add(go);
 
             objects.AddRange(go.GetChildrenWhere(pred));
         }
 
         return objects;
     }
 }

You can then show a custom EditorWindow to show whatever warning you want.

EDIT: Sorry, missed the code for a GameObject extension method that the above code uses. Here it is:

 public static IEnumerable<GameObject> GetChildrenWhere(this GameObject obj, Func<GameObject, bool> pred)
     {
         var children = new List<GameObject>();
         for (var i = 0; i < obj.transform.childCount; i++)
         {
             var child = obj.transform.GetChild(i).gameObject;
 
             if (pred(child))
                 children.Add(child);
 
             children.AddRange(GetChildrenWhere(child, pred));
         }
         return children;
     }


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 NerdRageStudios · Feb 13, 2017 at 04:41 AM 0
Share

Thats really helpful thank you, and certainly something useful there for me to build a more extensive method of running post build checks, thanks very much :)

avatar image
0

Answer by Yeasty · Feb 13, 2017 at 08:11 AM

Hey buddy have a look at this http://wiki.unity3d.com/index.php/DebugConsole

Comment
Add comment · 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

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

84 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cancel build in PostProcessScene? 0 Answers

Physics Raycast work fine but don't work after build game. 0 Answers

BCE0044 erro only shows when trying to build .apk 0 Answers

Distribute terrain in zones 3 Answers

How to exclude "Selection & Handles" and other editor-specific code in builds? 2 Answers


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