Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Any Macro to detect 4.5 .Net API Level?

Discussion in 'Experimental Scripting Previews' started by Voxel-Busters, Sep 30, 2017.

  1. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    We are facing some issues where On older API .Net level code was running fine but new .net 4.5 has issues.

    Please check below.

    Statement below .Net 4.5
    Code (CSharp):
    1. _xmlWriter.WriteAttributeString("xmlns:android", "http://schemas.android.com/apk/res/android");
    This throw the below exception
    ArgumentException: Invalid name character in 'xmlns:android'. The ':' character, hexadecimal value 0x3A, cannot be included in a name.


    Above Code line in .Net 4.5 needs to be converted to
    Code (CSharp):
    1. _xmlWriter.WriteAttributeString("xmlns", "android", null, "http://schemas.android.com/apk/res/android");
    The same statement below .Net 4.5 is throwing exception saying null can't be passed.

    It would be great if it can be resolved or consistent. If its supposed to work as above, there should be a way to detect which code to use (may be via macros or any api variables)

    For further references please check on our forum page.

    @JoshPeterson

    Thanks,
    VB Team
     
    Last edited: Sep 30, 2017
  2. joncham

    joncham

    Unity Technologies

    Joined:
    Dec 1, 2011
    Posts:
    276
    You should be able to check for a NET_4_6 define.
     
  3. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,963
    Thanks @joncham. May I know from which Unity version it exists?

    Here is the code we have currently due to inconsistencies with 4.6. I hope i it will be fixed soon.

    protected void WriteAttributeString (XmlWriter _xmlWriter, string _prefix, string _localName, string _nameSpace, string _value)
    {
    #if UNITY_5_6_OR_NEWER
    if (GetAPICompatibilityLevel() == ApiCompatibilityLevel.NET_4_6)
    {
    if (string.IsNullOrEmpty (_prefix) && string.IsNullOrEmpty (_nameSpace))
    _xmlWriter.WriteAttributeString (_localName, _value);
    else
    _xmlWriter.WriteAttributeString (_prefix, _localName, _nameSpace, _value);
    }
    else
    #endif
    {
    if (!string.IsNullOrEmpty (_nameSpace))
    {
    _xmlWriter.WriteAttributeString (_prefix + ":" + _localName, _nameSpace, _value);
    }
    else if (!string.IsNullOrEmpty (_prefix))
    {
    _xmlWriter.WriteAttributeString (_prefix + ":" + _localName, _value);
    }
    else
    {
    _xmlWriter.WriteAttributeString (_localName, _value);
    }
    }
    }

    private ApiCompatibilityLevel GetAPICompatibilityLevel ()
    {
    #if !UNITY_5_6_OR_NEWER
    return UnityEditor.PlayerSettings.apiCompatibilityLevel;
    #else
    return UnityEditor.PlayerSettings.GetApiCompatibilityLevel(EditorUserBuildSettings.selectedBuildTargetGroup);
    #endif
    }
     
  4. joncham

    joncham

    Unity Technologies

    Joined:
    Dec 1, 2011
    Posts:
    276
    This should be available from 2017.1 and newer. Same versions that the new Scripting Runtime has been available.