Example #1
0
    static T Create(string newPath, bool focus = false)
    {
        if (!newPath.StartsWith("Assets/"))
        {
            newPath = "Assets/" + newPath;
        }
        var asset = UnityEditor.AssetDatabase.LoadAssetAtPath <T>(newPath);

        if (asset != null)
        {
            return(asset);
        }

        asset = ScriptableObject.CreateInstance <T>();

        int firstBar = newPath.Length - 1;

        for (; firstBar >= 0 && newPath[firstBar] != '/'; firstBar--)
        {
        }
        if (firstBar >= 0)
        {
            FileAdapter.RequestDirectory(newPath.Substring(0, firstBar));
        }

        string assetPathAndName = UnityEditor.AssetDatabase.GenerateUniqueAssetPath(newPath + ".asset");

        Debug.Log($"Try create asset at {newPath} and created at {assetPathAndName}");

        UnityEditor.AssetDatabase.CreateAsset(asset, assetPathAndName);

        UnityEditor.AssetDatabase.SaveAssets();
        UnityEditor.AssetDatabase.Refresh();

        if (focus)
        {
            UnityEditor.EditorUtility.FocusProjectWindow();
            UnityEditor.Selection.activeObject = asset;
        }

        return(asset);
    }
Example #2
0
    public static EditorLog CreateNewOne( string name, bool editorOnly = false )
    {
        #if UNITY_EDITOR
        var dir = DEFAULT_DIRECTORY + ( editorOnly ? "Editor/" : string.Empty );
        var path = dir + name + "Log";
        var asset = ScriptableObject.CreateInstance<EditorLog>();

        asset.Add( $"Created log of {name}" );
        FileAdapter.RequestDirectory( dir );

        string assetPathAndName = UnityEditor.AssetDatabase.GenerateUniqueAssetPath( path + ".asset" );

        UnityEditor.AssetDatabase.CreateAsset( asset, assetPathAndName );

        UnityEditor.AssetDatabase.SaveAssets();
        UnityEditor.AssetDatabase.Refresh();

        return asset;
        #else
        Debug.Log( "You shouldnt call this function(EditorLog.CreateNewOne) outside of editor" );
        return null;
        #endif
    }