Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, November 19, 2014

Unity Full Project Export

I tweak, patch and update many of the assets I purchase / download from the Asset Store. When creating packages (exports) I struggled to create an export that included the "Project Settings" folder where nifty things like the TagManager, LayersManager and InputManager all reside. Sure the old method of zipping up the folder and placing the .zip file inside the Assets folder would work but was extremely clunky.

A quick search on the Unity Answers section led me to an Editor based(1) script that would create a full project export; including the Settings folder. Sweet!

Here is the script, in all its glory!

using UnityEditor;
using UnityEngine;

public class FullExport : MonoBehaviour {


    [MenuItem("Tools/Full Export")]
    private static void NewMenuOption()
    {
        string fileName = "FullProject_Export.unitypackage";
        AssetDatabase.ExportPackage("Assets", fileName, ExportPackageOptions.Interactive | ExportPackageOptions.Recurse | ExportPackageOptions.IncludeLibraryAssets|ExportPackageOptions.IncludeDependencies);
        Debug.Log(fileName + " is located in this projects root folder");
    }
}
To "run" the script, after installing just look at the menubar for "Tools" and then choose "Full Export".



You can download the script already set inside an "Editor" folder which is inside an "_Exporter" folder.


(1) Editor scripts MUST be located inside a folder called "Editor". The folder can be a subfolder AND you can have as many as you want(2) .
(2) Just like the "Resources" folder, they can be scattered through out your project and all will be merged when building your app.

Wednesday, November 12, 2014

Unity 3D ErrorManager v1.1 (text logging)

Edit: Use this with Unity 4.6 and newer; for Unity 4.5 and older, please see THIS post

While I've named it ErrorManager; it could just as easily have been named TextLogManager because it can be use for so much more than just errors.



Here's the top part of the C# file - see below for download link
/// <summary>
/// ErrorLogManager 
///     - Primary: used to log errors to text file
/// 
/// Created by: Gregory Meach
/// Date: November 12, 2014
/// Version: 1.1 (Updated for Unity 4.6)
/// 
/// License: Do what you want, no credit necessary
/// Liability: None whatsoever.
/// 
/// Example: (called from ANY other class)    
///     ErrorLogManager.sharedManager.UpdateErrorLog("fakeMethod","not found",ErrorLogManager.sharedManager.GetErrorDate());
/// Note:
///     GetErrorDate() helper returns "now" as a string
///
/// </summary>

public class ErrorLogManager : MonoBehaviour {
    
    #region ErrorLog
    [Serializable]
    public class ErrorData {
        public string method;
        public string error;
        public string dateString;
    }
    public List<ErrorData> ErrorList = new List<ErrorData>();
    private string currentLogName = string.Empty;
    private string logInitText = string.Empty;
    private bool isFirstSave = true;

    // Set this to whatever name you want to use
    private string errorLogName = "Game_ErrorLog";

    // Set this to the maximum log entries before they are auto-saved
    private int maxLogEntriesBeforeSave = 30;

    // Set this to TRUE to overwrite the log file on startup
    private bool clearErrorLogOnStartup = false;

    // Set this to FALSE to overwrite the log file on startup
    private bool useTimeStampInFileName = true;

    // Helper methods
    public string GetErrorDate() { return System.DateTime.Now.ToString("s"); }
    public string GetSavedDataPath() { return (Application.persistentDataPath + Path.DirectorySeparatorChar); }
    public string GetFileDate() { 
        string dateString = System.DateTime.Now.ToString("d"); 
        dateString = dateString.Replace("/",".");
        return dateString;
    }

