Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Conversion of code from old project to UWP, a story and a question

Discussion in 'Windows' started by KyleHatch85, Dec 31, 2017.

  1. KyleHatch85

    KyleHatch85

    Joined:
    Dec 13, 2011
    Posts:
    99
    I've been round the house trying to find how to convert a key class i have in my project, finding little snippets but not enough to get the whole picture, so i'm hoping someone might be able to help me fill in the blanks. First the class. Basically it allows me to tag scripts with the configuration Attribute to be auto loaded in when Configs.Init is called. Means i can set up lots of config class and know they will all be loaded without having to manually load each one.

    Code (csharp):
    1.  
    2. using System;
    3. using System.IO;
    4. using System.Reflection;
    5. using UnityEngine;
    6.  
    7. public class ConfigurationAttribute : System.Attribute
    8. {
    9.    
    10. }
    11.  
    12. public class BaseConfig
    13. {
    14.    public virtual bool Init()
    15.    {
    16.        return false;
    17.    }
    18. }
    19.  
    20. public static class Configs
    21. {
    22.    #if UNITY_STANDALONE
    23.    public static string s_sConfigLocation = Application.streamingAssetsPath + Path.DirectorySeparatorChar + "Configs" + Path.DirectorySeparatorChar;
    24.    #elif UNITY_ANDROID || UNITY_IPHONE
    25.    public static string s_sConfigLocation = Application.persistentDataPath + Path.DirectorySeparatorChar + "Configs" + Path.DirectorySeparatorChar;
    26.    #endif
    27.  
    28.    /// <summary>
    29.    /// Initialises the Configs class setting the Root location where all configs can be found!
    30.    ///
    31.    /// Also loads all the configs in this class reporting if any of them failed to load!
    32.    /// </summary>
    33.    public static bool Init()
    34.    {
    35.        return FindAllConfigurationFiles();
    36.    }
    37.    
    38.    private static bool FindAllConfigurationFiles()
    39.    {
    40.        string className = "";
    41.        bool bResult = true;
    42.        Type[] types = null;
    43.        //Get all classes in current domain
    44.        foreach(var dataClass in AppDomain.CurrentDomain.GetAssemblies())
    45.        {
    46.            if(false == dataClass.FullName.Contains("Assembly-CSharp") && true == dataClass.FullName.Contains("Editor") && true == dataClass.FullName.Contains("firstpass"))
    47.            {
    48.                continue;
    49.            }
    50.            
    51.            //Valid type found
    52.            types = dataClass.GetTypes();
    53.            
    54.            foreach(Type type in types)
    55.            {
    56.                if(type.IsDefined(typeof(ConfigurationAttribute), false))
    57.                {
    58.                    className = type.ToString();
    59.                    
    60.                    UnityEngine.Debug.Log("Config found configuration type : " + className);
    61.                    
    62.                    object instance = Activator.CreateInstance(type);
    63.                    
    64.                    try
    65.                    {
    66.                        MethodInfo theMethod = type.GetMethod("Init", BindingFlags.Instance | BindingFlags.Public);
    67.                        theMethod.Invoke(instance, null);
    68.                    }
    69.                    catch(Exception msg)
    70.                    {
    71.                        Debug.LogError("Couldnt locate Init method for config: " + className + " info: " + msg.Message);
    72.                        bResult = false;
    73.                    }
    74.                }
    75.            }
    76.        }
    77.        
    78.        return bResult;
    79.    }
    80.  
    theres more to it than that, but this is the main part, and the part i'm having issue with converting.

    Inside FindAllConfigurationFiles(), the first issue is:
    Code (csharp):
    1. var dataClass in AppDomain.CurrentDomain.GetAssemblies()),
    doesn't seem to exist in UWP. Googling around shows me this https://stackoverflow.com/questions/32487141/get-list-of-loaded-assemblies-on-uap10-platform so i followed that and added in:

    Code (csharp):
    1.  
    2. #if UNITY_STANDALONE || UNITY_EDITOR || UNITY_ANDROID || UNITY_IPHONE
    3.         foreach (var dataClass in AppDomain.CurrentDomain.GetAssemblies())
    4.         {
    5. #elif UNITY_WSA
    6.         foreach (var dataClass in GetAssemblyList().Result)
    7.         {
    8. ...
    9. #endif
    10.  
    If you click on that link you will see that there is an aysnc class, so i changed to .net 4.6, my project uses vuforia which isn't compatabile with il2cpp, so i kept .net as the scripting backend. Also of issue was
    Code (csharp):
    1.  
    2. var files = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFilesAsync();
    this line would not compile, namespave windows.applicationmodel could not be found. So, i wrapped the whole function up in #if UNITY_WSA && NETFX_CORE, it stopped complaining, but i don't know if it will compile or not?

    Going back to FindAllConfigurationFiles(), the next part to stifle me,

    Code (csharp):
    1.  if (type.IsDefined(typeof(ConfigurationAttribute), false))
    When building this gave me: error CS1929: 'Type' does not contain a definition for 'IsDefined' and the best extension method overload 'CustomAttributeExtensions.IsDefined(MemberInfo, Type, bool)' requires a receiver of type 'MemberInfo'. This stump me and a bit of googling got me no where. So here i am.

    So couple queries, the above mainly. Any help would be greatly appreciated.

    Secondly, most of my errors only appeared on building. Is there a simple step i am missing that allows you to see this errors at compile time, they seem like compile errors but only appear when built, i assume this is something to do with how unity see's the scripting backend, or such like.

    Anyway, apologies for the wall of text. Thanks