Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2010
    Posts
    63

    Working on an AssetPostprocessor figured I would share...

    So I have been working on an AssetPostprocessor and I thought I would share because I wanted feedback on if I am doing this correctly. I feel like I am still a novice in C# but I also think something like this may help in the long run.

    I use it to give 3D modelers a set of rules to apply to their models so that when I bring the models into Unity all the proper scripts get attached and the world basically builds itself. Might be useful for someone wanting to make a large game with a small team. It really streamlines things a lot I think.

    Anyways here is the code... so far only two files just so you get the idea. I want it to be able to scale up:

    Code:
    //ZergCow 2011
    //Create a C# script (I think you have to place this in an editor folder) then copy and past this code into that file.
    
    using UnityEngine; 
    using UnityEditor; 
    using System.IO;
    using System.Reflection;
    
    public class PostProcessForMaya : AssetPostprocessor {
      
    #region Variables
    	private static bool _clearLogQ = false; //keep track on if the console is clear
    #endregion
    	
    	public override int GetPostprocessOrder ()
        {
            if (!_clearLogQ) { ClearLog("GetPostprocessOrder"); }
    		return 1;
    	}
    	
    #region 	Processor Functions	
    	
    	void OnPreprocessTexture (){
    		if(!_clearLogQ){ClearLog("OnPreprocessTexture");}
    	}
    		
    	void OnPostprocessTexture (Texture2D tex){
    		if(!_clearLogQ){ClearLog("OnPostprocessTexture");}
    	}
    	
    	void OnPreprocessAudio (){
    		if(!_clearLogQ){ClearLog("OnPreprocessAudio");}
    	}
    
    	void OnPostprocessAudio (AudioClip tex){
    		if(!_clearLogQ){ClearLog("OnPostprocessAudio");}
    	}
    	
    	void OnPreprocessModel (){
    		if(!_clearLogQ){ClearLog("OnPreprocessModel");}
    		var modelImport = assetImporter as ModelImporter;
            if (CheckModelDirectory("Assets/_Models/DES")) DESImportRules.PreprocessModelRuleSet(modelImport);
    	}
    		
    	void OnAssignMaterialModel (Material mat, Renderer rend){
    		if(!_clearLogQ){ClearLog("OnAssignMaterialModel");}
    	}
    	
    	void OnPostprocessGameObjectWithUserProperties (GameObject go, string[] names, object[] values) {
    		if(!_clearLogQ){ClearLog("OnPostprocessGameObjectWithUserProperties");}
            if (CheckModelDirectory("Assets/_Models/DES")) DESImportRules.PostprocessGameObjectWithUserPropertiesRuleSet(go, names, values);
    	} 
    	
    	void OnPostprocessModel (GameObject go){
    		if(!_clearLogQ){ClearLog("OnPostprocessModel");}
            if (CheckModelDirectory("Assets/_Models/DES")) DESImportRules.PostprocessModelRuleSet(go);
    	}
    	
    #endregion
    	
    	static void OnPostprocessAllAssets (string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPath){
    		Debug.Log("importedAssets: " + importedAssets.Length + " --- deletedAssets: " + deletedAssets.Length + " --- movedAssets: " + movedAssets.Length + " --- movedFromPath: " + movedFromPath.Length);
    		if(_clearLogQ){_clearLogQ=false;}
    	}
    
    #region Custom Functions
    	
    	bool CheckModelDirectory(string pathToCheck){
    		if(Path.GetDirectoryName(assetPath).ToLower() != pathToCheck.ToLower()){
    			Debug.LogError("ERROR: The directory "+pathToCheck.ToLower()+" was not found");
    			return false;
    		}
    		return true;
    	}
    
        static void ClearLog(string clearFrom){ //WARNING: this is undocumented and may break on version updates. Use Red Gate's .NET Reflector to find the method.
    		var assembly = Assembly.GetAssembly(typeof(SceneView));    
    		var type = assembly.GetType("UnityEditorInternal.LogEntries");    
    		var method = type.GetMethod("Clear");    
    		method.Invoke(new object(), null);
    		_clearLogQ = true;
    		Debug.Log("Just Cleared Console from the Method: " + clearFrom);        
    	}
    #endregion
    }
    Code:
    //ZergCow 2011
    //Create a C# script (I think you have to place this in an editor folder) then copy and past this code into that file.
    
    using UnityEditor;
    using UnityEngine;
    
    public class DESImportRules
    {
        #region Custom Attribuites
    
        private const string BoolAddCamera = "AddCamera";
        private const string BoolAddPointLight = "AddPointLight";
    
        #endregion
    
        public static void PreprocessModelRuleSet(ModelImporter modelImporte)
        {
            //not using animations so I am leaving the details for that out.
            modelImporte.globalScale = 1;
            modelImporte.meshCompression = ModelImporterMeshCompression.Off;
            modelImporte.addCollider = true;
            modelImporte.swapUVChannels = false;
            modelImporte.generateSecondaryUV = false;
            modelImporte.normalImportMode = ModelImporterTangentSpaceMode.Import;
            modelImporte.tangentImportMode = ModelImporterTangentSpaceMode.Calculate;
            modelImporte.splitTangentsAcrossSeams = true;
            modelImporte.generateMaterials = ModelImporterGenerateMaterials.PerTexture;
            modelImporte.generateAnimations = ModelImporterGenerateAnimations.None;
        }
    
        public static void PostprocessGameObjectWithUserPropertiesRuleSet(GameObject go, string[] propNames, object[] propValues)
        {
            for (var i = 0; i < propNames.Length; i++)
            {
                var propName = propNames[i];
                var propValue = propValues[i];
    
                if (propName.ToLower() == BoolAddCamera.ToLower() && (bool)propValue == true)
                {
                    go.transform.Rotate(0, -90, 0);
                    go.AddComponent(typeof(Camera));
                    go.GetComponent<Camera>().depth = 0;
                }
    
                if (propName.ToLower() == BoolAddPointLight.ToLower() && (bool)propValue == true)
                {
                    go.AddComponent("Light");
                    go.GetComponent<Light>().type = LightType.Point;
                    go.GetComponent<Light>().shadows = LightShadows.Soft;
                    go.GetComponent<Light>().intensity = 0.25f;
                }
            }
        }
    
        public static void PostprocessModelRuleSet (GameObject go)
        {
            //do something
        }
    }

  2. #2
    Join Date
    Jul 2010
    Posts
    63
    I know I didnt get much interest here but, I showed this to the Houston Unity Group and they loved it. They wanted to know more and wanted to know the ins and outs of this so they could use it more. So I am working on making them a set of tutorial videos in the same style that 3D buzz does it by showing the code as I type.

    I just think this might be something 3D buzz should look at making a tutorial for as an extension to the "simple maya level import" or something. Just a thought.

    In my tutorial I am basically going to show how to add custom attribuites to Maya objects then show you how to read those in Unity and do things with them.

  3. #3
    Join Date
    Jan 2011
    Location
    Ohio USA
    Posts
    44
    I am still new to most of this so every little bit helps and my 'team' is very small indeed. It consists of 3 people, one of which only does story and game design...

    Thank you for posting this.

    Regards,

    Code0wnz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •