Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How am I supposed to run the following code?

Discussion in 'Editor & General Support' started by IIBearWithII, Jun 10, 2023.

  1. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    This code is in the manual, under "EditorBuildSettings.scenes". I was thinking if I attached it to an empty GameObject it would run, but maybe EditorWindow doesn't trace back to MonoBehaviour?

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. public class ExampleWindow : EditorWindow
    6. {
    7.     List<SceneAsset> m_SceneAssets = new List<SceneAsset>();
    8.  
    9.     // Add menu item named "Example Window" to the Window menu
    10.     [MenuItem("Window/Example Window")]
    11.     public static void ShowWindow()
    12.     {
    13.         //Show existing window instance. If one doesn't exist, make one.
    14.         EditorWindow.GetWindow(typeof(ExampleWindow));
    15.     }
    16.  
    17.     void OnGUI()
    18.     {
    19.         GUILayout.Label("Scenes to include in build:", EditorStyles.boldLabel);
    20.         for (int i = 0; i < m_SceneAssets.Count; ++i)
    21.         {
    22.             m_SceneAssets[i] = (SceneAsset)EditorGUILayout.ObjectField(m_SceneAssets[i], typeof(SceneAsset), false);
    23.         }
    24.         if (GUILayout.Button("Add"))
    25.         {
    26.             m_SceneAssets.Add(null);
    27.         }
    28.  
    29.         GUILayout.Space(8);
    30.  
    31.         if (GUILayout.Button("Apply To Build Settings"))
    32.         {
    33.             SetEditorBuildSettingsScenes();
    34.         }
    35.     }
    36.  
    37.     public void SetEditorBuildSettingsScenes()
    38.     {
    39.         // Find valid Scene paths and make a list of EditorBuildSettingsScene
    40.         List<EditorBuildSettingsScene> editorBuildSettingsScenes = new List<EditorBuildSettingsScene>();
    41.         foreach (var sceneAsset in m_SceneAssets)
    42.         {
    43.             string scenePath = AssetDatabase.GetAssetPath(sceneAsset);
    44.             if (!string.IsNullOrEmpty(scenePath))
    45.                 editorBuildSettingsScenes.Add(new EditorBuildSettingsScene(scenePath, true));
    46.         }
    47.  
    48.         // Set the Build Settings window Scene list
    49.         EditorBuildSettings.scenes = editorBuildSettingsScenes.ToArray();
    50.     }
    51. }
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    Did you try to read the code?

    The
    MenuItem
    part near the top could be a hint of how it's intended to be used.
     
    IIBearWithII likes this.
  3. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    Wow, ok. It's in the menu already, just by putting it in my Assets folder? Cool!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Sort of.

    This highlights exactly why "just send me the script!" is completely and totally useless.

    Yes, the presence of this script in your project will enable that menu item.

    But what you may (or may not) have noticed is that you probably can no longer build your game.

    This is because built games do not have the UnityEditor namespace.

    Unity has VERY specilic rules about folders. This file must be placed in a folder called
    Editor/
    , not just anywhere in the project.

    Wherever you got this SHOULD have told you this.

    Again, read the code, reason about what it does, what it touches, there is simply no substitute for that.
     
    IIBearWithII likes this.
  5. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    This is the only other thing that was on the page:

    public static EditorBuildSettingsScene[] scenes;
    Description
    The list of Scenes that should be included in the build. This is the same list of Scenes that is shown in the Build Settings window. You can modify this list to set up which Scenes should be included in the build.
     
  6. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    I'm kinda struggling to understand how you're going about learning Unity. Are you just running down every page in the scripting documentation? A lot of examples aren't useful on their own and have the expectation of some kind of prior learning.

    For example that code example is useless to anyone who doesn't already understand the requirements of editor coding. And they would know it's just a super condensed example to show how you might use this specific API.
     
    Kurt-Dekker likes this.
  8. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    No, I'm not doing that.

    I just finished reading "Unity in Action," the third edition, by Joseph Hocking. Any questions not answered there I look up in Unity documentation or ask on the forums here.
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Not sure why you would read a book on Unity. It was probably out of date before it was finished being written. It's a game engine, you gotta learn by doing. I would've just doing the junior programmer course on the Unity Learn website.
     
    Kurt-Dekker likes this.
  10. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    898
    That script is an example of using EditorBuildSettings.scenes. This particular example decided to put the example in an Editor script.

    A lot of Unity examples are going to be contextless and assume a base level of editor understanding. Asking here doesn't really hurt, but to speed your progress you'll want to learn how to piece together the clues in the code to see how to use and apply it. Here, the script inherits from EditorWindow, not MonoBehaviour. Click the 'EditorWindow' link to follow the paper trail and piece together what's going on.

    In short, you'll need to develop detective and self-learning skills to develop effectively in Unity. ;)
     
    IIBearWithII likes this.
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I strongly suggest you simply ask yourself again and again this question: "Can I...?"

    I know you've probably seen this before but I'm telling you... THIS METHOD WORKS.

    One tiny step at a time, each step building on the previous until suddenly you look up one day and you realize you actually know how to do a lot of amazing things in Unity.

    Imphenzia: How Did I Learn To Make Games:

     
    IIBearWithII likes this.
  12. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    Reading books is always my first go to, but I'm always open to trying other methods of learning as well. No one learning source ever seems to cover everything for me. There's always some gaps, even if only because the way it was explained didn't quite click for me.
     
  13. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    Nice video. Sort of what I'm doing, I think. I plan on making simple games to start. I have two projects on the go right now, one mostly text-based, and the other one strictly 2D graphics.
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Bad for gamedev. It just changes WAAAAAAAAY too fast. Gamedev is all dynamic and wiggly and changes every day. Roll with it or you are ALWAYS going to struggle.

    Do this ^ ^ ^

    And by simple, make it SUPER simple.

    1. Start with solo PONG

    2. Change that into BRICK OUT

    3. Make the bricks move and you have SUPER BRICK OUT

    4. Make the bricks shoot back and you have SPACE INVADERS

    If you cannot do the four above games then you won't have success doing any other game either. EVERYTHING is more complicated than the above, even Flappy Bird. Flappy bird is confusing because it has a shifting frame or reference which can be implemented any number of ways.

    Start with the above four games.
     
    IIBearWithII likes this.
  15. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    Thanks for the clarification. I'll definitely work on my detective skills. I wonder if there's some online Unity course with a murder-mystery theme??....
     
  16. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    This script that I started this thread with still forces me to select each scene one at a time. Is there any way to adjust the code to have it add, say, 100 scenes from a particular file directly into the build settings?
     
  17. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Oh my gosh, you don't need code for ANY of this!

    Just filter the project for scenes, select all the ones you want, drag them into the Editor Build Settings panel all at once.
     
    IIBearWithII likes this.
  18. IIBearWithII

    IIBearWithII

    Joined:
    Feb 15, 2023
    Posts:
    87
    Ok, thanks!