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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Creating directory from script

Discussion in 'Editor & General Support' started by Marsa-Palas, May 24, 2015.

  1. Marsa-Palas

    Marsa-Palas

    Joined:
    Mar 4, 2015
    Posts:
    8
    I am trying to create a directory from script while in Edit mode. The problem is directory doesn't get created immediately after the function was called. Sometimes it shows in the Project view a whole minute after. But I need that as a resource directory for MyTool asset.

    Why is this happening? Is there some refresh function, or similar that I need to invoke?

    Here is a code for details

    Code (CSharp):
    1. public class MyToolMenu : MonoBehaviour {
    2.     [MenuItem ("GameObject/Util/Create MyTool")]  
    3.     static void CreateMyTool()
    4.     {
    5.         createNewTool();  
    6.     }
    7.  
    8.     static void createNewTool()
    9.     {
    10.         // set resource images
    11.         try
    12.         {
    13.             Directory.CreateDirectory ("Assets/Editor Default Resources/Images");
    14.             File.Copy ("Assets/MyTool/Images/background.jpg", "Assets/Editor Default Resources/Images/background.jpg");
    15.         }
    16.         catch(IOException e)
    17.         {}
    18.  
    19.         // create tool and add it to scene
    20.         GameObject tool = new GameObject("MyTool ");
    21.         tool.transform.position = new Vector3(0, 1, 0);
    22.         tool.AddComponent(typeof(MyTool));
    23.     }
    24. }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use AssetDatabase.Refresh().

    --Eric
     
    Marsa-Palas and liortal like this.
  3. Marsa-Palas

    Marsa-Palas

    Joined:
    Mar 4, 2015
    Posts:
    8
    Thank you!