Pushing code from Unity3D

Unity provides several post-processing capabilities that allow you to both intercept and override and also add your own processing to just about anything in the Asset pipeline, anything from assets, scripts, and even the build process itself.

Processing assets

Post- or pre-processing of assets is very useful if you have custom-made or complex assets that need additional work once they are imported in Unity. In most cases, this is not needed as Unity does a lot of work for you by processing assets already.

Note

If you do create any asset-processing scripts, remember they need to be placed in AssetsEditor.

We won't go into too much detail here as it is a very large area; this section is mainly to highlight its existence for those who were not aware. It is well-worth reading and checking up on.

Tip

For more information about asset processing, refer to the Unity scripting reference guide at https://docs.unity3d.com/Documentation/ScriptReference/AssetPostprocessor.html.

For a nice and clean example of an asset processor, see the post on using Unity to make a simple FBX model post processor at http://forum.unity3d.com/threads/53179-Simple-AssetPostprocessor-example.

Processing the build

A more interesting area for study, especially if you are working with many platforms and find yourself doing repetitive tasks on each platform (or when you create your Unity assets and need to copy files to a platform to work), is the ability to extend Unity3D's own project build process.

Simply create a normal class script in AssetEditor, and then create your build action function with the [PostProcessBuild] attribute and the build function signature, as follows:

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;

public class MyBuildPostprocessor
{
  [PostProcessBuild]
  public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) 
  { }
}

The attributes from the build processing give you the following information:

  • BuildTarget: This tells you which platform is currently being built using the BuildTarget enumeration.
  • Path: This gives you the output path where the build project is being written. This is useful if you want to copy additional files to it.

You can also control the order in which this function is processed by adding parameters to the [PostProcessBuild] attribute as follows:

[PostProcessBuild(10)]
public static void OnPostprocessBuild(BuildTarget target,
 string pathToBuiltProject) 
{ }

The order number is a definition of priority: the higher the number, the lower the priority. By default, all scripts have a priority of 1. Scripts with lower numbers are executed first (even negative numbers such as -10 are allowed for ultimate priority), whereas scripts with higher numbers are executed last.

This is especially useful if you want to have several actions execute on a successful build and want to control the order in which they are executed.

Note

You can also copy code files directly to the target solution, if you wish, from your Unity project. If you do not want those files to be read or executed by Unity, then simply suffix them with .ignore, and Unity will ignore them. Just remember to rename them when copying them to a platform.

For example:

MyPlatformClassFile.cs.ignore

For more information about build processing, see the Unity scripting reference guide at http://docs.unity3d.com/412/Documentation/ScriptReference/PostProcessBuildAttribute.html.

Tip

For a very full-featured example of highly customized build processing, check out the AdRotator Unity plugin, which is open source, on GitHub at:

https://github.com/Adrotator/AdrotatorV2/tree/master/AdRotatorUnityPackage

Just check in the AdRotatorUnitySDK.AssetsEditorAdRotatorPostBuild.cs script.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.190.211