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

Load text from dll

Discussion in 'Scripting' started by Entwicklerpages, Apr 10, 2014.

  1. Entwicklerpages

    Entwicklerpages

    Joined:
    Aug 31, 2013
    Posts:
    14
    Hello Community!

    I'm working on a dll-project for Unity. I use Xamarin as my IDE. (It is almost the same - I think it IS the same - as the new MonoDevelope shipped with Unity but without the nice modifications like Shader Support)

    Now I want to add support for data presets. I use serialization to save the data created by my EditorWindow. (I use the BinaryFormatter but I encode the result as Base64 so I can save it easy as an txt file and Unity recognizes it as a TextAsset)

    To do that I would create a preset and then save the generated file to another location. Yes, I could put this file in a subdir of my project but I try to put it in the Editor DLL. If I only put it in a "Prefab" subdir of my project somebody can try to change it. Not a big problem, it is his or her copy and he/she can reimport that file. But it would be perfect if he or she never get the ideo to modifiy the preset. So, it is not a problem. It is only for styling purposes.

    Have anybody an ideo to do that.
    Only putting the whole text into a string in a class is easy but very, very ugly. Are there better options?
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Why is it not saved as .asset?
     
  3. Entwicklerpages

    Entwicklerpages

    Joined:
    Aug 31, 2013
    Posts:
    14
    Hm,

    until now I never used .asset (The full name was Asset Bundles, right?). I just heard sometimes about it. But were Asset Bundles not a pro feature? I have pro but I wan't to sell the finished package at the asset store and it should be aviable for free and pro.

    Thank you for your help!
    (Le'ts start to read Documentation!! :D)
     
  4. Entwicklerpages

    Entwicklerpages

    Joined:
    Aug 31, 2013
    Posts:
    14
    After looking some minutes I know .asset files aren't AssetBundles.

    They are the same as every other asset (.mat for Example).
    So if I understand right, I just write a simple script that load a .txt file as TextAsset and then save the object with the AssetDatabse.CreateAsset as a .asset file right?

    That looks realy nice.
    I think I try to make a script that holds every preset and write it in an asset file.
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Yeah, you can save anything that derive from ScriptableObject into a .asset. Unity handles all the serialization/deserialization.

    Little method to add a "Create Item" menu in the right click in the Project window.
    Code (csharp):
    1.  
    2.     [MenuItem("Assets/Create/Item")]
    3.     public static void CreateItem()
    4.     {
    5.         MethodInfo method = EditorWindow.focusedWindow.GetType().GetMethod("GetActiveFolderPath", BindingFlags.Instance | BindingFlags.NonPublic);
    6.  
    7.         if (method == null)
    8.             return;
    9.  
    10.         ItemData item = ScriptableObject.CreateInstance<ItemData>();
    11.         string path = (string)method.Invoke(EditorWindow.focusedWindow, new object[0]);
    12.  
    13.         path += "/New Item.asset";
    14.  
    15.         AssetDatabase.CreateAsset(item, path);
    16.     }
    17.  
     
  6. Entwicklerpages

    Entwicklerpages

    Joined:
    Aug 31, 2013
    Posts:
    14