BuildInfos

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Build Infos page creation)
 
(Compatibility)
Line 9: Line 9:
  
 
==Compatibility==
 
==Compatibility==
'''. Unity versions:''' Unity 2018.1 through Unity 2018.3.8.1f1 (Untested on Unity 2020+).
+
'''. Unity versions:''' Unity 2018.1 through Unity 2019.3.8.1f1 (Untested on Unity 2020+).
 
<br>'''. Net compatibility level:''' 2.0 minimum - no dynamic methods.
 
<br>'''. Net compatibility level:''' 2.0 minimum - no dynamic methods.
 
<br>'''. Unsafe code:''' None.
 
<br>'''. Unsafe code:''' None.
 
<br>'''. Reflectivity:''' None.
 
<br>'''. Reflectivity:''' None.
<br>'''. Networking:''' None.  
+
<br>'''. Networking:''' None.
  
 
==Description==
 
==Description==

Revision as of 19:13, 6 July 2020

Author: Serge Billault

Contents

Archive

BuildInfos.zip

Archive Content

. BuildInfos.cs
. BuildInfosText.cs (optional/exemple)

Compatibility

. Unity versions: Unity 2018.1 through Unity 2019.3.8.1f1 (Untested on Unity 2020+).
. Net compatibility level: 2.0 minimum - no dynamic methods.
. Unsafe code: None.
. Reflectivity: None.
. Networking: None.

Description

Build informations automatic generation.

Usage

Unzip archive content in your Assets folder.
. Action required: none
. Setup required: none
. Scripting: "string infos = ( BuildInfos.Instance != null ) ? BuildInfos.Instance.summary : string.Empty;"

Code

using System;
using UnityEngine;
 
//****************************************************************************************
//
//****************************************************************************************
 
#if UNITY_EDITOR
 
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
 
//****************************************************************************************
//
//****************************************************************************************
 
public class BuildInfosGenerator : IPreprocessBuildWithReport
{
    //************************************************************************************
    //
    //************************************************************************************
 
    public int callbackOrder { get; } = 0;
 
    //************************************************************************************
    //
    //************************************************************************************
 
    public BuildInfos CreateResource()
    {
        BuildInfos build_infos = BuildInfos.Load();
 
        if( build_infos == null )
        {
            try
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo( $"{ Application.dataPath }/{ BuildInfos.DEF_LOCATION }" );
 
                if( dir.Exists == false ) dir.Create();
 
                if( dir.Exists == true  )
                {
                    AssetDatabase.CreateAsset( ScriptableObject.CreateInstance< BuildInfos >(), $"Assets/{ BuildInfos.DEF_LOCATION }/{ BuildInfos.FILENAME }.asset" );
 
                    AssetDatabase.SaveAssets ();
 
                    AssetDatabase.Refresh    ();
 
                    build_infos = BuildInfos.Load();
                }
            }
            catch( Exception e )
            {
                Debug.LogError( e );
            }
        }
 
        return build_infos;
    }
 
    //************************************************************************************
    //
    //************************************************************************************
 
    public void OnPreprocessBuild( BuildReport report ) 
    {
        if( report.summary.result != BuildResult.Failed )
        {
            BuildInfos build_infos = CreateResource();
 
            if( build_infos != null )
            {
                System.DateTime date = System.DateTime.Now;
 
                build_infos.summary  = $"{ Application.productName } { Application.version } - { date.ToString( "M/dd/yyyy' - 'HH':'mm':'ss" ) }";
 
                AssetDatabase.SaveAssets();
 
                AssetDatabase.Refresh   ();
 
                Debug.Log( $"Updated Build infos > { build_infos.summary }" );
            }
        }
    }
}
 
#endif
 
//****************************************************************************************
//
//****************************************************************************************
 
#if UNITY_EDITOR
 
[ CreateAssetMenuAttribute( fileName = BuildInfos.FILENAME, menuName = MENUNAME, order = 0 ) ]
 
#endif
 
public class BuildInfos : ScriptableObject
{
    //************************************************************************************
    //
    //************************************************************************************
 
           public  const string FILENAME     = "BUILD_INFOS";
 
           public  const string MENUNAME     = "Build Infos";
 
           public  const string DEF_LOCATION = "BuildInfos/Resources";
 
    static private BuildInfos   m_instance = null;
 
    static public  BuildInfos   Instance { get { if( m_instance == null ) Load(); return m_instance; } }
 
    static public  BuildInfos   Load()   { if( m_instance == null ) { m_instance = ( BuildInfos )Load( FILENAME ); } return m_instance; }
 
    //************************************************************************************
    //
    //************************************************************************************
 
    static public BuildInfos Load( string resPath )
    {
        BuildInfos instance = Resources.Load< BuildInfos >( resPath );
 
        if( instance != null ) instance.hideFlags = HideFlags.None;
 
        return instance;
    }
 
    //************************************************************************************
    //
    //************************************************************************************
 
    [ SerializeField ] private string m_summary = string.Empty;
 
    public string summary { get { return m_summary; } set { m_summary = ( value != null ) ? value : string.Empty; }  }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox