Search Unity

[SOLVED] Seeking help with AssetDatabase

Discussion in 'Asset Database' started by Issun, Jul 21, 2018.

  1. Issun

    Issun

    Joined:
    Nov 7, 2013
    Posts:
    17
    Hi everybody! I usually try to stick it out and solve problems myself but this one is beyond me and I need to seek some help. I'm doing a project, it's probably not the best way to do it but I've decided to do it this certain way and I've hit a road block...

    I need some help, can anybody out there write me either one of 2 different methods:

    1. A version of AssetDatabase.CreateAsset that takes a normal C# object instead of a UnityEngine Object.
    I will be using a string in the content variable.
    AssetDatabase.CreateAsset(object content, string path);

    2. A version of AssetDatabase.LoadResource that takes a string that is the content to load, as opposed to pointing the method to the path of an .asset file which is just a string encoded in YAML. I'm 99.9% sure this is possible.

    The second one is preferable if possible thank you, either one will make my dreams come true, maybe try to write both if you can! I have no idea how to go about writing either of these methods but i know that if you have a thought in programming 99% of the time you can get it done.

    Thanks in advance.
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Hi Issun,
    As a general rule, I would strongly recommend that you stick to using frameworks / API's etc. in the manner that they were intended. Once you start trying to bend things to your will, you are often just creating a lot more pain for yourself. :)

    So what is the requirement here exactly? How will a string "be created" at the given path? Do you mean create an empty file having that name? Or something else?

    AssetDatabase does not have a method called LoadResource. It does have LoadAssetAtPath which already takes a string.
     
  3. Issun

    Issun

    Joined:
    Nov 7, 2013
    Posts:
    17
    Hello Doug_B thank you for replying to my thread.

    Thank you for your advice, I would usually do this but as i mentioned i've decided to do a thing a certain way, probably not the best way but I'm just trying to get it to work, I am full of shame.

    What I wanted here was to be able for it to create a file, at the path given with the filename given but to just basically be a simple .txt file and to write the given object to that .txt file, mostly a string.

    Yeah you're right i meant LoadAssetAtPath, and yes it does take a string, if we made a new method it would need a new name, in the new method i need i would like to pass it a string to use to create an asset from, kind of like passing a string of JSON to JsonUtility, except I would pass it some YAML.

    My problem in my project is that I am creating a .asset file and trying to load it back in, but unity does not generate the .meta file for the file i create before i try to LoadAssetAtPath, a file must have a .meta file for LoadAssetAtPath to work unfortunately. Alternatively to creating either of these two methods if you know a way to force unity to generate a .meta file for a given file that could help me out too.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    Have you tried calling:
    Code (csharp):
    1. AssetDatabase.SaveAssets();
    2. AssetDatabase.Refresh();
     
  5. Issun

    Issun

    Joined:
    Nov 7, 2013
    Posts:
    17
    Holy crap it worked! I didn't even think of this because the asset I do create, i create it with File.WriteAllText().
    Using AssetDatabase.Refresh() creates the .meta file and allows the file to be loaded with AssetDatabase.LoadAssetAtPath()!

    Cant believe all my searches of "force meta file creation" and similar didnt suggest Refresh()

    My problem solved, thank you very much!
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    Glad to help!
     
    Issun likes this.