Skip to content
TheCell's Blog
  • Home
  • Portfolio
  • Electronics and Programming

Unity EditorScripting

On 18. November 2023 by TheCell

Just a quick tutorial on Editorscripts. They can be quite useful and make your life easier, preprocess assets or streamline your workflow. In This example I am populating a gameobject with a bunch of textures that I’ve added to my project. In this example I need a gameobject in the scene which has the TextureListExample MonoBehaviour. I need to have that scene open and then I can use my custom menu tool to repopulate my gameobject references. The script must be created inside a folder called Editor somewhere in the Assets folder hierarchy.

TextureListExample.cs

using System.Collections.Generic;
using UnityEngine;

public class TextureListExample : MonoBehaviour
{
    [SerializeField] private List<Texture2D> allMaterials = new List<Texture2D>();

    public void SetMaterials(List<Texture2D> materials)
    {
        allMaterials = materials;
    }
}

MenuScriptPopulateExampleList.cs

using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

#nullable enable

public class MenuScriptPopulateExampleList
{
    [MenuItem("SSS/PopulateExampleList %&e")]
    public static void PopulateExampleList()
    {
        Debug.Log("Repopulating Texture List");
        var textureListExample = GameObject.FindFirstObjectByType<TextureListExample>();

        if (textureListExample != null)
        {
            var textures = new List<Texture2D>();

            var assetPath = Path.Combine("Assets", "Materials");
            var assetFolders = AssetDatabase.GetSubFolders(assetPath);
            var textureGuids = AssetDatabase.FindAssets("t:Texture2D", assetFolders);
            foreach (var guid in textureGuids)
            {
                var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(AssetDatabase.GUIDToAssetPath(guid));
                textures.Add(texture);
            }

            textureListExample.SetMaterials(textures);
            EditorUtility.SetDirty(textureListExample);
        }
        else
        {
            Debug.LogError("No TextureListExample in this Scene");
        }
    }
}

Helpful references

  • https://learn.unity.com/tutorial/editor-scripting#5c7f8528edbc2a002053b5fa
  • https://docs.unity3d.com/ScriptReference/AssetDatabase.html
  • https://docs.unity3d.com/ScriptReference/EditorUtility.html
Gamedev
Tags: editorscript, menu, unity

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Categories

  • Allgemein
  • Fotos
  • Gamedev
  • games
  • HSLU
  • ImpressionsFrom
  • KVD
  • Papers
  • Persönlich
  • Portfolio
  • Programming
  • Zitate

Recent Posts

  • Unity EditorScripting
  • One way to handle and save Enums
  • The Impressions from Bugsnax
  • The Impressions from Manifold Garden
  • The Impressions from Sable

Archives

  • November 2023
  • December 2022
  • November 2022
  • February 2022
  • November 2021
  • October 2021
  • September 2021
  • July 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • September 2020
  • July 2020
  • April 2020
  • March 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • June 2019
  • February 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • May 2018
  • March 2018
  • February 2018
  • December 2017
  • November 2017
  • September 2017
  • July 2017
  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • October 2016
  • September 2016
  • July 2016
  • May 2016
  • April 2016
  • March 2016
  • August 2015
  • July 2015
  • May 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • October 2014
  • April 2014
  • March 2014
  • February 2014

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright TheCell's Blog 2025 | Theme by Theme in Progress | Proudly powered by WordPress