    public void UpdateErrorLog(string callingMethod, string errorMsg, string errorDate) {
        ErrorData newError = new ErrorData();
        newError.method = callingMethod;
        newError.error = errorMsg;
        newError.dateString = errorDate;
        ErrorList.Add(newError);
        if (ErrorList.Count > maxLogEntriesBeforeSave)
            FlushErrorLog();
    }

Here's the package link: ErrorManager_v1.1.unitypackage

Tuesday, June 24, 2014

Unity 3D ErrorManager (text logging)

Edit: Use this with Unity 4.5.x and older; for Unity 4.6 and up, please see THIS update

While I've named it ErrorManager; it could just as easily have been named TextLogManager because it can be use for so much more than just errors.

Here's the top part of the C# file - see below for download link
/// <summary>
/// ErrorLogManager 
///     - Primary: used to log errors to text file
/// 
/// Created by: Gregory Meach
/// Date: June 23, 2014
/// Version: 1.0 (initial release)
/// 
/// License: Do what you want, no credit necessary
/// Liability: None whatsoever.
/// 
/// Example: (called from ANY other class)    
///     ErrorLogManager.sharedManager.UpdateErrorLog("fakeMethod","not found",ErrorLogManager.sharedManager.GetErrorDate());
/// Note:
///     GetErrorDate() helper returns "now" as a string
///
/// </summary>

public class ErrorLogManager : MonoBehaviour {
    
    #region ErrorLog
    [Serializable]
    public class ErrorData {
        public string method;
        public string error;
        public string dateString;
    }
    public List<ErrorData> ErrorList = new List<ErrorData>();
    private string currentLogName = string.Empty;
    private string logInitText = string.Empty;
    private bool isFirstSave = true;

    // Set this to whatever name you want to use
    private string errorLogName = "Game_ErrorLog";

    // Set this to the maximum log entries before they are auto-saved
    private int maxLogEntriesBeforeSave = 30;

    // Set this to TRUE to overwrite the log file on startup
    private bool clearErrorLogOnStartup = false;

    // Set this to FALSE to overwrite the log file on startup
    private bool useTimeStampInFileName = true;

    // Helper methods
    public string GetErrorDate() { return System.DateTime.Now.ToString("s"); }
    public string GetSavedDataPath() { return (Application.persistentDataPath + Path.DirectorySeparatorChar); }
    public string GetFileDate() { 
        string dateString = System.DateTime.Now.ToString("d"); 
        dateString = dateString.Replace("/",".");
        return dateString;
    }

    public void UpdateErrorLog(string callingMethod, string errorMsg, string errorDate) {
        ErrorData newError = new ErrorData();
        newError.method = callingMethod;
        newError.error = errorMsg;
        newError.dateString = errorDate;
        ErrorList.Add(newError);
        if (ErrorList.Count > maxLogEntriesBeforeSave)
            FlushErrorLog();
    }

Here's the package link: ErrorManager_v1.0.unitypackage

Tuesday, February 4, 2014

Unity3d and "strings"

I've been using the Unity Asset Store a lot and have downloaded quite a lot of free assets and purchased my fair share as well.

One thing I see a lot in many of the C# scripts (probably in Javasc, oops  UnityScript too) is the repeated use of using plain old STRINGS in critical, e.g. game breaking methods and processes.

I mean we use strings all of the place and it seems like Unity / C# is no exception especially since you define and use both TAGS and LAYERS and they are of course strings!

Here's my GOLDEN RULE for STRINGS:
If I have to type the contents of a string (like "com.meachware.levelOneLeaderboard") more than ONCE; I must create a constant for it. <- period!


The kicker is the simple fix in C# is this:

using UnityEngine;
using System.Collections;

namespace com.ootii.Messages {
    public class Type {

        // A list of Message types.
        public const string all = "EVERYONE";
        public const string filter = "FILTER";
        public const string target = "TARGET";

    }
}
Note: This example is taken directly from the awesome Unity Dispatcher asset I just purchased and even he didn't use constants (that's the reason for the "namespace").

Yup, create a new class and list out all your string constants. Then in ANY other class when you need to access them, instead of typing "FILTER" you simply use "Type.filter" (without the quotes).

Oh AND you get, for FREE type-ahead / autocorrect! How sweet is that!

Now I have not done any benchmarks to see if you have say 1200 const string entries it impacts memory / performance but I gotta believe the benefits outweigh any impact.

PS Full credit for this concept (at least where I learned it from) is the Unity Learn series / Stealth project. Here's the link where they talk about Tag Management.